Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Client Server

by TheVend (Novice)
on Jul 09, 2014 at 18:54 UTC ( [id://1092959]=perlquestion: print w/replies, xml ) Need Help??

TheVend has asked for the wisdom of the Perl Monks concerning the following question:

I need a very basic server client that does the following.

Server: Echo's back to client the clients IP address. Logs the clients IP address.

Client: Attempts to connect to server and if successful displays the servers echoed IP.

I have some “borrowed” server code but can’t seem to get it to echo the connected clients IP

SERVER

use strict; use Socket; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); my $server = "localhost"; # Host IP running the server # create a socket, make it reusable socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "Can't open socket $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Can't set socket option to SO_REUSEADDR $!\n"; # bind to a port, then listen bind( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can't bind to port $port! \n"; listen(SOCKET, 5) or die "listen: $!"; print "SERVER started on port $port\n"; # accepting a connection my $client_addr; while ($client_addr = accept(NEW_SOCKET, SOCKET)) { # send them a message, close connection my $name = gethostbyaddr($client_addr, AF_INET ); print NEW_SOCKET "Smile from the server"; print "Connection recieved from $name\n"; close NEW_SOCKET; }

CLIENT

use strict; use Socket; # initialize host and port my $host = shift || 'localhost'; my $port = shift || 7890; my $server = "localhost"; # Host IP running the server # create the socket, connect to the port socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]) or die "Can't create a socket $!\n"; connect( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can't connect to port $port! \n"; my $line; while ($line = <SOCKET>) { print "$line\n"; } close SOCKET or die "close: $!";

Replies are listed 'Best First'.
Re: Client Server
by AppleFritter (Vicar) on Jul 09, 2014 at 21:06 UTC

    You can turn the $client_addr received from accept into a human-readable IP address using unpack_sockaddr_in and inet_ntoa (and then echo that back to the client, as sn1987a described in Re: Client Server). That said, Socket is a fairly low-level module; are you sure you don't want something else instead, say IO::Socket?

    For instance, here's the server:

    #!/usr/bin/perl use warnings; use strict; use IO::Socket; use Socket; # use port 7890 as default my $port = shift || 7890; my $socket = IO::Socket::INET->new( LocalPort => $port, Type => SOCK_STREAM, Reuse => 1, Listen => 10, ) or die "Couldn't create server: $@\n"; while (my $client = $socket->accept()) { my ($port, $iaddr) = unpack_sockaddr_in($client->peername()); $client->send(inet_ntoa($iaddr)); $client->close(); }

    And here's the client:

    #!/usr/bin/perl use feature qw/say/; use warnings; use strict; use IO::Socket; # initialize host and port my $port = shift || 7890; my $server = "localhost"; # Host IP running the server my $socket = IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port, Proto => "tcp", Type => SOCK_STREAM ) or die "Can't connect to server: $@\n"; while(<$socket>) { say; } close $socket or die "Can't close socket: $!";

    Credit where credit is due: all this is pretty much directly copied from The Perl Cookbook, specifically Recipes 17.1 ("Writing a TCP Client"), 17.2 ("Writing a TCP Server") and 17.7 ("Identifying the Other End of a Socket". I really recommend that book, it's very handy.

      Yep, that did it. Thank you!
      Im trying to write the value of the connected IP to the server drive. I have the file handle set but how to I quantify the IP as a var to pass it to the file handle?

        I'm not quite sure what you mean. Could you clarify?

        Suppose that you've got the IP address, as returned by inet_ntoa, saved in a variable (say $ip_address), and suppose that you've got a filehandle for a logfile that's opened for writing (say $logfile_handle), then you can just do this (adding a timestamp as well for good measure):

        ... my $ip_address = inet_ntoa($iaddr); ... say $logfile_handle scalar localtime, " Connection from: $ip_address"; ...

        Integrating this into the above server example should be straightforward.

Re: Client Server
by sn1987a (Deacon) on Jul 09, 2014 at 19:57 UTC

    You never send the client's IP back to the client.

    print "Connection recieved from $name\n";

    will just print it to the server's STDOUT. You would need to add

    print NEW_SOCKET "Connection recieved from $name\n";

    to also send it to the client.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1092959]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found