ruby socket programming

M

monarailguy42

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
 
L

Luis Parravicini

# ... 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?
 
R

Rolando Abarca

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

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

bino_oetomo

Dear All

----- Original Message -----
From: "Luis Parravicini" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Wednesday, May 23, 2007 11:10 PM
Subject: Re: ruby socket programming

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-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top