[Vala] Detect terminated socket connection



Hi list,

I have a simple socket application that passes commands from a telnet connection to a MySql server and returns the results over socket again, if there are any.

In productive use my application receives about 30 statements per second from the client side. Running on a fast and spare machine it sometimes notices way too late that the client side has disconnected. Thus it keeps on receiving client requests that might have been buffered somewhere in the socket's IP stack.

The only way I found out to notice whether the client has disconnected or not, is by checking if the received input is null:
----
var dis = new DataInputStream (conn.input_stream);
var dos = new DataOutputStream (conn.output_stream);
string req = yield dis.read_line_async (Priority.HIGH_IDLE);

while(true)
{
    if(req == null)
    {
        print("Client terminated connection.\n");
        break;//leave endless loop, flush buffer..
    }
}
----
This is not really a solution, because unless the stack's buffer is empty my application won't notice that the client has gone already. The next time the client connects, my buffer hasn't been flushed, so the client receives results/acknowledgments from an older session and thus gets confused.

I tried to check on the ".closed" property of the SocketConnection object but this won't become true when I expect it. So my loop keeps on running even after the connection has been closed.
----
if(conn.closed)//won't happen..
----

The same happens for the socket output. After having received and sent loads of messages, it'll take some time until my application notices that the pipe is broken, i.e. it keeps on sending data for too long until I get the exception "Error sending data: Broken pipe". However, if there is not a lot of traffic, then the code behaves totally normal.

So my question is:
Is there a way to detect a terminated socket connection, before I "provoke" this by reading from / sending to a broken pipe?


Thanks in advance,

Gilzad




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]