Flip-flop conversations using Socket

Following the guidelines I found in numerous examples on the web, I =
wrote a simple Client-Server app in Perl. However, it appears that the =
send buffer does not get flushed until the connection is closed. That's =
fine for one-way data transfer, but I need the server to be able to =
receive some request parameters and then respond to them.

Code fragment illustrating how I'm sending lines of data:

---
use Socket;
$| =3D 1; # force a fflush after every print
my $servername =3D 'localhost';
my $port =3D 8888;
my $proto =3D getprotobyname('tcp');
my $iaddr =3D inet_aton($servername);
my $paddr =3D sockaddr_in($port, $iaddr);
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";
my $line;
# =
=

# Start off by sending the server a message, terminated by new-line
# =
=

print SOCKET "MAXLENGTH=3D14 MAXWORDS=3D200\n";
#
# Now read the server's reply
# =

while ($line =3D <SOCKET>) {
print "$line";
}
close SOCKET or die "close: $!";
---

(Omitting server code for brevity -- hopefully, my error is evident =
here)
This deadlocks waiting on $line =3D <SOCKET>. Using the debugger on the =
server, I can see that the connection is accepted, but the client's =
initial message is never received.

Any suggestions?

Thanks,
Chap=

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Chap Harrison [ Do, 02 September 2010 22:33 ] [ ID #2047098 ]

Re: Flip-flop conversations using Socket

>>>>> "CH" == Chap Harrison <clh [at] pobox.com> writes:

CH> Following the guidelines I found in numerous examples on the web, I wrote a simple Client-Server app in Perl. However, it appears that the send buffer does not get flushed until the connection is closed. That's fine for one-way data transfer, but I need the server to be able to receive some request parameters and then respond to them.


CH> ---
CH> use Socket;

don't use that. use IO::Socket which is higher level and much easier to use.

CH> $| = 1; # force a fflush after every print

that only sets it for the current handle which is STDOUT
CH> my $servername = 'localhost';
CH> my $port = 8888;
CH> my $proto = getprotobyname('tcp');
CH> my $iaddr = inet_aton($servername);
CH> my $paddr = sockaddr_in($port, $iaddr);
CH> socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
CH> or die "socket: $!";
CH> connect(SOCKET, $paddr) or die "connect: $!";

all of that could be done with:

my $sock = IO::Socket::INET->new( "$servername:$port" ) ;

one short call instead of 7 lines of many calls.

CH> my $line;
CH> #
CH> # Start off by sending the server a message, terminated by new-line
CH> #
CH> print SOCKET "MAXLENGTH=14 MAXWORDS 0\n";

first rule of sockets, use syswrite instead of print as that will not
need flushing. if you set the autoflush flag on the handle (IO::Socket
has such a method) then you can use print.

CH> #
CH> # Now read the server's reply
CH> #
CH> while ($line = <SOCKET>) {
CH> print "$line";
CH> }
CH> close SOCKET or die "close: $!";
CH> ---

CH> (Omitting server code for brevity -- hopefully, my error is evident here)
CH> This deadlocks waiting on $line = <SOCKET>. Using the debugger on the server, I can see that the connection is accepted, but the client's initial message is never received.

it is likely because the print went to the io buffer and was never
flushed to the socket. either enable flushing (with the method) or use
syswrite.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 02 September 2010 22:51 ] [ ID #2047099 ]

Re: Flip-flop conversations using Socket

On Sep 2, 2010, at 3:51 PM, Uri Guttman wrote:

>
> don't use that. use IO::Socket which is higher level and much easier =
to use.
>

It sure is! Thank you.


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Chap Harrison [ Do, 02 September 2010 23:21 ] [ ID #2047102 ]
Perl » gmane.comp.lang.perl.beginners » Flip-flop conversations using Socket

Vorheriges Thema: floating point precision
Nächstes Thema: Recursively filter an array