You need to print a "control line feed" after each send and receive, also, turn auto-flush on (error on my behalf - it's okay to do this, but not necessary - see Update at bottom):
Next, every time you send and receive (via print), use that CRLF constant:use IO::Socket qw(crlf); # now you can use CRLF constant $|++; # non-buffered
Last, don't use defined in your server wait loop, and don't forget to ask the server for the answer after the client has asked the question. Here is the whole deal for clarity (i did remove $|++ to prevent cargo-culting):# in server: print $new_sock $_, CRLF; # in client: print $sock "Hello there", CRLF;
# server use strict; use IO::Socket qw(:crlf); my $sock = new IO::Socket::INET ( LocalAddr => 'localhost', LocalPort => 10000, Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $new_sock $_, CRLF; } close($sock); #------------------------------ # client use strict; use IO::Socket qw(:crlf); my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => 10000, Proto => 'tcp', ); die "could not create socket\n" unless $sock; print $sock "Hello there", CRLF; print $sock->getline(); close($sock);
Oh yeah - go ye forth and get Network Programming with Perl.
Update!
runrig reminded me that $|++ flushes STDOUT by default, not the socket (silly /me), so that should
be $socket->autoflush(),
but, as of IO::Socket version 1.18 autoflush is on by
default. Sorry about that. runrig++
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle)
In reply to (jeffa) Re: Using IO::Socket
by jeffa
in thread Using IO::Socket
by readey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |