Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got a script that accept client connection and forks them off to different processes. I've got the client connect as an IO::Socket object; when I'm done with $client, how do I disconnect it without disabling the entire socket (and disconnecting all the other clients) ?
  • Comment on Disconnect client without closing socket?

Replies are listed 'Best First'.
Re: Disconnect client without closing socket?
by pfaut (Priest) on Dec 15, 2002 at 21:08 UTC

    accept() returns a new socket that is connected with the remote client. You still have your original socket that you were listening on. When you are done with this client, close his socket. The rest of your sockets will remain intact.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      How do you close the socket? I'm trying to $client->shutdown(2); but the client is still staying connected.

        According to perldoc IO::Socket, IO::Socket is derived from IO::Handle. IO::Handle provides a close() method.

        --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Disconnect client without closing socket?
by batkins (Chaplain) on Dec 15, 2002 at 21:16 UTC
    just exit; in the child process.
      This is not right. Yes, after you exit, the socket will be closed eventually, but there is a delay. Some time it takes couple of muinutes for the local port to be freed up, and before that that local port is no longer available to others. You have to explicitly close your socket, to free the local port up NOW, RIGHT NOW.

      Update: Although from pure programming view, Reuse would work, but it is obviously not a good practice, and that's not the purpose of Reuse. You should always close your socket before exit. Don't program in a quick and dirty way, always do it in the right way with the best style.

        Enable the Reuse option when creating the socket.