creating Handles

Discussion in 'Perl' started by SRam, Aug 19, 2003.

  1. SRam

    SRam Guest

    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;
    };
    }
    }
    }
    SRam, Aug 19, 2003
    #1
    1. Advertising

  2. SRam

    Guest

    (SRam) wrote in message news:<>...

    > 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.
    , Aug 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Maturo
    Replies:
    1
    Views:
    431
    Jason Newell
    Nov 4, 2005
  2. Alex
    Replies:
    3
    Views:
    1,491
    Alvin Bruney
    Dec 2, 2003
  3. feng
    Replies:
    1
    Views:
    346
    Curt_C [MVP]
    Jan 20, 2004
  4. Rick Strahl [MVP]
    Replies:
    2
    Views:
    381
    Teemu Keiski
    May 6, 2004
  5. eino
    Replies:
    1
    Views:
    431
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    May 8, 2007
Loading...

Share This Page