Hello all,
it aint usual that I post a question, but this one has me stumped !! I am of the belief that we should always program 'defensively', as is Microsofts want. Exceptions are just that, exceptions to the programs normal flow, as they are expensive in terms of resources etc. we should program defensively and cater for invalid status(es) in our logic. With this in mind I have the following code (sorry about the formatting of it but this BB is sh!te and has re-formatted it as it wants)...
if (m_objSocket != null)
{
m_objSocket.Send(System.Text.Encoding.ASCII.GetByt es("You are now connected!\r\n"));
// *** Ensure that the Socket is Connected ***
while (m_objSocket.Connected)
{
// *** Thread Processing ***
m_objSocket.Send(System.Text.Encoding.ASCII.GetByt es("You are still connected!\r\n"));
// *** Relinquish to the OS ***
Thread.Sleep(100);
}
// *** ShutDown the Socket ***
m_objSocket.Shutdown(SocketShutdown.Both);
m_objSocket.Close();
// *** Fire the Disconnection Event ***
OnClientDisconnnection();
}
The problem is that the 'm_objSocket.Connected' always returns true, even when the Client app (Hyperterminal in my test case) has terminated, then a SocketException is thrown. In the MSDN documentation it says:
<quote>
There is no guarantee that the Socket is still Connected even if Connected returns true. A value of true simply means that the Socket was connected at the time of the last I/O operation.
</quote>
How the hell are we supposed to program defensively if we have to handle the disconnection of a Client Socket by trapping the applicable exception ??
If anyone can throw any light on this I would be most grateful....
Rgds,
D
it aint usual that I post a question, but this one has me stumped !! I am of the belief that we should always program 'defensively', as is Microsofts want. Exceptions are just that, exceptions to the programs normal flow, as they are expensive in terms of resources etc. we should program defensively and cater for invalid status(es) in our logic. With this in mind I have the following code (sorry about the formatting of it but this BB is sh!te and has re-formatted it as it wants)...
if (m_objSocket != null)
{
m_objSocket.Send(System.Text.Encoding.ASCII.GetByt es("You are now connected!\r\n"));
// *** Ensure that the Socket is Connected ***
while (m_objSocket.Connected)
{
// *** Thread Processing ***
m_objSocket.Send(System.Text.Encoding.ASCII.GetByt es("You are still connected!\r\n"));
// *** Relinquish to the OS ***
Thread.Sleep(100);
}
// *** ShutDown the Socket ***
m_objSocket.Shutdown(SocketShutdown.Both);
m_objSocket.Close();
// *** Fire the Disconnection Event ***
OnClientDisconnnection();
}
The problem is that the 'm_objSocket.Connected' always returns true, even when the Client app (Hyperterminal in my test case) has terminated, then a SocketException is thrown. In the MSDN documentation it says:
<quote>
There is no guarantee that the Socket is still Connected even if Connected returns true. A value of true simply means that the Socket was connected at the time of the last I/O operation.
</quote>
How the hell are we supposed to program defensively if we have to handle the disconnection of a Client Socket by trapping the applicable exception ??
If anyone can throw any light on this I would be most grateful....
Rgds,
D
Comment