sergey has asked for the wisdom of the Perl Monks concerning the following question:

I'm pretty new to Perl network programming, so I'm sure I may have a bunch of stuff wrong. This is just a simple client/server communication, but the server doesn't seem to receive the messages until the client closes the connection (or quits). I expected it to get the data and process it line by line.

Client code:
$proto = getprotobyname("tcp"); $iaddr = inet_aton($host) or die "Invalid host : $!\n"; $paddr = sockaddr_in($port,$iaddr); socket(SOCKET,PF_INET,SOCK_STREAM,$proto) or die "socket: $!\n"; connect(SOCKET,$paddr) or die "connect: $!\n"; print "connected\n"; print SOCKET "HELLO\n"; print SOCKET "END\n"; print "finished sending\n"; while (chomp($line = <SOCKET>)) { print $line; if ($line eq "END") { print SOCKET "received $line\n"; print SOCKET "END"; } } close SOCKET or die "close: $!";


Server Code:
$protocol = getprotobyname("tcp"); socket(SOCK,PF_INET,SOCK_STREAM,$protocol) or die "socket: $!\n"; setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1) or die "setsockopt: $!\n"; $paddr = sockaddr_in($port, INADDR_ANY); bind(SOCK, $paddr) or die "bind: $!\n"; listen(SOCK,SOMAXCONN) or die "listen: $!\n"; print "Server started on port $port.\n"; while ($client_addr = accept(CLIENT,SOCK)) { my ($client_port,$client_ip) = sockaddr_in($client_addr); $client_ipnum = inet_ntoa($client_ip); $client_host = gethostbyaddr($client_ip,AF_INET); print "$client_host connected from ip $client_ipnum\n"; readagain: while (chomp($line = <CLIENT>)) { print "From client: $line\n"; if ($line eq "END") { goto inputfromuser; } } inputfromuser: print "Input > "; chomp($input = <>); print CLIENT "$input\n"; goto readagain; print CLIENT "Test message :)\n"; #print CLIENT "\0"; print "Out of loop\n"; } print "Closing connection.\n"; close CLIENT;
  • Comment on Client/server issues - Server is not receiving information until client closes the connection
  • Select or Download Code

Replies are listed 'Best First'.
Re: Client/server issues - Server is not receiving information until client closes the connection
by ikegami (Patriarch) on Apr 21, 2008 at 18:46 UTC
    You can simply your code greatly by using IO::Socket::INET. For example,
    use Socket qw( PF_INET SOCK_STREAM inet_aton ); $proto = getprotobyname("tcp"); $iaddr = inet_aton($host) or die "Invalid host : $!\n"; $paddr = sockaddr_in($port,$iaddr); socket(SOCKET,PF_INET,SOCK_STREAM,$proto) or die "socket: $!\n"; connect(SOCKET,$paddr) or die "connect: $!\n"; my $old_fh = select(SOCKET); $|=1; select($old_fh);

    can be written as:

    use IO::Socket::INET qw( ); my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $host, PeerPort => $port, ) or die("Unable to create client socket: $!\n");

    The server would be

    use IO::Socket::INET qw( ); use Socket qw( SOMAXCONN ); my $server_sock = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, ReuseAddr => 1, Listen => SOMAXCONN, ) or die("Unable to create server socket: $!\n");

    As an additional bonus, IO::Socket::INET turns on autoflush for the socket, so if kyle is correct, it will also fix your problem.

Re: Client/server issues
by kyle (Abbot) on Apr 21, 2008 at 18:16 UTC

    What are the "issues"? You've posted code and said there may be a problem with it, but you haven't said what you want it to do or what it actually does. How does its behavior deviate from the desired behavior? Perhaps have a look at How do I post a question effectively?

    Update after the OP's update: My guess is you're suffering from buffering.

      I'm sorry about that, I'll change it.
      Thank you so much. That fixed it ^_^
Re: Client/server issues - Server is not receiving information until client closes the connection
by Lazarus (Novice) on Apr 23, 2008 at 15:41 UTC
    For really simple Client/Server communication, you may want to try XMLRPC as implemented by the Frontier::Daemon and Frontier::Client CPAN libraries.
    http://search.cpan.org/~kmacleod/Frontier-RPC-0.07b4/lib/Frontier/Daemon.pm
    http://search.cpan.org/~kmacleod/Frontier-RPC-0.07b4/lib/Frontier/Client.pm
    If you check out the description, the sample code provided looks even simpler than your own. Works for me...