The stupid question is the question not asked | |
PerlMonks |
Re^3: sysread and syswrite in tcp socketsby gone2015 (Deacon) |
on Dec 22, 2008 at 19:24 UTC ( [id://732176]=note: print w/replies, xml ) | Need Help?? |
I'm not sure I understand the problem here... You're printing the <username> to a socket, sleeping for 1 second, and then printing the <pwd>. I cannot tell whether the other end will see these as separate or not -- though it is likely if client and server are the same machine. sysread will return what has been received so far, or will wait until at least 1 byte is available -- so if the receiver is fast enough it will effectively see each packet as it arrives (all the bytes of a packet become available at the same time). read, on the other hand, will collect data from incoming packets until it has the number of bytes you asked for, or the connection is closed. At the sender end we have: (a) any buffering done at the Perl level (in particular PerlIO); and (b) any buffering done at the socket level. Stuff output by print will be buffered by PerlIO. If 'autoflush' is set, then at the end of a print statement, the buffer will be flushed to the socket. But syswrite bypasses the PerlIO buffering, and sends stuff directly to the socket. You will find that IO::Socket::INET sets 'autoflush' by default -- so by default the difference between print and syswrite is small, except that with syswrite you have to cope with partial writes ! Now, there's nothing in TCP that absolutely requires packets to be sent as soon as there is data ready to go, but generally it will do so unless data previously sent has not yet been acknowledged (this is Nagle's algorithm). So, whether the result of separate print or syswrite operations arrive together depends to some extent on the speed of the network. As far as you fragment of code is concerned, this will (if it is fast enough) createFile() for each packet received... I suspect that isn't what you had in mind ?
In Section
Seekers of Perl Wisdom
|
|