http://www.perlmonks.org?node_id=509017


in reply to Problems reading back from socket

A basic client and server, socket implementation(untested).

Server:
use strict; use IO::Socket; my $sock = new IO::Socket::INET (LocalHost => 'localhost', LocalPort => 5004, Proto => 'tcp', Listen => 5, Reuse => 1 + ); die "Socket could not be created. Reason: $!" unless $sock; my ($new_sock, $buf); while ($new_sock = $sock->accept()) { print "Connection from ", $new_sock->peerhost, ": on port ", $new_s +ock->peerport,"\n"; print $new_sock "Hello from the server"; while (defined ($buf = <$new_sock>)) { print $buf; } } close ($sock);
Client:
use strict; use IO::Socket; my $line; my $socket = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '5004', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $socket; $socket->recv($line, 80); print "$line"; print $socket "Hello from the Client!\n"; close($socket);


Cheers,
Rupesh.