How to use the Socket "Connected" property properly?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr. J

    How to use the Socket "Connected" property properly?

    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.

    }


  • Ian Griffiths [C# MVP]

    #2
    Re: How to use the Socket "Connected " property properly?

    Welcome to TCP...

    The way you discover that the remote end has closed a connection is by
    attempting to read from it. The read attempt returns no data, indicating
    that the connection has closed.

    This is not unique to .NET by the way - most sockets do this. (The same
    issue exists on Linux for example.)

    The reason for this is that because of the way TCP works, it is valid for
    the client to continue to send data to the server even though the server may
    have closed the connection at its end. All it really means for the server
    to close the connection is that it is shutting down its ability to transmit
    data to the client. The connection is in a state sometimes described as
    'half-open'.

    So that's it's the Read operation that tells you the server has closed the
    connection - the fact that the server has closed its connection simply means
    that you won't be receiving any more data from it. This is not necessarily
    going to prevent you from sending it more data, which is why the connection
    doesn't appear to be closed at this point.


    --
    Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
    DevelopMentor - http://www.develop.com/

    "Dr. J" wrote:[color=blue]
    >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.
    >
    > }[/color]


    Comment

    Working...