?
Solved

Connection hangs using C Socket Library

Posted on 2006-04-08
5
Medium Priority
?
320 Views
Last Modified: 2010-04-15
I'm using the C socket library to create a program which downloads and processes certain html documents.

Sometimes, however, I come accross a site that works fine on a browser or with wget, but will cause my program to hang endlessly when I try to download it using the C socket library.  

The routine where the data is actually downloaded is this:

      char buf[MAXRECV + 1];
      int status;
      while (status != 0) {
            memset(buf, 0, MAXRECV + 1);
            status = ::recv(m_sock, buf, MAXRECV, 0); // this statement causes the hang
      }

An example website which works normally through a browser, but hangs with C socket library is http://natzoo.si.edu/Animals/WebCams/

Can anyone offer some possible explanations for this?

Thanks.
0
Comment
Question by:chsalvia
  • 4
5 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 1000 total points
ID: 16407464
seems you run sockets in synchronous mode, so recv is waiting for incoming data, if you want it to exit, if nothing is available, you have to run sockets in asynchronous mode (nonblocking sockets).

here is an example, how to set a socket nonblocking (non-win-example) :

http://docsun.cites.uiuc.edu/sun_docs/C/solaris_9/SUNWdev/NETPROTO/p37.html


ike
0
 
LVL 20

Expert Comment

by:ikework
ID: 16407471
here is another one:

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzab6/rzab6xnonblock.htm

take a look to the ioctl() - function. with this you set the socket nonblocking ...
0
 
LVL 20

Expert Comment

by:ikework
ID: 16407493
// set socket non-blocking:

int    on = 1;
rc = ioctl(listen_sd, FIONBIO, (char *)&on);
if (rc < 0)
{
   perror("ioctl() failed");
   close(listen_sd);
   exit(-1);
}
0
 
LVL 20

Expert Comment

by:ikework
ID: 16407502
and the same for windows:
/*
int ioctlsocket(
  SOCKET s,
  long cmd,
  u_long* argp
);
*/

int    on = 1;
rc = ioctlsocket(listen_sd, FIONBIO, (char *)&on);
if (rc < 0)
{
   perror("ioctlsocket() failed");
   close(listen_sd);
   exit(-1);
}
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 1000 total points
ID: 16410140
Your code doesn't check the return value of  recv() for an error.


Should have something such as...
#include <errno.h>

...
...
 status = ::recv(m_sock, buf, MAXRECV, 0); // this statement causes the hang
if (status == -1 &&   errno !=  EINTR  && errno != EAGAIN) {
    perror("socket");
        break;
}

Use select() to wait for a status change on the socket before performing the recv call.


    char buf[MAXRECV + 1];
    fd_set readset;
    int status, temp;

     while (status != 0)
     {
          FD_ZERO(&readset);
          FD_SET(m_sock, &readset);

          memset(buf, 0, MAXRECV + 1);
          if ((temp = select(m_sock + 1, &readset, NULL, NULL, NULL) )  > 0 ) {                        
              status = ::recv(m_sock, buf, MAXRECV, 0); // this statement causes the hang
              if (status == -1) {
                   perror("recv");
                   break;
              }
          }
          else if (temp == -1) {
               if (errno != EINTR) {  perror("select");  }
          }
     }

You really don't need non-blocking sockets  for a simple interactive client application: turning on
the non-blocking flag is ONE thing, but there are other major changes you would need to make
to properly use a non-blocking socket.

To use a non-blocking socket, you need to flag it non-blocking just after creation and wait for
a positive select()  response on the socket before each operation on the descriptor, including a connect()
or write().

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

850 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