Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-11
3
Medium Priority
?
1,314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

721 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