D
deadpickle
what I want to do is have a server on one machine and a client on the
other. I want the client to connect to the server through a tcp network
(over the internet). I have tried this and I constantly get the no
socket error. The program works on the local machine just fine. Also I
am going trough a router but I did forward the ports. Is there
something I need to do to the code? Am I entering the correct values?
Client Side:
for (; {
use strict;
use IO::Socket;
my $file =
my $host = shift || 'localhost'; #Enter server IP Address
my $port = shift || 7890; #Enter server port
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
#--------------/ Socket opened \----------------
$filename=......; # the file to be sent by the server side
$fileout=.....; # 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);
@elem = split /\|/, $line;
if ($elem[0]=~/^FIL\|/){
print FH_RECEPTION $elem[1]."\n";
$all_received=0;
}
elsif ($elem[0]=~/^EOT\|/){
print "Normal End of Transmission received\n";
$all_received=1;
last;
}
elsif ($elem[0]=~/^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
};
Server Side:
use strict;
use IO::Socket;
my $sock = new IO::Socket::INET(
LocalHost => 'localhost', #server's IP id
LocalPort => 7890, #port to bind socket to
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
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>)) {
print $buf;
}
}
other. I want the client to connect to the server through a tcp network
(over the internet). I have tried this and I constantly get the no
socket error. The program works on the local machine just fine. Also I
am going trough a router but I did forward the ports. Is there
something I need to do to the code? Am I entering the correct values?
Client Side:
for (; {
use strict;
use IO::Socket;
my $file =
my $host = shift || 'localhost'; #Enter server IP Address
my $port = shift || 7890; #Enter server port
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
#--------------/ Socket opened \----------------
$filename=......; # the file to be sent by the server side
$fileout=.....; # 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);
@elem = split /\|/, $line;
if ($elem[0]=~/^FIL\|/){
print FH_RECEPTION $elem[1]."\n";
$all_received=0;
}
elsif ($elem[0]=~/^EOT\|/){
print "Normal End of Transmission received\n";
$all_received=1;
last;
}
elsif ($elem[0]=~/^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
};
Server Side:
use strict;
use IO::Socket;
my $sock = new IO::Socket::INET(
LocalHost => 'localhost', #server's IP id
LocalPort => 7890, #port to bind socket to
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
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>)) {
print $buf;
}
}