>>>>> "CH" == Chap Harrison <c...@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=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  ------  u...@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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to