Solved

Creating a Non-Blocking Socket

Posted on 2004-10-11
7
314 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
ID: 12275862
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12275898
0
 

Expert Comment

by:Popoi
ID: 12281078
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:sasabunetic
ID: 12285933
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
ID: 12288776
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
ID: 12295728
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
ID: 12299528
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

809 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