Overview
Features
Download
Documentation
Community
Add-Ons & Services

TCP Server and ConnectionAbortedException

Please post support and help requests here.

TCP Server and ConnectionAbortedException

Postby merdem » 15 Mar 2007, 17:47

Hello all,

Here is the scenario:

I have a TCPServer running and accepting connections. I implemented my own MyTCPServerConnectionFactory which keeps record of the MyTCPServerConnections it creates and dispatches. This allows me to access the MyTCPServerConnection objects when they are running.

In each MyTCPServerConnection run() method, I do something like this:

^void MyTCPServerConnection::run() {
DialogSocket dialog_socket(this->socket());
std::string msg;
try {
while(dialog_socket.receiveMessage(msg)) {
// DO SOMETHING...
}
} catch (ConnectionAbortedException e) {
return;
}
std::cout << "Connection closed by foreign host...
";
}^

So far so good. The TCPServerConnection socket is blocking (I believe it is default). At some point I want to terminate the connection socket. Remember I can access the MyTCPServerConnection object, so I call my own method on the object:

^void MyTCPServerConnection::shutdown() {
this->socket().shutdown();
}^

According to the socket information I read, the call to shutdown() on a blocking socket should wake it and it does but it throws the ConnectionAborted (or WSAECONNABORTED in Winsock, I am using Windows) exception. Again according to the winsock documentation:

^WSAECONNABORTED 10053

Software caused connection abort.

An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error.^

But the UNIX documentation on socket() does not list the EACONNABORTED as an error produced by a socket but only by the accept(). So, I am confused.

To make it more complicated, if I create a DialogSocket (as a client) and connect it to a server, read block it and call shutdown(), it does not throw any exceptions but just returns 0.

I am pretty sure I am missing something somewhere...

Thank you,

Murat
merdem
 
Posts: 14
Joined: 19 Jul 2006, 22:28

Re: TCP Server and ConnectionAbortedException

Postby merdem » 15 Mar 2007, 18:39

UPDATE:

I stand corrected. In the run() method above, when the dialog_socket is blocked on the read operation and another thread calls the shutdown(), dialog_socket does NOT wake-up...
merdem
 
Posts: 14
Joined: 19 Jul 2006, 22:28

Re: TCP Server and ConnectionAbortedException

Postby guenter » 16 Mar 2007, 11:55

Murat,

instead of shutting down the socket, you could simply close() it, which should cause receiveMessage() to throw.
You could also call select() or poll() before you call receiveMessage(), and only call receiveMessage() if select() indicates that there is data available. If you call select() with a reasonable timeout (e.g. 100ms), you could also periodically poll a "close-connection" flag, and, instead of closing the socket from the outside (another thread), simply return from run(), which will close the socket.

guenter
 
Posts: 1111
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests

cron