Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I make my cpp connect() timeout?

Posted on 2009-04-02
9
Medium Priority
?
895 Views
Last Modified: 2012-05-06
Hi all,

I have an iPhone app that utilizes a cpp class I made that HTTP posts to my server. I am currently trying to detect when the user has entered in a bad IP address. Please see the attached line of code. With some bogus IP addresses, the connect() command goes through and returns n = -1, and I therefore know when to flag an error and tell the user to change the IP.

The problem is, with SOME IP addresses, the connect() doesn't go through and return n= -1 or something like that, it just SITS there on that line, indefinitely. Meanwhile, the app looks like it has crashed.

How do I make the program go on after a certain length of time if the connect() statement doesn't progress on its own?
int n = connect(socketFD, (SOCKA *) & serverAddress, sizeof(serverAddress));

Open in new window

0
Comment
Question by:ehensens
  • 5
  • 4
9 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24051996
You can make the socket non-blocking. Then the connect immediately returns with socket error -1 and errno set to E_WOULDBLOCK (GetLastError() == WSAEWOULDBLOCK on Windows). You, then could call select with timeout to check whether the socket has turned to a writeable state. When select returns timeout you could cancel the connect and output a user message.    
0
 

Author Comment

by:ehensens
ID: 24052014
Hi itsmeandnobodyelse,

Could you give an example? And where do I make the socket non-blocking, on my server or on my client?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24052163
To set the socket non-blocking do

  unsigned long onoff = 1;
  int rc = ioctlsocket(socketFD, FIONBIO, &onoff);
  if (rc < 0) return rc;  // socket error

if you pass onoff = 0 the socket was reset to blocking mode.

To check for connection is ok you do

    int n = connect(socketFD, (SOCKA *) & serverAddress, sizeof(serverAddress));
    if (n < 0 && errno == E_WOULDBLOCK)
    {
         fd_set writeset = { 0 };
         TIMEVAL timeout = {TIMEOUT_SECS, TIMEOUT_USECS};

         FD_SET(socketFD, &writeset);
         
         int rc = select(0, NULL, &writeset, NULL, &timeout);
         if (rc == 0)  // timeout
         {
         }

    }

0
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!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24052177
>>> And where do I make the socket non-blocking, on my server or on my client?

On the client just before the connection. You should make it reverse to blocking (normally) after getting a connection.
0
 

Author Comment

by:ehensens
ID: 24052242
Are there extra include files I need? I am getting error "TIMEVAL was not declared in this scope"
0
 

Author Comment

by:ehensens
ID: 24052250
Also,

FIONBIO was not declared
ioctlsocket was not declared
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24052362
>>>> FIONBIO was not declared
Hmm. It was FIONBIO at Unix and Windows. Can you check your docs for ioctlsocket? Or ctlsocket? I mean to remember that there was a naming mixup with ioctlsocket.

>>>> Are there extra include files I need?

Probably. The TIMEVAL is a struct with two unsigned long. You can make your own structure or check your docs what select is expecting for last argument.

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 24052394
I googled and found fcntl  for ioctlsocket

The call is

    fcntl(socketFD, FD_SETFL, O_NONBLOCK);
0
 

Author Comment

by:ehensens
ID: 24052835
yup thats it, thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

877 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