K
Keith Langer
I have a tcpclient which is created from a tcplistener. Once the
connection is created, I issue a GetStream.Read with a ReceiveTimeout
of 0. This causes the thread to block until either data is received
or an exception is thrown.
Under normal circumstances, when the requester closes the connection I
would expect a System.IO.IOException. When the requester's process is
terminated abruptly, I get a System.InvalidOperationException. Either
way the Read operation stops blocking when the connection is closed.
Today I found one case where the tcpclient has no clue that the
connection was broken. If I connect via a VPN tunnel and then break
the VPN connection, the tcpclient continues to block on the Read
operation and an exception never occurs. I've waited up to five
minutes and still no exception. Now I have a thread that is blocking
forever on a non-existent connection! This lead me to have to
rearchitect the application to use a specific timeout and also abort
the connection if no data is received within a specific period of
time. I also found that when the program is shut down the thread will
not abort properly if it has issued a blocking Read operation. I've
tried issuing a Close and setting the tcplistener to nothing, but to
no avail.
Is there a way to block on the Read operation but still get an
exception when a socket is improperly closed?
thanks,
Keith
connection is created, I issue a GetStream.Read with a ReceiveTimeout
of 0. This causes the thread to block until either data is received
or an exception is thrown.
Under normal circumstances, when the requester closes the connection I
would expect a System.IO.IOException. When the requester's process is
terminated abruptly, I get a System.InvalidOperationException. Either
way the Read operation stops blocking when the connection is closed.
Today I found one case where the tcpclient has no clue that the
connection was broken. If I connect via a VPN tunnel and then break
the VPN connection, the tcpclient continues to block on the Read
operation and an exception never occurs. I've waited up to five
minutes and still no exception. Now I have a thread that is blocking
forever on a non-existent connection! This lead me to have to
rearchitect the application to use a specific timeout and also abort
the connection if no data is received within a specific period of
time. I also found that when the program is shut down the thread will
not abort properly if it has issued a blocking Read operation. I've
tried issuing a Close and setting the tcplistener to nothing, but to
no avail.
Is there a way to block on the Read operation but still get an
exception when a socket is improperly closed?
thanks,
Keith