I generally use Socket instead of the higher-level IO::Socket, but that's just my personaly preference. Here's some code to get you started. First the server:
use strict;
use Socket;
my $socket; # the socket waiting for incoming connections
my $incoming; # an incoming connection
my $port = 4000;
my $ipaddr = "localhost";
my $numConnections = 1;
socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
|| die "Could not create socket: $!\n";
bind($socket, sockaddr_in($port, inet_aton($ipaddr)))
|| die "Could not bind socket: $!\n";
listen($socket, $numConnections)
|| die "Could not listen on socket: $!\n";
accept($incoming, $socket)
|| die "Could not accept connection on socket: $!\n";
my $message;
my $messageLen = 999999; # the maximum number of bytes to read off th
+e socket
defined(recv($incoming, $message, $messageLen, 0)) || die "Receive err
+or: $!";
print "I got the following message: $message.\n";
defined(send($incoming, "2", 0)) || die "Send error: $!";
shutdown($incoming, 2);
shutdown($socket, 2);
and now the client.
use strict;
use Socket;
my $socket;
my $port = 4000;
my $ipaddr = "localhost";
socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
|| die "Could not create socket: $!\n";
connect($socket, sockaddr_in($port, inet_aton($ipaddr)))
|| die "Could not connect: $!\n";
my $message;
my $messageLen = 999999; # the maximum number of bytes to read off the
+ socket
defined(send($socket, "1", 0)) || die "Send error: $!";
defined(recv($socket, $message, $messageLen, 0)) or die "Receive error
+: $!";
print "I got the following message: $message.\n";
shutdown($socket, 2);
Good luck!
-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man... |