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

Hello all I ask your wisdom to help me on a problem about Socket. I have a peer 2 peer tv application that builts a streaming server on my localhost: my $sock = IO::Socket::INET->new(PeerAddr => 'localhost', PeerPort => '8902', Type => SOCK_STREAM, Proto => 'tcp', Reuse => 1); I want to keep this connection alive also when I change the channel. The streaming remains in the same socket. With the following code both programs crash:
while(1){ while(sysread($sock, $content, 512)){ . . . #some stuff } $sock->close(); $sock = IO::Socket::INET->new (PeerAddr => 'localhost', PeerPo +rt => '8902', Type => SOCK_STREAM, Proto => 'tcp', Reuse => 1, + KeepAlive => 1, ); die "Could not create socket: $!\n" unless $sock; . . . }
I hope I was clear enough. Thanks for any help

Replies are listed 'Best First'.
Re: Socket Keep Alive
by talexb (Chancellor) on Sep 17, 2007 at 17:31 UTC
      I hope I was clear enough.

    I admit I'm a little confused.

    Usually code that exchanges information through pipes has one piece of code that opens a socket, writes and reads, and another piece that opens a socket, reads and writes. What I see in your code fragment is a read, a close and a new, which suggests to me that you're not showing us a complete piece of code that works (or doesn't work), which makes it difficult to determine what the problem might be.

    Can you explain your design approach and give us a better idea of what's not working?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Socket Keep Alive
by almut (Canon) on Sep 17, 2007 at 17:49 UTC

    As talexb said, a bit more background info on how you've set up your streaming server would help.

    As to 'KeepAlive': this is something usually negotiated at a higher level (and AFAIK, not a valid option to IO::Socket::INET). For example, a browser and a webserver can agree on keeping their HTTP connection alive over multiple requests. But as soon as one side closes the socket, it's closed (i.e. no longer 'alive'). And that's exactly what you're doing in the code snippet you posted...

      Hello again, sorry for the first incomprensible question. I will try to make it clearer:

      basically I am running a peer 2 peer tv application that creates a web server on the 'localhost'. To this a media player connects in order to retrieve the streaming. If at the same time I connect my perl application to the 'localhost' I can also retrieve the data. So far so good!

      Now the problem arises when I want to keep a persistant connection to 'localhost' from my perl application in order to 'sniff' the data, even when my p2p app changes channels (meaning opens a new connection on the same port at localhost).

      With the code I showed you what happen is that both applications crash.

      Do you have any idea on how to build the application in order to have a persistant connection allowing also my p2p app to connect to that same port and work properly?

      Hope you can help and thanks for your patience.