Solved

Creating a Non-Blocking Socket

Posted on 2004-10-11
7
312 Views
Last Modified: 2013-11-20
Hi!

See the source code below:

  s = socket(AF_INET, SOCK_STREAM, 0);  
  if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) == SOCKET_ERROR) {
    MessageBox(&WSERROR3[0], &WSTITLE[0], MB_OK | MB_ICONSTOP);
    WSACleanup();
    return;
  }

  retval = send(s,&buf[0],len,0);          // Send string


Code is working OK, but there is a problem if connection is not established, then delay is 20 sec and in this time dialog is death. This 20 sec is too much for me. I've heared that this can be fixed with non-blocking socket. This means that delay is still 20 sec, but in that time dialog is not death. How can I do this in my case? Is it possible to reduce this 20 sec, down to 5 sec?

Thanks
0
Comment
Question by:sasabunetic
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:Amritpal Singh
Comment Utility
0
 
LVL 6

Expert Comment

by:Amritpal Singh
Comment Utility
0
 

Expert Comment

by:Popoi
Comment Utility
Good links above.

Also: if you cannot create a nonblocking socket for some reason (there are plenty), then you can use connect() in a different thread.

As a rule, you shouldn't be doing anything that might be time consuming in the GUI thread, if you do it will lead to the "dead dialog" syndrome you describe. Create a worker thread instead.

hope this helps
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sasabunetic
Comment Utility
Thanks for tips, but I still have problems.
I've tried to used ioctlsocket function. See the source:

  if(ioctlsocket(s, FIONBIO, (u_long FAR*)&intNonBlock ) == SOCKET_ERROR) {
    MessageBox(&WSERROR5[0], &WSTITLE[0], MB_OK | MB_ICONSTOP);
    return;
  }

  if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) == SOCKET_ERROR) {
    MessageBox(&WSERROR3[0], &WSTITLE[0], MB_OK | MB_ICONSTOP);
    WSACleanup();
    return;
  }

So when I use it like this, I don't even get the socket connect if the cable is connected to board. When I don't use ioctlsocket() and cable is connected to board, communication is working ok. But I would like to use nonblocking mode, in case the cable is not connected.
Do you have any suggestion?
0
 

Expert Comment

by:Popoi
Comment Utility
Hmm.. i'm not 100% sure what you mean. But let me see if i understand: connect() returns SOCKET_ERROR, and WSAGetLastError returns  WSAEWOULDBLOCK.

then you would follow these steps (quoted from MSDN)

there are three possible scenarios:

- Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.

- If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).

-If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).

dunno if that helps
0
 

Author Comment

by:sasabunetic
Comment Utility
Thanks for help.
I found in MSDN a functioin "select", which would be appropriate for my application. But I don't know how to set it correct. The source would be something like this:

  if(ioctlsocket(s, FIONBIO, (u_long FAR*)&intNonBlock ) == SOCKET_ERROR) {
    MessageBox(&WSERROR5[0], &WSTITLE[0], MB_OK | MB_ICONSTOP);
    return;
  }
  if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) == SOCKET_ERROR) {
    if (select(0, 0, writefds, exceptfds, timeout) == ?)

The parameters in select() are not correct. See the MSDN. Return from select shoud be one of next:
 - connection has succeeded or
 - connection attempt failed or
 - timeout in 5 sec

Thanks
0
 

Accepted Solution

by:
Popoi earned 250 total points
Comment Utility
sounds like you need an example to follow rather than specific help. check out http://www.sockets.com/ for excellent resources and sample code.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now