On 08/12/2015 01:53 PM, Brandon McCaig wrote:
Keep in mind that when reading from a socket you're not reading directly from a hard link to the data. There's an entire network of devices that the data has to travel through to arrive at your machine. Lots can happen on the network. Packets can be dropped, routed wrong, etc. The packets get reassembled in order once they arrive on your machine and only then can you read the data from them. There's no way to ensure that all of the data is there when you go to read. All you can do is read and hope. There's no guarantee the data will ever get to you. A robust program will be prepared to deal with that. One thing to note is that when you read you're throwing away partial reads of data. If the stream contains more than 512 bytes of data then you'll lose the first n - 512 bytes. If you want to slurp up the entire thing into memory and process it all at once then perhaps at a -1 argument to read() indicating that it should store the read data at the end of the buffer instead of overwriting it.

that isn't really the case regarding data loss. if you are using TCP it is designed to ensure all the data gets there in the correct order. it may have a rare burp or so but consider all the web and more relies on it. the IP layer may drop packets but tcp will resend them as demanded by the receiver. you don't write tcp based socket code with extra code to check for missing data. also the read 512 bytes you bring up doesn't make sense to me. the reads will always return something and maybe not a full read. you check the read count for -1 and then the errno to check for an error. managing where the read data goes is another subject.

uri


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to