[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Creating a Non-Blocking Socket

Posted on 2004-10-11
7
Medium Priority
?
318 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 750 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: 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.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 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