# Ruby socket programming

**URL:** <https://www.ruby-forum.com/t/ruby-socket-programming/104900>\
**Category:** Ruby\
**Created:** [May 23, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/ruby-socket-programming/104900 "2007-05-23T17:55:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [May 23, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/ruby-socket-programming/104900/1 "2007-05-23T17:55:35Z")

</div>

Hello I am trying to write a simple ruby socket program. I am trying  
to communicate to a device that once I establish a TCPSocket  
connection I can pass it sql commands and it will return me data.  
Here is what I have been trying.

require ‘socket’  
begin  
t = TCPSocket.new(‘192.168.0.71’, 8080)  
rescue  
puts “error: #{$!}”  
else  
# … do something with the socket  
t.write(“select read\_count from tag\_id”)  
answer = t.gets  
puts answer  
# and terminate the connection when we’re done  
t.close  
end

My answer contains nothing, so I know I must be doing something  
wrong. The device I am communication to is a ThingMagic RFID M4 and  
the manufacturer assures me that once a TCP Socket connection is  
established that passing queries as above will give back data. I know  
that the device is working from other tests. Any help would be  
appreciated.  
Thanks  
Larry

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [May 23, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/ruby-socket-programming/104900/2 "2007-05-23T22:07:14Z")

</div>

On May 23, 2007, at 11:54 AM, monarailguy42 wrote:

> else  
> # … do something with the socket  
> t.write(“select read\_count from tag\_id”)

did you try with a t.flush or maybe a t.puts("…")?..

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [May 23, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/ruby-socket-programming/104900/3 "2007-05-23T18:11:38Z")

</div>

On 5/23/07, monarailguy42 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> # ... do something with the socket
> 
> ```
> 
> t.write(“select read\_count from tag\_id”)  
> answer = t.gets

How the device knows the data you wrote is the complete query?  
Shouldn’t you send some mark (newline, ‘;’ or something like that) to  
let the device knows where the query ends?

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [May 24, 2007, 5:06am UTC](https://www.ruby-forum.com/t/ruby-socket-programming/104900/4 "2007-05-24T05:06:50Z")

</div>

Dear All

----- Original Message -----  
From: “Luis P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, May 23, 2007 11:10 PM  
Subject: Re: ruby socket programming

> On 5/23/07, monarailguy42 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > # ... do something with the socket
> > 
> > ```
> > 
> > t.write(“select read\_count from tag\_id”)  
> > answer = t.gets
> 
> How the device knows the data you wrote is the complete query?  
> Shouldn’t you send some mark (newline, ‘;’ or something like that) to  
> let the device knows where the query ends?

I Tried with socket server script from

and the client is :  
—Start----  
#!/usr/local/bin/ruby  
require ‘socket’  
aa=0  
loop do  
aa = aa+1  
sock = TCPSocket.new(‘127.0.0.1’, 20000)  
sendit = “We send #{aa}”  
sock.send(sendit,0)  
str = sock.recv(100)  
puts ‘we received #{str}’  
sock.close  
sleep (1)  
end  
—Stop----

Work fine for me

But … i think it’ll be better if the device can act as client, and  
always  
send all data in comma delimeted , so the server (we , user) can just  
wait  
for data and parse received data easily.  
Just like what Barcode reader do with keyboard port

Sincerely  
-bino-
