Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Client Server

by AppleFritter (Vicar)
on Jul 09, 2014 at 21:06 UTC ( [id://1092968]=note: print w/replies, xml ) Need Help??


in reply to Client Server

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.

Replies are listed 'Best First'.
Re^2: Client Server
by Anonymous Monk on Jul 09, 2014 at 22:55 UTC
    Yep, that did it. Thank you!
Re^2: Client Server
by TheVend (Novice) on Jul 15, 2014 at 17:28 UTC
    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.

        This is what I needed I wasn't sure how to qualify the IP as a variable this did the trick.

        my $ip_address = inet_ntoa($iaddr);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-23 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found