Recently I had a need to test network communication between two different services over a specific port for a clustered application. Since I didn’t want to have to initiate an application failover just to test the network communication, I decided to use a simple Perl script to listen for inbound communication on the cluster node being tested from the development environment.
What the code does is to open a specific port for listening. I used the basic telnet client to send traffic from the source machine (client) to the destination machine (server).
Here are the code listings for the script for both Solaris 10 and AIX 6.1.
Solaris 10
#!/usr/bin/perl -w use strict; use warnings; use IO::Socket; # Local host bind address (hostname/ipaddr) my $LOCALADDR = 10.0.0.1 # Local host bind port my $PORT = 10240; my $sock = new IO::Socket::INET ( LocalHost => $LOCALADDR, LocalPort => $PORT, Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while() { print $_; } close($sock);
AIX 6.1
#!/usr/bin/perl -w use strict; use warnings; use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr # Local host bind address (hostname/ipaddr) my $LOCALADDR = 10.0.0.1 # Local host bind port my $PORT = 10240; my $server = IO::Socket::INET->new( Proto => 'tcp', LocalHost => $LOCALADDR, LocalPort => $PORT, Listen => 1, Reuse => 1 ) or die "can't setup server"; print "SERVER Waiting for client connection on port $PORT\n"; my $new_sock = $server->accept(); while() { print $_; } close($server);