in reply to Two-Part Socket Question.
"Also when I try to recv() data from the socket when none is available it locks up the application"
I noticed that you don't use IO::Select in your client program. Probably you thought IO::Select is only for multiplexing, thus there is no need to use it in your client program, but that is a misunderstanding.
If you don't want to block on recv() even when nothing to recv(), then better use IO::Select, and check can_read first.
Try with the attached scripts, and observe the results:
Server:
use IO::Select; use IO::Socket::INET; use warnings; use strict; my $s = IO::Socket::INET->new(Proto => "tcp", LocalAddr => "localhost" +, LocalPort => 1234, Listen => 10); print "waiting...\n"; my $c = $s->accept(); print "connected\n"; while (1) { $c->send("abc"); sleep(3); }
A client that does not block
use IO::Select; use IO::Socket::INET; use warnings; use strict; print "started\n"; my $c = IO::Socket::INET->new(Proto => "tcp", PeerAddr => "localhost", + PeerPort => 1234); print "connected\n"; my $sel = IO::Select->new($c); my $msg; my @r; while (1) { if (@r = $sel->can_read(0)) { $r[0]->recv($msg, 1024); print time() . " $msg" . "\n" } else { print time() . " one loop\n"; } sleep(1); }
A client blocks
use IO::Socket::INET; use warnings; use strict; print "started\n"; my $c = IO::Socket::INET->new(Proto => "tcp", PeerAddr => "localhost", + PeerPort => 1234); print "connected\n"; my $msg; while (1) { $c->recv($msg, 1024); print time() . " one loop\n"; }
|
|---|