Solved

How do I make my cpp connect() timeout?

Posted on 2009-04-02
9
880 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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