CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Binary Networking

 



MusicMonkey5555
Novice

Oct 6, 2010, 2:37 PM

Post #1 of 4 (962 views)
Binary Networking Can't Post

I am using use IO::Socket::Multicast;

To receive and send packets using multicast. I use recv to get the data currently and mcast_send to send the data. Currently it is text readable ascii. I need to re-write it all to relieve and send binary data. Any tips?

I am guessing I can still just use recv and send, but the var it is stored in will contain binary rather than ascii so I need to do a simple conversion from binary to ascii. Then before I want to send I need to do a conversion from ascii to binary. Is this correct or is there an easier way or more to it?


7stud
Enthusiast

Oct 6, 2010, 10:56 PM

Post #2 of 4 (958 views)
Re: [MusicMonkey5555] Binary Networking [In reply to] Can't Post


Quote
so I need to do a simple conversion from binary to ascii

Of course, that's the crux of the problem right there. How will you do the conversion? And how will you know when you have reached the end of the data that was sent?

Similarly, how will the other side know how to convert the data, and how will the other side know when you are done sending data?


(This post was edited by 7stud on Oct 6, 2010, 10:59 PM)


MusicMonkey5555
Novice

Oct 6, 2010, 11:22 PM

Post #3 of 4 (951 views)
Re: [7stud] Binary Networking [In reply to] Can't Post

I feel really stupid now after figuring it out more. What they meant by Binary was sending it just as bits (how it is always sent which I knew). the difference is that instead of those bits representing ascii characters they represent actual data (int, char...) not an ascii character it isn't about what is sent or revived, but how it is formatted and how I grab the data or parse it.

As you said I need to know the structure, which is documented. so for instance if the start is an int then I will read in the first 32 and store it then the next item (how ever big it may be). Once I know the structure I can simply read it in as an int, then a long, then an char or whatever else.

My only worry now is if i read it using recv then send it out again (for something else c++ to handle and parse it) will any of it have changed. It shouldn't just being stored in a var, but not sure if send appends anything or not. If you have any clue about that let me know. Suppose it won't matter since I know what I need to read and if there is some extra I can just ignore it.


(This post was edited by MusicMonkey5555 on Oct 6, 2010, 11:24 PM)


7stud
Enthusiast

Oct 7, 2010, 10:36 PM

Post #4 of 4 (931 views)
Re: [MusicMonkey5555] Binary Networking [In reply to] Can't Post

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.

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)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives