sockets

S

someone

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.
 
R

Rocco Caputo

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top