creating Handles

S

SRam

I am coding for a server. After server is reading a particluar port,
How can I create handles for them and distinguish them individually


#!/usr/local/bin/perl -w

use strict;
use IO::Socket;
use IO::Select;
use IO::Handle;

# create a socket to listen to a port
my $listen = IO::Socket::INET->new(Proto => 'tcp',
LocalPort => 2323,
Listen => 1,
Reuse => 1) or die $!;

# to start with, $select contains only the socket we're listening on
my $select = IO::Select->new($listen);

my @ready;

# wait until there's something to do
while(@ready = $select->can_read)
{

my $socket;

# handle each socket that's ready
for $socket (@ready)
{

# if the listening socket is ready, accept a new connection
if($socket == $listen)
{
my $new = $listen->accept;
$select->add($new);
print "Select= $select, \n";
print $new->fileno. ": connected\n";

}
else
{

# otherwise, read a line of text and send it back again
my $line="";
$socket->recv($line,80);
print "\nReceived at Server:$line";
$line ne "" and $socket->send($line) or do
{
# either can't send or can't receive, so they must have hung up
print $socket->fileno . ": disconnected\n";
$select->remove($socket);
$socket->close;
exit;
};
}
}
}
 
N

nobull

Subject: creating Handles

I do not think that your question is about creating handles.
I am coding for a server. After server is reading a particluar port,
How can I create handles for them and distinguish them individually

I do not understand the question - the code you have written seems to
do just that.

I suspect you were wanting to asking was how to store application
level information assocuated with a socket so that you can retrieve it
when IO::Select passed you a ready handle.

If you use a Perl object reference (other than an object that uses
overload) in a string context it will return a string that can be
assumed to uniquely define that object so long as the object exists.

You can therefore use object references as hash keys.

my $new = $listen->accept;
$socket_info{$new}{FOO} = 'bar';

You need to remember to delete the entry in %socket_info when you
destroy the IO::Socket object.

Alternatively, objects that are based on GLOBS (such as IO::Socket
objects) have spare storage capacity. You can exploit this. The HASH
knob is already used by IO::*, but you can hang private data off the
SCALAR knob.

my $new = $listen->accept;
${*$new}->{FOO}='bar';

You could also use an ARRAY keyed on fileno but this would be
pathologically bad if you ever used a platform where sockets are given
high filenos.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top