Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now