Solved

Detect if a client lost its socket connection to the server application

Posted on 2003-12-11
3
1,292 Views
Last Modified: 2008-03-17
I have a server application which sets up a socket, waits for a connection and then is supposed to process the received data (socket(), bind(), listen(), accept())

So far I then end up in this loop:

      while(1) {
            fd_set fds;
            struct timeval tv;
            char buf[MAX_PACKET_SIZE];
            int size;

            FD_ZERO(&fds);
            FD_SET(clientsock, &fds);
            tv.tv_sec = 0;
            tv.tv_usec = 0;

            if(select(clientsock+1, &fds, NULL, NULL, &tv))      {
                  /*data is available, read and send it back*/
                  size = recv(clientsock, buf, MAX_PACKET_SIZE, 0);
                  buf[size] = 0;
                  cout << buf;
                  send(clientsock, buf, size, 0);
            }
      }


That's okay - but how do I detect a lost connection? At the moment I end up in an endless loop and have no chance of knowing if the client is still there. In the final product I have to return to waiting for a new connection, but then I first have to know when the initial connection was ended.
0
Comment
Question by:BlaM
  • 2
3 Comments
 

Author Comment

by:BlaM
ID: 9919279
Oh yes, by the way: It's a linux application ;) - forgot to mention.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 9919501
select returns SOCKET_ERROR - that is -1 and defined in UNIX systems as well as in Windows - if any of the sockets in fd_set get disconnected. The specific error is in errno.

A return value of 0 means success.

So, you should define

        #define SOCKET_SUCCESS 0

and change the if statement above to

     if(select(clientsock+1, &fds, NULL, NULL, &tv) == SOCKET_SUCCESS)


Hope that helps

Alex
0
 

Author Comment

by:BlaM
ID: 9919583
Thanks. That was simpler than I thought ;)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 55
C++ get user from AD  (VS6) 7 73
Template syntax for variable length arrays 9 75
Unable to start eclipse ? 17 141
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

776 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