reading byte stream from network

A

akifusenet

Hi

I hope this is an OK place to ask a beginner question. Right now I am
just playing around with Ruby and IMO the best way to play around is
actually writing smtg. So I wanted to convert one of my old C programs
into Ruby. This simple program is listening on a UDP port and receives
some formatted messages send by a program. Sender program is actually
sending out a C struct. Smgt like:

typedef struct header{
UINT32 x;
UINT32 y;
UINT32 z;
} header_type;

I came upto a point in which I created an UDPSocket and recvfrom works
fine. But the received string is ofcourse cryptic. How can I convert
this byte stream into something meaningful again?

First question, how can I change network to host byte order? ( like
ntoh?)
And then how can i put this byte stream into a struct or smtg else.
(The real aim is getting x,y,z quickly)

Thanks in advance.

Akif,
 
T

thomas.macklin

Hi

I hope this is an OK place to ask a beginner question. Right now I am
just playing around with Ruby and IMO the best way to play around is
actually writing smtg. So I wanted to convert one of my old C programs
into Ruby. This simple program is listening on a UDP port and receives
some formatted messages send by a program. Sender program is actually
sending out a C struct. Smgt like:

typedef struct header{
UINT32 x;
UINT32 y;
UINT32 z;

} header_type;

I came upto a point in which I created an UDPSocket and recvfrom works
fine. But the received string is ofcourse cryptic. How can I convert
this byte stream into something meaningful again?

First question, how can I change network to host byte order? ( like
ntoh?)
And then how can i put this byte stream into a struct or smtg else.
(The real aim is getting x,y,z quickly)

Thanks in advance.

Akif,

Yeah, look at String#reverse in conjuction w/ unpack. One of the
coolest and most confusing aspects of ruby (for me as a C programmer
anyway) is the equivalence between strings and unstructured binary.
It's confusing b/c buffers of characters in C in a way have "duck
typing" as you usually read them in as an unsigned char *. Usually
with OO languages other than ruby, you have to go through some madness
to get unstructured data into a string, or vice versa.
 
J

Joel VanderWerf

First question, how can I change network to host byte order? ( like
ntoh?)
And then how can i put this byte stream into a struct or smtg else.
(The real aim is getting x,y,z quickly)

The suggestions to use pack and unpack are right on target, and learning
how to use them will be very valuable. At some point, you may want to
have a way of declaratively working with binary data structures, and
there are a couple of libraries that can help with that:

binaryparse (available as gem)

bindata (ditto)

bitstruct (my own library, not a gem:
http://raa.ruby-lang.org/project/bit-struct)
 
A

Akif Tokuz

unknown said:
Yeah, look at String#reverse in conjuction w/ unpack. One of the
coolest and most confusing aspects of ruby (for me as a C programmer
anyway) is the equivalence between strings and unstructured binary.
It's confusing b/c buffers of characters in C in a way have "duck
typing" as you usually read them in as an unsigned char *. Usually
with OO languages other than ruby, you have to go through some madness
to get unstructured data into a string, or vice versa.


I didnt think it would work in the first run. But hell it worked!

You should have seen the faces of my friend when i showed them this
single line is actually equal to that ^+%+%&^+%^+ lines of C code:=)
Respect...

def processTrackDataMessage(message)
trackDataMsg=message.unpack('SIS3D2CDSDSDSDS3A6S36DS10D4S2ISA36A12A16IS2A36A12A36A12')
end

Thanks for the tip guys.

Akif,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top