Link to home
Start Free TrialLog in
Avatar of sasabunetic
sasabunetic

asked on

Creating a Non-Blocking Socket

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
Avatar of Amritpal Singh
Amritpal Singh
Flag of India image

Avatar of Popoi
Popoi

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
Avatar of sasabunetic

ASKER

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?
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
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
ASKER CERTIFIED SOLUTION
Avatar of Popoi
Popoi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial