Server-Clienrt Connect over Internet

D

deadpickle

Server
========================================================================
use strict;
use IO::Socket;
my $port = '2000';
my $sock = new IO::Socket::INET(
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
$|=1;
my($new_sock, $c_addr, $buf);
while (($new_sock, $c_addr) = $sock->accept()) {
my ($client_port, $c_ip) = sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host = gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host","
[$client_ipnum]\n";
while (defined ($buf = <$new_sock>)) {
chomp($buf);
if ($buf=~/^FILE\|/){
my $filename = (split /\|/, $buf)[1];
unless (open (FH, "<", $filename)){
print $new_sock "NACK|File '$filename'
do not exists\n";
}
else {
while (my $line =<FH>){
chomp($line);
print $new_sock
"FIL|".$line."\n";
#
# Remove/comment next line in
production
#
print "SENT>FIL|".$line."\n";
}
close(FH);
print $new_sock "EOT|\n";
}
last;
}
elsif ($buf=~/^QUIT\|/){
last;
}
else {
print $new_sock "NACK|Command not
understood\n";
last;
}
}
close($new_sock);
}

exit;


Client
========================================================================
for (;;) {
use strict;
use IO::Socket;
$|=1;
my $host = shift || '192.168.1.100';
my $port = shift || '2000';
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
#--------------/ Socket opened \----------------

my $filename = 'K:\School\Meteorology\UAV\uavposition.txt'; # the file
to be sent by the server side
my $fileout = 'C:\uavposition.txt'; # the file in which we should place
received datas
unless (open (FH_RECEPTION, ">", $fileout)){
die "Unable to create $fileout $!";
}

print $sock "FILE|$filename\n";
my @elem=();
my $all_received;
while (my $line=<$sock>){
chomp($line);
#
# Remove the next line in production
#
print "RECEIVED>$line\n";
@elem = split /\|/, $line;
if ($line=~/^FIL\|/){
$elem[1]='' unless (defined $elem[1]);
print FH_RECEPTION $elem[1]."\n";
$all_received=0;
}
elsif ($line=~/^EOT\|/){
print "Normal End of Transmission received\n";
$all_received=1;
last;
}
elsif ($line=~/^NACK\|/){
print "ERROR: $elem[1]\n";
last;
}
else {
print "Do not understand|$line\n";
last;
}
}
close($sock);
close(FH_RECEPTION);
if ((defined $all_received) and ($all_received==1)){
print "Everything has been received in $fileout\n";
}

elsif (defined $all_received) {
print "some part of the file has been received in $fileout\n";
}
sleep (5);
}
exit;



I want the client to be able to connect through a firewall and
over the internet to the server program. My question is, will these
program do this? Are they written only for local network connection?
Will they successfully connect through a firewall with a router that
has port forwarding?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top