Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Closing Sockets with IO::Socket::INET

by lackita (Sexton)
on Aug 05, 2008 at 15:44 UTC ( [id://702401]=perlquestion: print w/replies, xml ) Need Help??

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

Hey monks, I'm using IO::Socket::INET to listen for incoming data, and I want to be able to close this port within my script. After I start up the socket I connect to the port using telnet, but when either the $socket->close() or $socket->shutdown(2) command is tried my telnet session isn't terminated. Could somebody explain how I can definitively close the port I'm using?

Replies are listed 'Best First'.
Re: Closing Sockets with IO::Socket::INET
by Perlbotics (Archbishop) on Aug 05, 2008 at 20:10 UTC
    Lacking some snipped, I just guess.... After accepting a new connection, your program owns two sockets (and handles to those). One socket still listening for new connections and another one that represents the newly established connection (your telnet session). Maybe you just closed the listening socket instead of that of the new connection by confusing the handles?
      That's a good call, the problem was I wasn't closing the filehandle created by accept. Thanks guys.
Re: Closing Sockets with IO::Socket::INET
by pc88mxer (Vicar) on Aug 05, 2008 at 16:05 UTC
    Are you forking to handle the connection? Is it possible that you have the socket opened twice (once in the parent and once in the child)? That would explain why closing the child socket doesn't close the connection.

    For an example of how to write a forking TCP server, have a look at the documentation for Net::TCP::Server.

Re: Closing Sockets with IO::Socket::INET
by Illuminatus (Curate) on Aug 05, 2008 at 16:31 UTC
    You are going to have to provide more information in order to properly diagnose the problem. You say "I'm using IO::Socket::INET to listen for incoming data, and I want to be able to close this port within my script." You do know that this socket cannot be used to initiate a connection. This socket is designed to take inbound requests from other applications. I think you need to provide a code snippet that better typifies your action.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://702401]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found