Solved

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

Posted on 2003-12-11
3
1,301 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

856 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