#!/usr/bin/perl -w use strict; use warnings; use IO::Socket::INET; my $sock = IO::Socket::INET->new( LocalPort => 8000, Listen => 10, Reuse => 1 ); die "Cannot create socket $!\n" unless $sock; print "Server waiting for client to connect on port 8000\n"; $| = 1; # autoflush my $client = $sock->accept(); my $handle; my $clientHost = "10.20.0.30"; my $clientPort = "8000"; while(){ while ($client) { # receive data, pass data via variable to dedicated subroutine # based on pattern matching my $recv_msg = &recv(); # handles recv() activity if ($recv_msg =~ /ADD/) { ADD( $recv_msg ); # shutdown ($client, 2); } elsif ( $recv_msg =~ /STK/ ) { STK( $recv_msg ); # shutdown ($client, 2); } } } # $sock->close(); sub connection { my ($host, $port) = @_; $handle = IO::Socket::INET->new ( PeerHost => $host, PeerPort => $port, Proto => "tcp") or $handle = 0; if ($handle != 0) { $handle->autoflush(1); # output gets there right away } else { print "Can't connect to host from sub connection."; } return $handle; } sub ADD { my ( @data ) = @_; sendRequest($data[0]); print "Data after socket->send: $data[0]\n"; print "This message is an ADD request.\n"; $handle->close(); $sock->close(); } sub STK { my ( @data ) = @_; sendRequest($data[0]); print "Data after socket->send: $data[0]\n"; print "This message is a STK request.\n"; $handle->close(); $sock->close(); } sub recv { my $msg = ""; $client->recv($msg, 12000); return $msg; } sub sendRequest { my ($reqSock, $msg) = @_; &sender($reqSock, $msg); } sub sender { my ($reqMsg) = @_; $handle = connection($clientHost, $clientPort); eval { $handle->send($reqMsg); }; print 'Crash: '.$@ if $@; if ($@) { print "Connection Reset"; $handle->close; # $handle = connection($clientHost, $clientPort); eval { $handle->send($reqMsg); }; } }