[San-Diego-pm] A few questions of Perl

Tkil tkil at scrye.com
Wed Dec 9 22:17:05 PST 2009


>>>>> "Alex" == Alex (Yu) Hu <foxele at gmail.com> writes:

Alex> I am working on a perl program that is suppose to be a test
Alex> application running on PC.

"on PC" meaning "under windows", yes?

Alex> If you don't mind, I have two questions regarding Perl:

Alex> 1. The perl program I am running has a network interface which
Alex>    is acting like a TCP client. For some reason, I want the
Alex>    client to send message on one port and receive message on the
Alex>    other port. Is this possible in perl?  I have a module does
Alex>    the send and receive on one port, but I don't know how to add
Alex>    a second one.

It's very much possible:

| #!/usr/bin/perl
| 
| use warnings;
| use strict;
| 
| use constant BUFFER_SIZE => 1000;
| 
| use IO::Select qw();
| use IO::Socket::INET qw();
| 
| # set up listening "server" socket:
| my $server = IO::Socket::INET->new( LocalPort => 12345,
|                                     ReuseAddr => 'true',
|                                     Listen    => 10 )
|   or die "$0: unable to configure server socket";
| 
| # now set up the output socket:
| my $output = IO::Socket::INET->new( PeerAddr => 'localhost:23456' )
|   or die "$0: unable to connect to output destination";
| 
| # add the server socket to the list of things to listen to
| my $select = IO::Select->new();
| $select->add( $server );
| 
| # only need one buffer, so allocate it outside the loop
| my $buffer = " " x BUFFER_SIZE;
| 
| # wait for something to happen.
| MAIN:
| while ( my @ready = $select->can_read() )
| {
|     foreach my $socket ( @ready )
|     {
|         if ( $socket eq $server )
|         {
|             # new incoming connection
|             my $new_client = $server->accept();
|             $select->add( $new_client );
|         }
|         else
|         {
|             # incoming info; read it, then forward to output
|             $buffer = "";
|             recv $socket, $buffer, BUFFER_SIZE, 0;
|             my $bytes = length $buffer;
|             if ( $bytes )
|             {
|                 send $output, $buffer, $bytes;
|             }
|             else
|             {
|                 # empty read means eof
|                 $select->remove( $socket );
|             }
|             last MAIN if $buffer =~ m!^quit\s*!s;
|         }
|     }
| }
| 
| exit 0;

Alex> 2. Is there a event scheduler modules I can use to schedule the
Alex>    sending of a message say several milisecond in the future.

If you're doing everything synchronously, then the 4-argument "select"
solution (as described in previous messages on the list) will work:
just wait for the given amount of time, then send the message.

If you're doing things asynchronously, then you're suddenly in a much
more complex world.  Basically, you have to recast your program into
event-driven terms; once you've done that, you can either create timer
events on a new handle (and use a loop similar to the one above), or
you can investigate some of the object/event-oriented Perl libraries
(of which POE seems to be an ongoing favorite.)

HTH,
t.


More information about the San-Diego-pm mailing list