On 7/27/11 Wed  Jul 27, 2011  10:07 AM, "siegfr...@heintze.com"
<siegfr...@heintze.com> scribbled:

> Sorry if this appears twice. Since it bounced back to me -- probably
> because of the HTML format -- I'm sending it again.
> 
> I did some google searching and I could not find an example of a
> bidirectional asynchronous socket client. A telnet client is an example
> of a bidirectional asynchronous socket client.
> 
> I don't specifically want source to a telnet client -- that would be
> much fancier than what I require and would not be helpful if the perl
> only called C++. I just want an example in pure perl (or ruby or
> python).
> 
> 
> By asynchronous I mean that reads and writes can occur at any time in
> any order with no warning.
> 
> 
> Asynchronous could also mean that we don't block while the  write is in
> progress. Blocking while the write is in progress is fine.
> 
> 
> I hope it is possible to write such a beast in perl.
> 
> Let's suppose I spawn two threads, one to block on a read socket and the
> other to block on the keyboard with $inp=<STDIN> (so it can print to the
> write socket when it receives input form the keyboard). Can the first
> thread print to the console if the second thread is blocking on
> $inp=<STDIN>? 
> 
> 
> Surely, someone has posted source to such a beast somewhere on the
> internet! 

I do not know of such an example, nor have I ever written one in Perl. I
have some experience in this type of application in other languages (C++,
Java).

Have you read 'perldoc perlipc'? Specifically the socket sections of that
document?

Your basic choices are to use multitasking, with one process or thread per
i/o handle, or wait on multiple i/o handles in one task.

The former approach requires the use of processes or threads. Processes are
created by the fork function, and are well-supported by Perl, at least under
Unix. However, processes do not share data, so you have an interprocess
communication problem.

Threads can share data structures, but threads are not well-supported by
Perl. I have read a lot of warnings about using threads under Perl, and I
have avoided using them for that reason.

The select function is the way to wait on multiple file handles under Unix.
It may also be supported under Windows. Figuring out how to use it is not
easy, but the effort can be rewarding. There is a built-in function select
that gives direct access to the Unix system call (see 'perldoc -f select').
There is also a module IO::Select that puts an object-oriented layer on top
of the select function.

Having used both approaches in other languages, but not Perl, my inclination
would be to take the 'select' approach, but you might want to try
multitasking. Once you figure out how to use select (or IO::Select), it
works well.

Good luck!



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to