# How to get bitmap file via ethernet

**URL:** <https://www.ruby-forum.com/t/how-to-get-bitmap-file-via-ethernet/61687>\
**Category:** Ruby\
**Created:** [May 26, 2006, 3:40am UTC](https://www.ruby-forum.com/t/how-to-get-bitmap-file-via-ethernet/61687 "2006-05-26T03:40:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@harry](https://www.ruby-forum.com/u/harry)\
**Post date:** [May 26, 2006, 3:40am UTC](https://www.ruby-forum.com/t/how-to-get-bitmap-file-via-ethernet/61687/1 "2006-05-26T03:40:06Z")

</div>

I have connected a PC (Windows2000) to a camera controller and I want  
to get image data from the controller.  
Two ports are used for this. Port 8500 is for text commands and text  
replies.  
Port 8501 is used for image data.  
When I request image data it streams to the PC via ethernet (8501) in  
bitmap format.

I have no problem with the text commands and replies but catching the  
binary data confuses me. I would like to get the data in an array and  
then write it to a bitmap file. Getting it into an array at this point  
has me stuck.

####################################  
require ‘socket’

ip\_address = “xxx.xxx.xxx.xxx”  
text\_port = 8500  
comm\_io = TCPSocket.new(ip\_address,text\_port) # for CCD controller text

image\_port = 8501  
image\_io = TCPSocket.new(ip\_address,image\_port) # for binarydata

outmsg = ‘my\_command’.upcase + “\r”  
comm\_io.print outmsg

inmsg = comm\_io.gets.chomp.split(/,/)

####################################

Any help appreciated.

Harry

---

<div class="post-metadata">

**Author:** ![harry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@harry](https://www.ruby-forum.com/u/harry)\
**Post date:** [May 26, 2006, 4:33am UTC](https://www.ruby-forum.com/t/how-to-get-bitmap-file-via-ethernet/61687/2 "2006-05-26T04:33:05Z")

</div>

On 5/25/06, Harry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has me stuck.  
> How much do you know about the protocol used for transmitting the  
> bitmap data? In my experience when you are reading binary data from a  
> socket there is usually a length indicator at the beginning to tell  
> you how much to read. So for example you would read 4 bytes and then  
> use String#unpack to turn those bytes into a length, which would be  
> how much you would read next for the rest of the data. For example, I  
> just wrote this server which serves up a small JPG file:

require ‘socket’

img = nil  
File.open(‘tp.jpg’, ‘rb’) do |f|  
img = f.read  
end

server = TCPServer.new(‘localhost’, 7778)  
while (session = server.accept)  
session.write([img.length].pack(‘N’))  
session.write(img)  
end

Then I wrote this client to read it (partially based on your code):

require ‘socket’

image\_port = 7778  
image\_io = TCPSocket.new(‘localhost’, image\_port)  
len = image\_io.read(4).unpack(‘N’)[0]  
img = image\_io.read(len)  
File.open(‘tp2.jpg’, ‘wb’) do |f|  
f.write(img)  
end

This works fine. I suspect you will need to do something similar.

Ryan

---

<div class="post-metadata">

**Author:** ![harry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@harry](https://www.ruby-forum.com/u/harry)\
**Post date:** [May 26, 2006, 10:28am UTC](https://www.ruby-forum.com/t/how-to-get-bitmap-file-via-ethernet/61687/3 "2006-05-26T10:28:21Z")

</div>

On 5/26/06, Ryan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

That was it.  
At the beginning of the data was the length indicator (and some other  
data). Thanks to you, I was able to capture the image data before the  
end of the day Friday and can get on with my code next week.

Thank you.
