Server/client over a network

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;
}


}
 
S

Sébastien Cottalorda

deadpickle a écrit :
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.
Hi,

On which side : client or server ?
If it's on the client side, you cannot manage to reach the server socket
through the router.
Try this:
#> telnet router 7890
that will try open a telnet session on the router port 7890
If your router forward this port, your request will go to the server on
which you want to pick the file.
Just enter FIL|namefile
Normaly you'll see something

NOTE: your client socket need to be open with the *router* and not with
the server (port forwarding case).
If you mean routing instead of port forwarding, then the client needs to
point to the *server* adress.
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?

It seems to be network problem.

Sebastien
 
D

deadpickle

Sébastien Cottalorda said:
deadpickle a écrit :
Hi,

On which side : client or server ?

I believe I get it on the server side if I dont assign the value
'localhost' to LocalHost in IO::SOCKET::INET. And I get it on the
client side whenever I connect to an ip address.
If it's on the client side, you cannot manage to reach the server socket
through the router.
Try this:
#> telnet router 7890
that will try open a telnet session on the router port 7890
If your router forward this port, your request will go to the server on
which you want to pick the file.

tried it in terminal and got:
Connecting to router...Could not open a connection to the host, on port
7890: Connect failed
So I guess it did not forward anything.
Just enter FIL|namefile
Normaly you'll see something

Not sure what this is. Is this a line in the script?
NOTE: your client socket need to be open with the *router* and not with
the server (port forwarding case).
If you mean routing instead of port forwarding, then the client needs to
point to the *server* adress.

I mean port forwarding since it is easy to do. how can I open a port
with the "router"?
 
S

Sébastien Cottalorda

deadpickle a écrit :
I believe I get it on the server side if I dont assign the value
'localhost' to LocalHost in IO::SOCKET::INET. And I get it on the
client side whenever I connect to an ip address.

I don't know why you need to enter this parameter on the server side.
When you check client/server mechanisme on the same machine, you never
got that - isn't it ?
tried it in terminal and got:
Connecting to router...Could not open a connection to the host, on port
7890: Connect failed
So I guess it did not forward anything.

You're right !
Not sure what this is. Is this a line in the script?

If you manage to connect, with telnet program, on the server side, you
should, normally receive nothing else than connected....
Still connected, type then, FIL|filename with your telnet client => then
you'll receive something, which means that you're connected to the
server program.
I mean port forwarding since it is easy to do. how can I open a port
with the "router"?
I don't know, it depends on your router itself.
Normally your router has 2 interfaces.
On the client side interface, make the 7890 port forward frames to
server IP on port 7890.
That's it.

Hope this helps.

Sebastien
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top