Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Server/client over a network

Reply
Thread Tools

Server/client over a network

 
 
deadpickle
Guest
Posts: n/a
 
      09-19-2006
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;
}


}

 
Reply With Quote
 
 
 
 
Sébastien Cottalorda
Guest
Posts: n/a
 
      09-19-2006
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
 
Reply With Quote
 
 
 
 
deadpickle
Guest
Posts: n/a
 
      09-20-2006
Sébastien Cottalorda wrote:
> 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 ?


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"?

 
Reply With Quote
 
Sébastien Cottalorda
Guest
Posts: n/a
 
      09-20-2006
deadpickle a écrit :
> Sébastien Cottalorda wrote:
>
>>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 ?

>
>
> 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 ?

>>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.


You're right !

>
>>Just enter FIL|namefile
>>Normaly you'll see something

>
> 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.

>
>>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"?
>

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
RF signal over Cat5e network - ie TV over network links Hank NZ Computing 21 11-18-2006 04:58 AM
Network Service account over network Jane Davis ASP .Net 1 06-22-2005 11:57 AM
RMI only works over network and not over internet zach.bastick@gmail.com Java 7 02-23-2005 10:52 PM
Web Crystal Report repite the same line over an over in the webserver Cesar Zam ASP .Net 0 11-06-2003 09:30 PM



Advertisments