FAQ
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;
$| = 1; # force a fflush after every print
my $servername = 'localhost';
my $port = 8888;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($servername);
my $paddr = 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=14 MAXWORDS=200\n";
#
# Now read the server's reply
#
while ($line = <SOCKET>) {
print "$line";
}
close SOCKET or die "close: $!";
---

(Omitting server code for brevity -- hopefully, my error is evident here)
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.

Any suggestions?

Thanks,
Chap

Search Discussions

  • Uri Guttman at Sep 2, 2010 at 8:52 pm
    "CH" == Chap Harrison 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=200\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@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Chap Harrison at Sep 2, 2010 at 9:21 pm

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 2, '10 at 8:33p
activeSep 2, '10 at 9:21p
posts3
users2
websiteperl.org

2 users in discussion

Chap Harrison: 2 posts Uri Guttman: 1 post

People

Translate

site design / logo © 2021 Grokbase