Please group reply to prevent getting accidentally ignored and so
everyone can help and be helped. Also please bottom post.

[EMAIL PROTECTED] wrote:
> Well, I have tried some modules, etc, but none of them have been
> properly documented, or made the documentation available to any
> mortals.  After trying out a few, I prefered to do the socket
> programming myself.
>

Which ones? The modules used for this task are going to be pretty
advanced, as the task is, they are likely going to take more than a
cursory glance to figure them out.

> If I do use select for sockets, or something, i.e:
> 
> while(1)
> {
>       if(my $input = <$sock1>)
>       {
>           ......
>       }
> 
>       if(my $input = <$sock2>)
>       {
>           ......
>       }
> }
> 
> Won't it just sit on $sock1 waiting for data until it gets some,
> whilst messages could be pouring in unread on $sock2?
> 

Yes and no. With select you can check whether a filehandle is ready for
reading (or writing, etc.) then you act on it. You don't check for input
by reading the input (thereby avoiding the blocking). After you have
checked to see if it is ready, then you read from it. Having said all of
this you *really* don't want to use the built-in select, definitely use
IO::Select if you don't use something higher level like POE. There is a
very plain and simple example at the bottom of the doc page for IO::Select:

http://search.cpan.org/~nwclark/perl-5.8.7/ext/IO/lib/IO/Select.pm

Though it is more of a server rather than client example. Essentially
you open the filehandles (sockets) you want to read from, add them to an
IO::Select object, then ask that object for a list of handles ready to
be read from. Then just read from the handle. On the same token you
should really be using IO::Socket::* instead of messing with the sockets
directly. If it is good enough for Mr. Stein, it is good enough for
*almost* all of us.

Having said all of this, it still isn't truly multitasking because you
can still block on a socket that had a lot of data or a slow socket.
This method presumes that your CPU and IO handler is fast enough to
prevent the appearance of a slow down because of blocking. To truly
multitask you are going to have to use forks, threads, or similar. POE
again could help with setting these things up which is why I recommend
it. It is a framework specifically designed to handle multitasking with
interesting IO, particularly from sockets.  But all of this gets
relatively complex which is why I recommended Network Programming with Perl.

> Also, is select necessary - can't you just use each sock my name,
> instead of by default?
> 

If I understand the above correctly, yes it is necessary, but I think
you are confusing the 4 argument form of select with the 1 argument
form. Regardless IO::Select hides all of those details.

perldoc -f select

Good luck,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to