I have an application that opens a socket and connects to another
application listening over a port. The problem I am encountering is that
when the listening application is closed my application cannot detect it to
take appropriate action. I am using "Connected" property of the Socket
class, but it seems this property does not reflect the true state of the
socket connection.
Here is the code snippet. Basically, I am checking the "Socket.Connect ed"
property in a while loop and want to drop out of the loop as soon as the
listening application is closed. The problem is when I close the listening
application, the while loop continues happily. Any suggestions on how to
make the Socket.Connecte d property to reflect true state of socket
connection? Any other ideas?
Thanks in advance,
Socket MySocket = null;
IPEndPoint Ep;
MySocket = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
Ep = new IPEndPoint(IPAd dress, Port);
// connect to another application listening
MySocket.Connec t (EpF);
while (MySocket.Conne cted )
{
// use socket to send/receive data
// Drop out of this loop if the listening
// application is closed.
}