sergey has asked for the wisdom of the Perl Monks concerning the following question:
$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: $!";
$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;
|
|---|
| 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 | |
|
Re: Client/server issues
by kyle (Abbot) on Apr 21, 2008 at 18:16 UTC | |
by sergey (Novice) on Apr 21, 2008 at 18:24 UTC | |
by sergey (Novice) on Apr 21, 2008 at 18:48 UTC | |
|
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 |