Solved

How do I make my cpp connect() timeout?

Posted on 2009-04-02
9
859 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
getting a fire fox page (view page source ) in c++ 6 42
Problem to ASCII 1 178
is twain_32.dll cmpatible with windows 10 ? 10 163
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

778 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