Closing Sockets

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

    Closing Sockets

    Assume that in C#, I create a server socket (listener) and code to start
    new threads with each connection using BeginAccept(). After some time,
    I have three threads running, each with their own client socket
    connection. If I close the listener socket, will the client sockets
    also shut down? Or do I need to manually shut these down as well?
  • Peter Duniho

    #2
    Re: Closing Sockets

    "Funke" <newsgroups@daf unks.comwrote in message
    news:12k02riou1 vrg1a@corp.supe rnews.com...
    Assume that in C#, I create a server socket (listener) and code to start
    new threads with each connection using BeginAccept(). After some time, I
    have three threads running, each with their own client socket connection.
    If I close the listener socket, will the client sockets also shut down?
    No, they will not be.
    Or do I need to manually shut these down as well?
    Yes, you do need to. The connected sockets are completely independent of
    the listening socket.

    Pete


    Comment

    • Funke

      #3
      Re: Closing Sockets

      Peter Duniho wrote:
      "Funke" <newsgroups@daf unks.comwrote in message
      news:12k02riou1 vrg1a@corp.supe rnews.com...
      >Assume that in C#, I create a server socket (listener) and code to start
      >new threads with each connection using BeginAccept(). After some time, I
      >have three threads running, each with their own client socket connection.
      >If I close the listener socket, will the client sockets also shut down?
      >
      No, they will not be.
      >
      >Or do I need to manually shut these down as well?
      >
      Yes, you do need to. The connected sockets are completely independent of
      the listening socket.
      Is there any way to get a list connected sockets that originated from the
      listening socket using the listening socket instance?

      Comment

      • Peter Duniho

        #4
        Re: Closing Sockets

        "Funke" <newsgroups@daf unks.comwrote in message
        news:koV%g.7342 0$zF5.49231@big news1.bellsouth .net...
        Is there any way to get a list connected sockets that originated from the
        listening socket using the listening socket instance?
        In .NET, not as far as I know. Typically, an application would maintain its
        own list of connected sockets that are related to a specific listening
        socket.

        I don't think there's even any reliable way to do it outside of .NET either.
        The only real connection between the listening socket and sockets connected
        by that listening socket is the port number, and since one could close a
        listening socket and later open a new one with the same port number, even
        enumerating all your open sockets and comparing the port number doesn't
        necessarily tell you that those open sockets were connected using a specific
        listening socket.

        IMHO, the correct thing to do is simply maintain your own list of related
        sockets, so that you can perform whatever operations on them are needed as
        appropriate.

        Pete


        Comment

        • William Stacey [C# MVP]

          #5
          Re: Closing Sockets

          Your client sockets should be closing themselfs down using your protocol.
          After the client finishes last send, it should shutdown send side. After it
          receives 0 from read, it can close socket as now both sides of socket are
          done. You should only force a Close as a last resort if you need to force
          an app down and don't care what the clients happen to be doing.
          List<Socketis one way to go to keep your scoreboard. Normally, you have
          some other "Client" object that maintains the state of that client - your
          socket object would just be contained in that class so a List of <Client>
          may be the way to go in that case.

          --
          William Stacey [C# MVP]

          "Funke" <newsgroups@daf unks.comwrote in message
          news:koV%g.7342 0$zF5.49231@big news1.bellsouth .net...
          | Peter Duniho wrote:
          | "Funke" <newsgroups@daf unks.comwrote in message
          | news:12k02riou1 vrg1a@corp.supe rnews.com...
          | >Assume that in C#, I create a server socket (listener) and code to
          start
          | >new threads with each connection using BeginAccept(). After some time,
          I
          | >have three threads running, each with their own client socket
          connection.
          | >If I close the listener socket, will the client sockets also shut down?
          | >
          | No, they will not be.
          | >
          | >Or do I need to manually shut these down as well?
          | >
          | Yes, you do need to. The connected sockets are completely independent
          of
          | the listening socket.
          |
          | Is there any way to get a list connected sockets that originated from the
          | listening socket using the listening socket instance?


          Comment

          Working...