The value of the internal file handle should suffice. To give some background, I am writing a simple mock tcp server, using IO::Socket::INET and IO::Select, to use in system testing. It is not finished yet, but as a minimum I'd like to at least display the socket ids in a trace so I can tell which client is sending what. Here are some (incomplete) code snippets to give you a feel:
my $listener = IO::Socket::INET->new(
LocalPort => $port,
Proto => 'tcp',
Listen => 5,
ReuseAddr => 1,
) or die "error: IO::Socket::INET: $@";
my $selector = IO::Select->new($listener);
while ( my @ready = $selector->can_read() ) {
for my $client (@ready) {
if ( $client == $listener ) {
my $new_conn = $listener->accept();
$selector->add($new_conn);
my $fh_hex = sprintf '0x%x', $new_conn;
print "Accepted new connection ($fh_hex)\n";
# ...
sub recv_client {
my $client = shift;
my $fh_hex = sprintf '0x%x', $client;
print "Recv from client ($fh_hex):\n";
# ... display data received ...
}
Update: Thanks to BrowserUk's excellent tip I am now using:
my $fh_hex = sprintf '0x%x', $new_conn;
my $peerhost = $new_conn->peerhost();
my $peerport = $new_conn->peerport();
my $peeraddr = $new_conn->peeraddr();
my $peerhostfull = gethostbyaddr($peeraddr, AF_INET) || "Cannot resolv
+e";
print "Accepted new connection $fh_hex ($peerhost:$peerport,host=$peer
+hostfull)\n";
which is much better.
|