[Chicago-talk] PLESE HELP. TCP server.

Richard Reina richard at rushlogistics.com
Tue Jul 19 09:09:11 PDT 2005


> Can you post any of the code for TCP_server.pl?

Sorry it took me so long.

#!/usr/bin/perl -w
use Socket;
$| = 1;
socket(SERVER, PF_INET, SOCK_STREAM,
getprotobyname('tcp'));

my$server_port = 8045;
my$my_addr = sockaddr_in($server_port, INADDR_ANY);
bind(SERVER, $my_addr) or die "Couldn't bind to port
$server_port : $!\n";

# establish a queue for incoming connections
listen(SERVER, SOMAXCONN) or
     die "Couldn't listen on port $server_port :
$!\n";

my $client_address;
   
   my ($ph_no) = @_;

while ($client_address = accept(CLIENT, SERVER)) {

   my ($port, $packed_ip) =
sockaddr_in($client_address);
   print "port: " . $port . "Packed IP: " . $packed_ip
. "\n";
   my $dotted_quad = inet_ntoa($packed_ip);
   print "Dotted Quad: " . $dotted_quad . "\n";

   my $data_to_send = "Welcome to Call Processing";
   my$flags = 0;

   defined (send(CLIENT, $data_to_send, $flags)) or
die "Can't send : $!\n";
   open( my $in, "/home/richard/test_TCP_send |" ) ||
die $!; 
   while( <$in> ) {
     defined (send(CLIENT, $_, $flags)) or die "Can't
send : $!\n";
    }

}

close (SERVER);



ON THE CLIENT MACHINE

#!/usr/bin/perl

use Socket;

socket(TO_SERVER, PF_INET, SOCK_STREAM,
getprotobyname('tcp'));
$| = 1;
my $remote_port = 8045;
my $remote_host;  # not worried about it cause we know
the IP
my $intranet_addr = 192.168.0.14;

my $paddr = sockaddr_in($remote_port, $intranet_addr);

connect(TO_SERVER, $paddr) or
   die "Could not connect to $remote_host:$remote_port
: $!\n";
print "CONNECTED TO SERVER\n";
$| = 1;
while(<TO_SERVER>) {

   recv (TO_SERVER, $data_read, 45, 0) or die "Can't
receive : $!\n";
   print $data_read . "\n";

}
close (TO_SERVER)


A people that values its privileges above its principles soon loses both.
 -Dwight D. Eisenhower.


More information about the Chicago-talk mailing list