1

So I am trying to setup a multithreaded server with ACE. I am using non-blocking client sockets to prevent recv()/send() from blocking. The problem is when I use recv() and the client disconnects ungraceful, the result of recv() does not give me a hind that the client disconnected. Is there any other methode to check the connectivity.

Here is a short snippet

            char buffer[4096];
            ssize_t bytesReceived = peer.recv(buffer, sizeof(buffer));

            if (bytesReceived < 1 && errno != EWOULDBLOCK)
            {
                printf("Disconnected:\n");

            }
            else if (bytesReceived > 0)
            {
                buffer[bytesReceived] = '\0';
                printf(buffer);
            }

So if the client disconnects, recv returns -1 but errno is still EWOULDBLOCK.

I also tried to use a short timeout in recv, but it leads to the same result as without just with errno = ETIME(TIME-Out).

Mathias Hölzl
  • 263
  • 4
  • 16
  • Have you tried some connection timeouts? Mayne some ping sequences? – Oleg Olivson Oct 31 '13 at 11:53
  • Yes but on disconnect recv() waits until the timeout is reached and then returns -1 with errno ETIME. So I am still not able to detect a disconnect. I am not allowed to send "unnecessary" pakets to the client. – Mathias Hölzl Oct 31 '13 at 11:56
  • Not recv timeout, but timeout between two data transfers. If you don't receive any data during specified time, you suggest that socket is disconnected. – Oleg Olivson Oct 31 '13 at 11:59
  • This is also not possible because its like a video-server and it could happen that no data is transfered for hours. – Mathias Hölzl Oct 31 '13 at 12:02
  • http://blog.stephencleary.com/2009/05/detection-of-half-open-dropped.html – Oleg Olivson Oct 31 '13 at 12:09
  • Thank you thats a very interessting blog. But the offered sulutions 1,2 are not possible because I cant change the client. 3 is not possible because I am not aware how long the next respond can take. And 4 seems to be not really portable. I dont get why they not use recv() results like http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx – Mathias Hölzl Oct 31 '13 at 12:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40320/discussion-between-oleg-olivson-and-mathias-holzl) – Oleg Olivson Oct 31 '13 at 12:25

0 Answers0