Reading byte stream from network

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,

unknown wrote:

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

Take a look at unpack (ri String#unpack)

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Aug 16, 8:42 am, akifuse...@gmail.com wrote:

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,

akifusenet@gmail.com wrote:

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\)

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thank you. Just what am I looking for.

Akif,

···

On 16 A ustos, 15:49, Stefan Rusterholz <apei...@gmx.net> wrote:

unknown wrote:
> typedef struct header{
> UINT32 x;
> UINT32 y;
> UINT32 z;
> } header_type;

Take a look at unpack (ri String#unpack)

Regards
Stefan
--
Posted viahttp://www.ruby-forum.com/.

unknown wrote:

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,

···

--
Posted via http://www.ruby-forum.com/\.