# Coding a server

**URL:** <https://www.ruby-forum.com/t/coding-a-server/156913>\
**Category:** Ruby\
**Created:** [January 5, 2009, 8:25am UTC](https://www.ruby-forum.com/t/coding-a-server/156913 "2009-01-05T08:25:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 8:25am UTC](https://www.ruby-forum.com/t/coding-a-server/156913/1 "2009-01-05T08:25:04Z")

</div>

Hey,

I’m porting some old c++ code for a server to ruby, and I can’t figure  
out what’s the correct way of doing the network packet parsing

I’m using eventmachine for the networking code but when a packet is  
received it must be parsed and depending on the header the packet will  
have one format or another, and each of these would have an associated  
reply format, I’m not quite sure on how to code this while keeping it  
clean

Any ideas on how you would do this?  
also, in cpp I’m using structs for the packet formats, but I have no  
idea on how to do that in ruby

---

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 9:29am UTC](https://www.ruby-forum.com/t/coding-a-server/156913/2 "2009-01-05T09:29:04Z")

</div>

On Mon, Jan 05, 2009 at 04:24:02PM +0900, Vernier – wrote:

> Any ideas on how you would do this?  
> also, in cpp I’m using structs for the packet formats, but I have no  
> idea on how to do that in ruby

Take a look at packetfu [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/packetfu/)

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 9:52am UTC](https://www.ruby-forum.com/t/coding-a-server/156913/3 "2009-01-05T09:52:15Z")

</div>

Jeremy H. wrote:

> On Mon, Jan 05, 2009 at 04:24:02PM +0900, Vernier – wrote:
> 
> > Any ideas on how you would do this?  
> > also, in cpp I’m using structs for the packet formats, but I have no  
> > idea on how to do that in ruby
> 
> Take a look at packetfu [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/packetfu/)
> 
> enjoy,
> 
> -jeremy

the bindata dependency for packetfu is exactly what I was looking for  
for the structs, thanks a lot

---

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/coding-a-server/156913/4 "2009-01-05T18:45:07Z")

</div>

Vernier – wrote:

> the bindata dependency for packetfu is exactly what I was looking for  
> for the structs, thanks a lot

Another option:

[http://redshift.sourceforge.net/bit-struct/](http://redshift.sourceforge.net/bit-struct/)

I don’t know bindata, but from a quick glance at the docs I see that it  
does handle related fields, like length fields that specify the length  
of another field. BitStruct does _not_ do that.

Another difference is that BitStruct is a subclass of string, so it is  
very efficient to send a bitstruct to a socket or file or perform other  
string operations.

---

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/coding-a-server/156913/5 "2009-01-05T22:24:25Z")

</div>

Vernier – wrote:

> > very efficient to send a bitstruct to a socket or file or perform other  
> > modifications  
> > Exactly. If you have big packets, and only need to access a few fields  
> > (esp. fixed length), bit-struct is especially efficient. But if you want  
> > to use accessors a lot, then other libs might be better. With  
> > bit-struct, each field access may require pack/unpack, bit  
> > masking/shifting, etc.

---

<div class="post-metadata">

**Author:** ![vernier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ec9cab/32.png) [@vernier](https://www.ruby-forum.com/u/vernier)\
**Post date:** [January 5, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/coding-a-server/156913/6 "2009-01-05T21:46:42Z")

</div>

Joel VanderWerf wrote:

> Another option:
> 
> [BitStruct](http://redshift.sourceforge.net/bit-struct/)
> 
> I don’t know bindata, but from a quick glance at the docs I see that it  
> does handle related fields, like length fields that specify the length  
> of another field. BitStruct does _not_ do that.
> 
> Another difference is that BitStruct is a subclass of string, so it is  
> very efficient to send a bitstruct to a socket or file or perform other  
> string operations.

amazing performance, for creating 10k instances of the same simple  
struct (ruby 1.9 rc1):

```
         user system total real

```

bindata 3.681000 0.000000 3.681000 ( 3.827000)  
bit-struct 0.063000 0.000000 0.063000 ( 0.057000)

so bit-struct is actually a good choice for networking without any  
modifications
