M
Mark_Galeck
Hello,
here is a simplest possible (I think), socket client-server code in
Perl. You start the server first, it listens, client starts, sends
the server a message, server replies back, that's it. The exact
analogous code to this, written in C, works as expected, but here in
Perl, not (both run under the latest Ubuntu). So I think, I
understand how the sockets work, I just don't understand how Perl
works.
Under Perl, the server just prints "accepted", but never gets any
messages from the client. Curiously, if I comment out the client
reading from server after sending, then everything else is fine, the
server gets the message and both exit.
What is going on?? Thank you for your insights! Mark
serv.pl:
use Socket;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
bind(SERVER, sockaddr_in(3000, inet_aton("127.0.0.1")));
listen(SERVER, 1);
accept(CLIENT, SERVER);
print "accepted\n";
$_ = <CLIENT>;
print "read from client:\n";
print ;
print CLIENT "foobar\n";
close(CLIENT);
close(SERVER);
client.pl:
use Socket;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
connect(SERVER, sockaddr_in(3000, inet_aton("127.0.0.1")))
or die "WHOA! could not connect";
print SERVER "foo\n";
$_ = <SERVER>; #only if I comment this line out, does the server get
the above message!!
print "read from server:\n";
print ;
close(SERVER);
here is a simplest possible (I think), socket client-server code in
Perl. You start the server first, it listens, client starts, sends
the server a message, server replies back, that's it. The exact
analogous code to this, written in C, works as expected, but here in
Perl, not (both run under the latest Ubuntu). So I think, I
understand how the sockets work, I just don't understand how Perl
works.
Under Perl, the server just prints "accepted", but never gets any
messages from the client. Curiously, if I comment out the client
reading from server after sending, then everything else is fine, the
server gets the message and both exit.
What is going on?? Thank you for your insights! Mark
serv.pl:
use Socket;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
bind(SERVER, sockaddr_in(3000, inet_aton("127.0.0.1")));
listen(SERVER, 1);
accept(CLIENT, SERVER);
print "accepted\n";
$_ = <CLIENT>;
print "read from client:\n";
print ;
print CLIENT "foobar\n";
close(CLIENT);
close(SERVER);
client.pl:
use Socket;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
connect(SERVER, sockaddr_in(3000, inet_aton("127.0.0.1")))
or die "WHOA! could not connect";
print SERVER "foo\n";
$_ = <SERVER>; #only if I comment this line out, does the server get
the above message!!
print "read from server:\n";
print ;
close(SERVER);