Detect client socket disconnect

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

    Detect client socket disconnect

    How do I detect if a client socket is no longer connected to the listen tcp
    socket ?

    I have tried with (just an example):

    ---------------------

    Socket tcpSocket;

    while(tcpSocket .Connected)
    {

    if(tcpSocket.Av ailable)
    {
    // Receive incomming buffer
    }

    Thread.Sleep(10 00;
    }

    // Client socket is disconnected


    ---------------------

    The problem is that tcpSocket.Conne cted never gets false, when the client
    first is connected. Even though the client do a client.Close(), it still
    sees that its connected.

    Best regards
    -Morten






  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Detect client socket disconnect

    Morten,

    This is just a guess, but have you read everything from the buffer? If
    there is information in the buffer to be read, then it might not pick up the
    disconnected state until the buffer is cleared.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Morten" <morten@n.n> wrote in message
    news:eOA6AsJyDH A.2556@TK2MSFTN GP10.phx.gbl...[color=blue]
    > How do I detect if a client socket is no longer connected to the listen[/color]
    tcp[color=blue]
    > socket ?
    >
    > I have tried with (just an example):
    >
    > ---------------------
    >
    > Socket tcpSocket;
    >
    > while(tcpSocket .Connected)
    > {
    >
    > if(tcpSocket.Av ailable)
    > {
    > // Receive incomming buffer
    > }
    >
    > Thread.Sleep(10 00;
    > }
    >
    > // Client socket is disconnected
    >
    >
    > ---------------------
    >
    > The problem is that tcpSocket.Conne cted never gets false, when the client
    > first is connected. Even though the client do a client.Close(), it still
    > sees that its connected.
    >
    > Best regards
    > -Morten
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Morten

      #3
      Re: Detect client socket disconnect

      Yes I'm quiet sure since I check for available data.

      public void HandleRequest(O bject stateInfo)
      {
      Socket tcpSocket;
      tcpSocket = (Socket)stateIn fo;
      int Available = 0;

      while(Form1.bLi sten)
      {
      Debug.WriteLine ("##TRACE##: Looking for incomming data...");

      if((Available = tcpSocket.Avail able) > 0)
      {

      Debug.WriteLine ("##TRACE##: Data incomming...");

      }
      else if(!tcpSocket.C onnected)
      {
      // Never gets to this point.
      RaiseNotifyEven t(NotifyCommand .Disconnected, "null");
      break;
      }
      Thread.Sleep(10 00);
      }
      Regards,
      Morten

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:uJcikwJyDH A.1736@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Morten,
      >
      > This is just a guess, but have you read everything from the buffer? If
      > there is information in the buffer to be read, then it might not pick up the
      > disconnected state until the buffer is cleared.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Morten" <morten@n.n> wrote in message
      > news:eOA6AsJyDH A.2556@TK2MSFTN GP10.phx.gbl...[color=green]
      > > How do I detect if a client socket is no longer connected to the listen[/color]
      > tcp[color=green]
      > > socket ?
      > >
      > > I have tried with (just an example):
      > >
      > > ---------------------
      > >
      > > Socket tcpSocket;
      > >
      > > while(tcpSocket .Connected)
      > > {
      > >
      > > if(tcpSocket.Av ailable)
      > > {
      > > // Receive incomming buffer
      > > }
      > >
      > > Thread.Sleep(10 00;
      > > }
      > >
      > > // Client socket is disconnected
      > >
      > >
      > > ---------------------
      > >
      > > The problem is that tcpSocket.Conne cted never gets false, when the client
      > > first is connected. Even though the client do a client.Close(), it still
      > > sees that its connected.
      > >
      > > Best regards
      > > -Morten
      > >
      > >
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]

      Comment

      • Garrison

        #4
        Re: Detect client socket disconnect

        I would like to second this question.

        The Connected property of the Socket class does not return false when the
        Socket no longer is connected to the host connection. Is there another way
        to verify that the Socket is disconnected?

        Garrison

        "Morten" <morten@n.n> wrote in message
        news:eOA6AsJyDH A.2556@TK2MSFTN GP10.phx.gbl...[color=blue]
        > How do I detect if a client socket is no longer connected to the listen[/color]
        tcp[color=blue]
        > socket ?
        >
        > I have tried with (just an example):
        >
        > ---------------------
        >
        > Socket tcpSocket;
        >
        > while(tcpSocket .Connected)
        > {
        >
        > if(tcpSocket.Av ailable)
        > {
        > // Receive incomming buffer
        > }
        >
        > Thread.Sleep(10 00;
        > }
        >
        > // Client socket is disconnected
        >
        >
        > ---------------------
        >
        > The problem is that tcpSocket.Conne cted never gets false, when the client
        > first is connected. Even though the client do a client.Close(), it still
        > sees that its connected.
        >
        > Best regards
        > -Morten
        >
        >
        >
        >
        >
        >[/color]


        Comment

        • Rich Blum

          #5
          Re: Detect client socket disconnect

          "Morten" <morten@n.n> wrote in message news:<eOA6AsJyD HA.2556@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
          > How do I detect if a client socket is no longer connected to the listen tcp
          > socket ?
          >
          > I have tried with (just an example):
          > .
          > .
          > The problem is that tcpSocket.Conne cted never gets false, when the client
          > first is connected. Even though the client do a client.Close(), it still
          > sees that its connected.
          >[/color]
          Morten -

          The Connected property shows the state of the socket at the time
          of the last IO operation. Thus the socket could be closed by the
          remote host with the Connected property value still being true. The
          value won't change until after you try reading or writing to the
          socket.

          You should be able to use the Available property to determine if
          the remote host has closed the connection. The Available property will
          either return the number of bytes available to be read on the socket
          (remember that its possible for a connected socket to still return
          zero bytes of data), or will throw a SocketException if the remote
          host shuts down or has closed the connection. All you should need to
          do is catch the SocketException in your code. (note that this behavior
          changed in .NET 1.1. In 1.0 the Available property just returned zero
          if the remote host closed the connection).

          Alternatively, you can also utilize the Poll() Socket method with
          the SelectRead SelectMode parameter. This method will return a true
          value if data is available or if the connection has been closed by the
          remote host. You will then need to differentiate between which of
          these situations has occurred (by reading the socket buffer, and
          seeing if it returns a zero value).

          Hope this helps shed some light on your problem.

          Rich Blum - Author
          "C# Network Programming" (Sybex)

          "Network Performance Open Source Toolkit" (Wiley)

          Comment

          • Morten

            #6
            Re: Detect client socket disconnect

            Thank you Rich Blum, especially the point with using the Poll method worked
            out my problem. Great! Thanx :)

            -Morten

            "Rich Blum" <rich_blum@juno .com> wrote in message
            news:ccdac0da.0 312230438.2941d eec@posting.goo gle.com...[color=blue]
            > "Morten" <morten@n.n> wrote in message[/color]
            news:<eOA6AsJyD HA.2556@TK2MSFT NGP10.phx.gbl>. ..[color=blue][color=green]
            > > How do I detect if a client socket is no longer connected to the listen[/color][/color]
            tcp[color=blue][color=green]
            > > socket ?
            > >
            > > I have tried with (just an example):
            > > .
            > > .
            > > The problem is that tcpSocket.Conne cted never gets false, when the[/color][/color]
            client[color=blue][color=green]
            > > first is connected. Even though the client do a client.Close(), it still
            > > sees that its connected.
            > >[/color]
            > Morten -
            >
            > The Connected property shows the state of the socket at the time
            > of the last IO operation. Thus the socket could be closed by the
            > remote host with the Connected property value still being true. The
            > value won't change until after you try reading or writing to the
            > socket.
            >
            > You should be able to use the Available property to determine if
            > the remote host has closed the connection. The Available property will
            > either return the number of bytes available to be read on the socket
            > (remember that its possible for a connected socket to still return
            > zero bytes of data), or will throw a SocketException if the remote
            > host shuts down or has closed the connection. All you should need to
            > do is catch the SocketException in your code. (note that this behavior
            > changed in .NET 1.1. In 1.0 the Available property just returned zero
            > if the remote host closed the connection).
            >
            > Alternatively, you can also utilize the Poll() Socket method with
            > the SelectRead SelectMode parameter. This method will return a true
            > value if data is available or if the connection has been closed by the
            > remote host. You will then need to differentiate between which of
            > these situations has occurred (by reading the socket buffer, and
            > seeing if it returns a zero value).
            >
            > Hope this helps shed some light on your problem.
            >
            > Rich Blum - Author
            > "C# Network Programming" (Sybex)
            > http://www.sybex.com/sybexbooks.nsf/Booklist/4176
            > "Network Performance Open Source Toolkit" (Wiley)
            > http://www.wiley.com/WileyCDA/WileyT...471433012.html[/color]


            Comment

            Working...