I've never posted a code segment to a mailing list.  I hope I'm doing this
right.  If not, please pardon me...

 

I need to write a multiplexed TCP server to handle multiple client
connections simultaneously.  It doesn't help that I'm new to perl and even
newer to TCP.  If this helps, I'm basing my code on an example on pp.
352-358 of Network Programming with Perl, Lincoln D. Stein, 2001.  I need to
store the socket handles in a hash with the IP of the client as the index,
because the traffic to be sent to the clients will come prefaced by the
recipient IP.  How do I retrieve the client IP after or from the "$connect =
$listen_socket->accept();" statement? I've seen it done with other connect
and bind methods.

 

Included below is the code as it stands just for establishing connections.
All help will be graciously accepted and greatly appreciated!

 

Darren Peterson

 

 

#!/usr/bin/perl

use IO::Socket;

use IO::Select;

 

use strict;

use warnings;

 

my %SESSIONS; # hash of socket handles, indexed by IP.

 

my $connect;

 

use constant PORT => 12000;

 

my $listen_socket = IO::Socket::INET->new(LocalPort => PORT,

                                          Listen => 20,

                                          Proto => 'tcp',

                                          Reuse => 1);

 

die $@ unless $listen_socket;

 

my $readers = IO::Select->new() or die "Can't create IO::Select read
object";

 

$readers->add($listen_socket);

 

warn "Listening for connections...\n";

 

while (1) {

 

   my @ready = $readers->can_read;

 

   for my $handle (@ready) {

 

      if ($handle eq $listen_socket) {

         $connect = $listen_socket->accept();

         $readers->add($connect);

      }

   }

}

 

Reply via email to