Solved

How do I make my cpp connect() timeout?

Posted on 2009-04-02
9
835 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

19 Experts available now in Live!

Get 1:1 Help Now