I tried it using Socket::Class.
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use Socket::Class; our $RUNNING : shared = 1; our $Server = Socket::Class->new( 'local_addr' => '127.0.0.1', 'local_port' => '7777', 'listen' => 5, 'blocking' => 0, 'reuseaddr' => 1, ) or die Socket::Class->error; $SIG{'INT'} = \&quit; threads->create( \&server_thread, $Server ); threads->create( \&client_thread, $Server ); while ($RUNNING) { $Server->wait(5); } sub quit { my ($thread); $RUNNING = 0; foreach $thread ( threads->list ) { eval { $thread->join(); }; } $Server->free(); exit(0); } sub server_thread { my ($server) = @_; my ($client); print "Server running at port: ", $server->local_port, "\n"; while ($RUNNING) { $client = $server->accept(); if ( !defined $client ) { last; } elsif ( !$client ) { next; } threads->create( \&client_thread, $client ); } return 1; } sub client_thread { my ($client) = @_; my ( $buf, $got ); print 'Client is running on port ' . $client->remote_port . "\n"; $client->wait(5); $client->free(); threads->self->detach() if $RUNNING; return 1; } quit();

In reply to Re: Getting port number with AnyEvent::Socket by Khen1950fx
in thread Getting port number with AnyEvent::Socket by MiggyMan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.