simple socket client-server interaction works on C, but the analogouscode does not work with Perl

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);
 
A

Alexander Bartolich

Mark_Galeck said:
[...]
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

You are using buffered I/O without explicit flush.

http://perldoc.perl.org/perlfaq5.ht...er-an-output-filehandle?--Why-must-I-do-this?

The simplies fix of your program (measured in lines of code) is
this line:

binmode(SERVER, ":unix");
 
P

Peter J. Holzer

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
[...]
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";

At this point, the file handle SERVER is buffered, so ...
print SERVER "foo\n";

This will *not* send "foo\n" to the server, it will only put it into the
buffer.

$_ = <SERVER>; #only if I comment this line out, does the server get
the above message!!

Now you wait for a message from the server. But you haven't sent a
message to the server yet, so the server is waiting for the client, too.

This is called a deadlock.

print "read from server:\n";

print ;


close(SERVER);

The close would flush the buffer (and send everything in it to the
server), but that line is never reached.

See the question "How do I flush/unbuffer an output filehandle? Why
must I do this?" in the FAQ for a solution.

Or just use IO::Socket objects instead of plain file handles. They have
autoflush turned on by default (the FAQ is out-of-date in this respect).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top