Problems? Is your data what you think it is? | |
PerlMonks |
send() + recv() on nonblocking tcp socketsby ph713 (Pilgrim) |
on Oct 12, 2005 at 16:32 UTC ( [id://499584]=perlquestion: print w/replies, xml ) | Need Help?? |
ph713 has asked for the wisdom of the Perl Monks concerning the following question:
Once again, I'm turning to the monks in hopes that somebody has sorted this mess out before. I have a class (let's call it Acme::Connection) which handles tcp connections and abstracts them a bit.
The question at the heart of this node is: What is the correct way to code for tcp sending and receiving in perl such that you handle all possible error conditions correctly, quickly, and efficiently, with gauranteed maximum timeouts of varying amounts throughout? (A maximum timeout for connecting, for recv()ing the first packet, perhaps a longer timeout for recv()ing the next packet, etc)...
And what that question really boils down to is - for a nonblocking tcp connection, what is the correct way to handle all the possible outcomes of calling $socket->recv($numbytes);. There's the actual return value we can check for defined-ness, there's the definedness and/or length of the received data versus the $numbytes we asked for, and there's several different relevant errnos which require different sorts of action. it ends up being a matrix of many possibilities and few documented gaurantees... In the case of send() the possibilities are even more complex, but in practice it tends to never fail for reasonably-sized packets on an open connection.
Specifically, the expected behavior is that one can open a connection with:
And that one can send or receive a chunk of data with the following calls:
The expected behavior is that $conn->recv(3124) will attempt to receive 3124 bytes of data over the network, and will fail with a timeout exception if that doesn't occur within 12 seconds (set with def_timeout in the constructor). Similarly, $conn->send($data) should send the contents of $data, and fail with a timeout exception if that doesn't happen in 12 seconds. I've truncated what used to be here, it was a big peice of ugly code, the current implementation at the time of the question.
Back to
Seekers of Perl Wisdom
|
|