Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > creating Handles

Reply
Thread Tools

creating Handles

 
 
SRam
Guest
Posts: n/a
 
      08-19-2003
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;
};
}
}
}
 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      08-21-2003
(SRam) wrote in message news:<. 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about file handles and windows handles @ Windows Operating Systems eino Python 1 05-08-2007 09:14 PM
What group handles database issues for .NET/C#? Larry Maturo ASP .Net 1 11-04-2005 09:14 PM
Anybody know how ASP.Net handles incoming requests from the ISAPI extension? Rick Strahl [MVP] ASP .Net 2 05-06-2004 02:04 PM
one custom error page that handles all exceptions feng ASP .Net 1 01-20-2004 07:22 PM
Accessing DCOM components from the code behind pages and using sessions to store DCOM object handles Alex ASP .Net 3 12-02-2003 01:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57