Overview
Features
Download
Documentation
Community
Add-Ons & Services

SocketStream: How to check connection is closed?

Please post support and help requests here.

SocketStream: How to check connection is closed?

Postby mbue » 19 Aug 2013, 14:54

I'm trying to imlpement the following scenario:

An arbitrary TCP clilent sends TCP messages to an application based on POCO's TcpServer class.
The message consist of a header and a body.

Any client that connects to the server will be processed in a loop within a separate thread (it can be assumed that there will only be a few clients).

The loop handles the following process:

Code: Select all
while ( Connection Is Open) {
    Read Header sent by Client
    Read Body sent by Client
    Process Message
}


The question is, how I can check whether a connection has been closed.
I don't want to assume a closed connection on every exception thrown by POCO. For example, when a receiving timeout occured, the loop should just discard the current message reception and wait for another one.

So how can I check for a closed connection to quit the loop? Is there a certain method or exception that is thrown?

Thank you in advance
mbue
 
Posts: 10
Joined: 07 Jun 2013, 11:34

Re: SocketStream: How to check connection is closed?

Postby guenter » 19 Aug 2013, 15:26

The stream state should be EOF if the peer has closed the connection, or fail/bad if a socket error occurs. You can check with std::ios::eof(), etc.
guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: SocketStream: How to check connection is closed?

Postby mbue » 20 Aug 2013, 09:38

Hello guenter,

thank you for your answer... could you maybe review the following function whether I handle the reception of a buffer correctly? Or maybe there's an easier way to handle this?

I especially wondered about the following:
- Is retrieving the status of SocketStream a good idea when operating directly on StreamSocket::receiveBytes?
- Is it valid to check whether StreamSocket::receiveBytes returns 0? What assumption can be made then? I'm assuming a socket error
- Is it a good idea to keep the connection open when receiving SocketStream::fail() or SocketStream::bad? Or will the operation be likely to fail next time?

The official documentation doesn't tell me much about such things...
A tutorial on error handling would be nice :)

Thank you.

Code: Select all
enum Result {
   ERR,
   DISCONNECT,
   TIMEOUT,
   OK
};

inline Result RecvBuffer(char* dest, size_t size, StreamSocket& sock) {
   SocketStream str(sock);

   short total_bytes = 0;
   short received_bytes = 0;
   try {
      while(total_bytes < size) {
         received_bytes = sock.receiveBytes(dest + total_bytes, size - total_bytes);
         if(str.eof()) {
            return DISCONNECT;
         }
         else if(str.fail() || str.bad() || received_bytes == 0) {
            return ERR;
         }
         total_bytes+=received_bytes;
      }
      return OK;
   }
   catch(Poco::Exception& ex) {
      if(ex.className() == "Poco::TimeoutException")
         return TIMEOUT;
      else
         return ERR;
   }
}
mbue
 
Posts: 10
Joined: 07 Jun 2013, 11:34

Re: SocketStream: How to check connection is closed?

Postby guenter » 20 Aug 2013, 18:30

When using SocketStream, you should do all I/O using the stream functions and not access the underlying socket directly. From looking at your code I see no reason to use SocketStream at all if all you're doing is reading a bunch of bytes into a buffer. When using StreamSocket::receiveBytes(), a close of the connection by the remote site will be indicated by receiveBytes() returning 0 bytes read. This is standard socket behavior. A network error will cause receiveBytes() to throw an exception (most likely Poco::Net::ConnectionAbortedException or Poco::Net::ConnectionResetException), but it's best to catch Poco::Exception so all cases are covered.
guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests