http://qs321.pair.com?node_id=184679

gmpassos has asked for the wisdom of the Perl Monks concerning the following question: (network programming)

Let's say that a server socket need to read the data sent by the client, but the client is not sending any data. In this case the server will be trying to read indefinitely. In other words, the server app will be stoped in the same command indefinitely. To prevent this we need to check if the socket has data to read first, if the client already have sent the data, and only after this read the socket. What is the best way to do this? (without threads)

Originally posted as a Categorized Question.

  • Comment on What is the easy way to check if the socket has data to read, before do a read()

Replies are listed 'Best First'.
Re: What is the easy way to check if the socket has data to read, before do a read()
by perlknight (Pilgrim) on Jul 24, 2002 at 14:21 UTC
    Use IO::Select, pass the socket handle to it and use can_read method to detemine if the handle is ready, here's snippet from perldoc:
    use IO::Socket; use IO::Select; $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080); $sel = new IO::Select( $lsn ); while(@ready = $sel->can_read) { ... }