Only client side closing the socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • youtubeprog
    New Member
    • Mar 2014
    • 2

    Only client side closing the socket

    Hi,


    I have a question about client/server socket communication: suppose I have a client socket connecting to a server socket and this client sends some bytes to the server, but there is no sequence of byte to mark the end of the bytes. The server socket only processes those bytes (actually, the server only transfers those bytes to another peer). So, only client knows when exactly the socket can be closed. Will I have any trouble if the server doesn´t close any of the connections?

    Thanks!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    What about timeout? That means automatically close them if the client does not respond after some time.
    The trouble if you don't close: Sockets are limited resources, you can't open a new one if all are used.

    If you open hundred of connections within a few seconds, then even a timeout cannot help you to avoid exhausting all resources.
    Alternatively, the client can first send the number of bytes he wants to send to the server. After the server received these number of bytes, he can close the socket. Also the server can ping the client in a regular interval to detect if it is still alive.

    You made the strange assumption that a client cannot close the connection. But it can, and a server can detect that by reading from the input stream and receiveing 0 bytes or an error.

    Comment

    • youtubeprog
      New Member
      • Mar 2014
      • 2

      #3
      Thank you for your reply,

      Maybe I was not very clear. Actually, the client can close the connection, and the client does close the connection. Only the server that does not close the connection. So, there will be some CLOSE_WAIT and FIN_WAIT_2 states for a while. Will I have problems with that?

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        The server reads from the input channel and waits until some bytes arrive. When the client closes the channel, the server should receive a 0 immediately for the number of bytes read. So the server can check for that and also close the connection immediately from its side.
        There can be packages arriving out of order, so a connection will not be closed immediately, but after a timeout. These are internals of the TCP-IP connection and should make no problems for you if the client and server are connected directly.
        I mean I can imagine a situation where the client sends the last data package and then closes the socket and the socket closing arrives first, but the last package was rerouted around the whole world because of a temporary network distortion and arrives a few minutes later. But this is very unlikely (it usually takes a second) and there is a timeout of four minutes for CLOSE_WAIT if I remember correctly and then you should receive an error for the last package not submitted.

        Maybe you should ask this question in the network forum to get better help. I am not specialized in the TCP-IP connection internals and have to read about that myself to be up to date.

        Comment

        Working...