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;

In reply to Client/server issues - Server is not receiving information until client closes the connection by sergey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.