TCP client : TCP « Network « Perl






TCP client

    

#!/usr/bin/perl 

use warnings;
use strict;

use IO::Socket;

my $host = 'localhost'; 
my $port = 4444;

my $server = new IO::Socket::INET(
    Proto => 'tcp',
    PeerAddr => $host,
    PeerPort => $port,
);
die "Connect failed: $!\n" unless $server;

print "Client connected.\n";
print "Server says: ", scalar(<$server>);
print $server "Hello from the client!\n";
print "Server says: ", scalar(<$server>);
print $server "And goodbye!\n";
print "Server says: ", scalar(<$server>);
close $server;

   
    
    
    
  








Related examples in the same category

1.TCP client using Socket module.
2.TCP inet server
3.TCP server
4.tcp inet client
5.Sample TCP client without the Socket module.
6.Sample TCP client.
7.Simple TCP Clients
8.Example of a Perl TCP server using Socket module.