Solved

How do I make my cpp connect() timeout?

Posted on 2009-04-02
9
848 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now