Solved

Creating a Non-Blocking Socket

Posted on 2004-10-11
7
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

690 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