Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:08:58+00:00 2026-06-14T07:08:58+00:00

Recently I ran into a great perl module "AnyEvent", which allows user to do

  • 0

Recently I ran into a great perl module "AnyEvent", which allows user to do asynchronous/event-driven programing.

Created the following snippet which works fine. The problem I have is that after it opens and closes lots sockets, it quickly exhausted all the client ports ("netstat -ant" shows 20,000+ sockets are in TIME_WAIT state).

$hdl = new AnyEvent::Handle (
  connect => [$ip, $port],
  on_connect=> sub {
      my ($handle, $host, $port, $tmp) = @_;
      #print "connect routine for $handle->{ue}\r\n";
      #update states.
  },
  on_read => sub {
      my $hdl = $_[0];
      #read data
      #send response.
  });

I wonder if it’s possible to create TCP socket with IO::Socket::INET and then use the newly created socket in AnyEvent::Handle:

my $sock = IO::Socket::INET->new( Proto    => 'tcp',
                             PeerAddr => $ue->{vars}->{ip},
                             PeerPort => $ue->{vars}->{dstPort},
                             ReusePort => 1,
            KeepAlive => 1
) || die "failed to setup outsock $@\n";
$hdl = new AnyEvent::Handle (
  fh => $sock,
  on_connect=> sub {
      my ($handle, $host, $port, $tmp) = @_;
      #print "connect routine for $handle->{ue}\r\n";
      #update states.
  },
  on_read => sub {
      my $hdl = $_[0];
      #read data
      #send response.
  });

Tried it but it doesn’t work. Appreciate any suggestions/comments.

Thanks to ikegami who looked into it and gave an suggestion. However, it seems that SO_REUSEADDR doesn’t take effect. Here is the code I used (based on his suggestion)

use strict;
use warnings;

use AnyEvent           qw( );
use AnyEvent::Handle   qw( );
use AnyEvent::Impl::EV qw( );
use AnyEvent::Socket   qw( tcp_connect );
use Socket             qw( SOL_SOCKET SO_REUSEPORT SO_REUSEADDR);

my $ts = 0;
my $trans = 0;
my $currentTS;

sub transaction {
   my ($host, $port) = @_;
   tcp_connect($host, $port, sub {
      my ($sock) = @_
         or die "Can't connect: $!";

      my $handle;
      $handle = AnyEvent::Handle->new(
         fh => $sock,
         on_eof => sub {
            $handle->destroy();
         },
         on_read => sub {
            my ($handle) = @_;
            #print $handle->rbuf();
            $trans ++;
            $currentTS = time();
            if ($currentTS > $ts) {
                $ts = $currentTS;
                print "$trans\n";
            }
            #printf "recved %d bytes of data\n", length($handle->rbuf);
            # This should continue to read until header +
            # Content-Length bytes have been read instead
            # of stopping after one read.
            if (length($handle->rbuf) > 0) {
                $handle->destroy();
            }
         },
      );
      $handle->push_write("GET /s HTTP/1.1\r\nHost: $host\r\n\r\n");
      #$handle->push_shutdown();  # Done writing.
   }, sub {
      my ($sock) = @_;

      #setsockopt($sock, SOL_SOCKET, SO_REUSEPORT, 1) or die $!;
      setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, 1)  or die $!;
        #   die "failed to set linger $!\n";
      return undef;
   });
}
{
   my $cv = AnyEvent->condvar();

   my $t = AnyEvent->timer(after=>0.001, interval=>1, cb=> sub {
      transaction("10.3.0.6", 80 );
   });

   $cv->recv();
}

My system is Ubuntu 11.04.
In directory /proc/sys/net/ipv4, here are the content of two files:

% more tcp_tw_recycle

1

% more tcp_tw_reuse

1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:08:59+00:00Added an answer on June 14, 2026 at 7:08 am

    I can’t run the following because Windows doesn’t provide SO_REUSEPORT, but I’m extremely confident that the following does what you requested.

    That said, I’m not sure it will help. From what I’ve read, SO_REUSEPORT allows you to bind to an already active port, but you’re not binding to any port.

    use strict;
    use warnings;
    
    use AnyEvent           qw( );
    use AnyEvent::Handle   qw( );
    use AnyEvent::Impl::EV qw( );
    use AnyEvent::Socket   qw( tcp_connect );
    use Socket             qw( SOL_SOCKET SO_REUSEPORT );
    

    sub transaction {
       my ($host, $port) = @_;
       tcp_connect($host, $port, sub {
          my ($sock) = @_
             or die "Can't connect: $!";
    
          my $handle;
          $handle = AnyEvent::Handle->new(
             fh => $sock,
             on_eof => sub {
                $handle->destroy();
             },
             on_read => sub {
                my ($handle) = @_;
                print $handle->rbuf();
    
                # This should continue to read until header +
                # Content-Length bytes have been read instead
                # of stopping after one read.
                $handle->destroy();
             },
          );
    
          $handle->push_write("GET / HTTP/1.1\r\nHost: $host\r\n\r\n");
          $handle->push_shutdown();  # Done writing.
       }, sub {
          my ($sock) = @_;
    
          setsockopt($sock, SOL_SOCKET, SO_REUSEPORT, 1)
             or die $!;
    
          return undef;
       });
    }
    

    {
       my $cv = AnyEvent->condvar();
    
       my $t = AnyEvent->timer(after=>0.001, interval=>0.001, cb=> sub {
          transaction("localhost", $ARGV[0] // die("usage"));
       });
    
       $cv->recv();
    }
    

    Server used for testing:

    use strict;
    use warnings;
    use 5.010;
    
    use IO::Socket::INET qw( );
    use Socket           qw( inet_ntoa );
    
    my $serv = IO::Socket::INET->new(
       Listen => 1,
    );
    
    say inet_ntoa($serv->sockaddr) . ":" . $serv->sockport;
    
    while (my $client = $serv->accept()) {
       say "Connection from ".inet_ntoa($client->peeraddr).":".$client->peerport;
       while (<$client>) {
          last if /^(?:\r?\n)?\z/;
       }
    
       say $client "HTTP/1.1 200 OK\r\n"
          .        "Content-Type: text/plain\r\n"
          .        "\r\n"
          .        "Hello\n";
    
       say "   done.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran into an issue at work in which, at least according to
I recently ran into a situation in which accessing session information from view is
I recently ran into a very sneaky bug, in which I forget to dereference
I recently ran into a bug in my code when using boost::bind . From
I recently ran into an error when I tried to change the column name
I recently ran into an issue where a query was causing a full table
I recently ran into a familiar javascript/jQuery timing bug and spent too long debugging
A problem I recently ran into was that when trying to update a field
I'm currently playing around with tipfy on Google's Appengine and just recently ran into
My application requires the .NET Framework version 3.5. I recently ran into a customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.