Oct 7, 2010, 10:36 PM
Post #4 of 4
Two issues you have to deal with are buffering and newline conversions. perl allows you to turn off automatic \n conversions. Newline conversions are the only difference between sending binary data and sending 'regular' data(= line oriented data). When you are sending binary data, you don't want perl to secretly add extra bytes, which perl does when you send a \n and the programming is running on a Windows operating system, because the other side is relying on strict byte counts for the data boundaries.
Re: [MusicMonkey5555] Binary Networking
[In reply to]
The easy way to turn off automatic conversions is to call binmode(). Another way is to provide the proper arguments to open() when you open the socket. See the docs for open(). If you are using IO::Socket, then I've been told that new() and connect() do not add newline conversions. However, I have a note in my book that says you still need to call binmode(), so I must have run across a situation where what I was told didn't seem to hold true.
The other issue is buffering. You usually don't want perl to buffer data into larger, more efficient chunks before sending the data because that can cause deadlock: one side has finished sending the data, but the other side hasn't received anything because perl is waiting for more data to fill the buffer before sending the data. To turn off buffering, you can either use IO::File and call autoflush() on a filehandle, or set perl's global variable $| equal to 1. If you use IO::Socket, I think buffering is turned off by default for calls to new() and connect().
If you are in the market for one, I recommend the book "Network Programming with Perl" by Lincoln Stein. I've read some network programming books for other languages, and they are universally so bad, I would recommend that anyone who wants to learn some network programming should learn basic perl and then read "Network Programming with Perl" because it is so good.
"Network Programming with Perl" is an old book and some important behind the scenes I/O structures have changed in perl since it was written (read the open() docs for information about the new I/O 'layers' perl employs), but that only diminishes its excellence a little bit. I think it's a real shame the author hasn't found the time to write an updated edition because the book is one of the real gems in computer programming.
(This post was edited by 7stud on Oct 7, 2010, 11:14 PM)