On Sun, 18 Jan 2004 20:41:04 +0800, someone wrote:
> is there a way i can check whether there is any data in a tcp socket and to
> check whether the socket has closed or not
>
> for example
>
> $line = <SOCKET>;
>
> if the party at the other end does not send data, then the program will hang
> at this point until data arrives. if there is data, how to know the size? i
> want to read whatever that is available.
The select() function, conveniently wrapped by the IO::Select module,
will let you wait for data on a socket without necessarily blocking.
sysread() will read whatever is available on the socket, up to a
specified limit.
If you want input by lines, you will need to gather data in a buffer and
find the newlines therein. Buffered I/O like <SOCKET> is not compatible
with select(), and the documentation for select() will mention this.
See:
perldoc IO::Select
perldoc -f sysread
perldoc -f syswrite
perldoc -f select
_Perl Cookbook_ (which has good examples of these things)
--
Rocco Caputo -
-
http://poe.perl.org/