By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,536 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,536 IT Pros & Developers. It's quick & easy.

creating Handles

P: n/a
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;
};
}
}
}
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ak**********@yahoo.com (SRam) wrote in message news:<20**************************@posting.google. com>...
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.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.