Server/client over a network

Discussion in 'Perl Misc' started by deadpickle, Sep 19, 2006.

  1. deadpickle

    deadpickle Guest

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


    }
     
    deadpickle, Sep 19, 2006
    #1
    1. Advertising

  2. 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
     
    Sébastien Cottalorda, Sep 19, 2006
    #2
    1. Advertising

  3. deadpickle

    deadpickle Guest

    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"?
     
    deadpickle, Sep 20, 2006
    #3
  4. 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
     
    Sébastien Cottalorda, Sep 20, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Wilson
    Replies:
    7
    Views:
    5,206
    Paul Wilson
    Jun 21, 2004
  2. Jane Davis

    Network Service account over network

    Jane Davis, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    454
    Kevin Spencer
    Jun 22, 2005
  3. Replies:
    7
    Views:
    6,733
    Alex Molochnikov
    Feb 23, 2005
  4. -
    Replies:
    2
    Views:
    407
    Alan Krueger
    Jul 29, 2005
  5. Saravanan Sundaramoorthy
    Replies:
    1
    Views:
    132
    Zoran Regvart
    Oct 13, 2010
Loading...

Share This Page