Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple socket client-server interaction works on C, but the analogouscode does not work with Perl

Reply
Thread Tools

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

 
 
Mark_Galeck
Guest
Posts: n/a
 
      08-16-2009
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);
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-16-2009
Mark_Galeck wrote:
> [...]
> 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.htm...ust-I-do-this?

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

binmode(SERVER, ":unix");

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-16-2009
On 2009-08-16 14:50, Mark_Galeck <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Mark_Galeck
Guest
Posts: n/a
 
      08-16-2009
thank you Peter and Alexander, this makes perfect sense
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<img onload= does not work in FireFox 1.0 but works in IE Luke Venediger Javascript 3 03-08-2005 08:15 PM
does not work on freeBSD but works on linux, and windows John fabiani Python 14 07-02-2004 04:23 PM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
Why this javascript function does not work on IE5, but works on IE6? Andrzej Javascript 0 10-06-2003 04:21 PM
newbie question - simple java socket example - it works but how do i . . . stan k. Java 3 09-27-2003 01:15 PM



Advertisments