# Writing a 4 byte Integer to socket

**URL:** <https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423>\
**Category:** ruby-talk\
**Created:** [30 March 2003 17:49 UTC](https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423 "2003-03-30T17:49:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele\_renzi4](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gabriele\_renzi4](https://rubytalk.org/u/gabriele_renzi4)\
**Post date:** [30 March 2003 17:49 UTC](https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423/1 "2003-03-30T17:49:56Z")

</div>

I need to write a value (it should be a Fixnum, but I’m not sure) over  
a TCPSocket.  
How can I write it in a typical “4 bytes, network byte order” style?

And what if I need to compose a byte and send it over the network?  
Is BitVector the only choice?

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [30 March 2003 18:08 UTC](https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423/2 "2003-03-30T18:08:49Z")

</div>

Hi,

> I need to write a value (it should be a Fixnum, but I’m not sure) over  
> a TCPSocket.  
> How can I write it in a typical “4 bytes, network byte order” style?
> 
> And what if I need to compose a byte and send it over the network?  
> Is BitVector the only choice?

Array#pack is probably what you’re looking for…  
[http://www.rubycentral.com/ref/ref\_c\_array.html#pack](http://www.rubycentral.com/ref/ref_c_array.html#pack)  
(the table showing the template characters for pack is a  
few pages up in that document… search for “Template  
characters for Array#pack”)

Hope this helps,

Bill

> **···**
>
> From: “gabriele renzi” [surrender\_it@rc1.vip.lng.yahoo.com](mailto:surrender_it@rc1.vip.lng.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [30 March 2003 21:52 UTC](https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423/3 "2003-03-30T21:52:42Z")

</div>

For an example of this in real code, install druby and look at drb/drb.rb  
functions ‘dump’ and ‘load’, which send or receive a marshalled object  
preceded by a 4-byte size value:

```
def dump(obj)
  obj = DRbObject.new(obj) if obj.kind_of? DRbUndumped
  begin
    str = Marshal::dump(obj)
  rescue
    str = Marshal::dump(DRbObject.new(obj))
  end
  [str.size].pack('N') + str
end

def load(soc)
  sz = soc.read(4) # sizeof (N)
  raise(DRbConnError, 'connection closed') if sz.nil?
  raise(DRbConnError, 'premature header') if sz.size < 4
  sz = sz.unpack('N')[0]
  raise(DRbConnError, "too large packet #{sz}") if @load_limit < sz
  str = soc.read(sz)
  ... snip rest

```

Regards,

Brian.

> **···**
>
> On Mon, Mar 31, 2003 at 03:08:49AM +0900, Bill Kelly wrote:
> 
> > From: “gabriele renzi” [surrender\_it@rc1.vip.lng.yahoo.com](mailto:surrender_it@rc1.vip.lng.yahoo.com)
> > 
> > > I need to write a value (it should be a Fixnum, but I’m not sure) over  
> > > a TCPSocket.  
> > > How can I write it in a typical “4 bytes, network byte order” style?
> > > 
> > > And what if I need to compose a byte and send it over the network?  
> > > Is BitVector the only choice?
> > 
> > Array#pack is probably what you’re looking for…  
> > [http://www.rubycentral.com/ref/ref\_c\_array.html#pack](http://www.rubycentral.com/ref/ref_c_array.html#pack)  
> > (the table showing the template characters for pack is a  
> > few pages up in that document… search for “Template  
> > characters for Array#pack”)

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi4](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gabriele\_renzi4](https://rubytalk.org/u/gabriele_renzi4)\
**Post date:** [30 March 2003 22:31 UTC](https://rubytalk.org/t/writing-a-4-byte-integer-to-socket/5423/4 "2003-03-30T22:31:39Z")

</div>

thanks Brian and Bill !  
that is actually what I need.

I’d never thought about looking at drb, I was thinking about looking  
at net/\* but the protocols are all characteri-oriented, and I need  
to write a client for a packet based protocol…

> **···**
>
> il Mon, 31 Mar 2003 06:52:42 +0900, Brian Candler [B.Candler@pobox.com](mailto:B.Candler@pobox.com) ha scritto::
> 
> > For an example of this in real code, install druby and look at drb/drb.rb  
> > functions ‘dump’ and ‘load’, which send or receive a marshalled object  
> > preceded by a 4-byte size value:
