Best method to use TCPSocket

A

Adam Bouchard

Hello,

I'm having an issue getting an expected response via TCPSocket. My goal
is to connect to a remote controller device and access XML data.

The following code properly authenticates a connection, however I am not
getting the response I expect.

CODE USED:

require "socket"

#set proxy server
host = "host" #removed specifics
port = "port" #removed specifics

#set parameters for send message
user = "XXX" #removed specifics
pass = "XXX" #removed specifics
mac = "XXX" #removed specifics
limit = 10
start_time = "20100910_120000"
end_time = "20100910_123000"

#format the send message required
message = "User: #{user}
Pass: #{pass}
MAC: #{mac}
Limit: #{limit}
Start: #{start_time}
End: #{end_time}
Finish"

#open TCP socket
s = TCPSocket.open(host, port)

#send the message to controller
s.write(message)

#read and print the response (specify number of bytes)
xml = s.recv(1000)
puts xml

#close the socket
s.close

This code outputs:
"api.heliodyne.com at your service, [draker.labs]"


However, the manufacturer is showing the XML that I should be able to
access in his debug file

"SensorQueryReader] constructBean Finish
2010-09-15 13:57:06,473 DEBUG [Thread-18837]
[heliodyne.data.impl.SensorQueryReader] constructBean draker.labs was
authenticated
2010-09-15 13:57:06,478 INFO [Thread-18837]
[heliodyne.data.impl.SensorQueryWriter] writeXML Found 2 data sets for
00:90:c2:db:7a:e8 from 2010-09-10 12:00:00.0 to 2010-09-10 12:30:00.0
2010-09-15 13:57:06,478 INFO [Thread-18837]
[heliodyne.data.impl.SensorQueryWriter] writeXML
<Set><ReadingTime>2010-09-10 12:19:01.0</ReadingTime><T1
units='F'>197.64</T1><T2 units='F'>154.8</T2><T3 units='F'>160.4</T3><T4
units='F'>159.04</T4><T5 units='F'>165.3</T5><T6 units='F'>164.2</T6><T7
units='F'>181.95</T7><VFSFlow units='GPM'>17.58</VFSFlow><PulseFlow
units='GPM'>0.0</PulseFlow><Pressure units='PSI'>59.54</Pressure><Energy
units='kBTU'>138.90959</Energy></Set>
2010-09-15 13:57:06,478 INFO [Thread-18837]
[heliodyne.data.impl.SensorQueryWriter] writeXML Finished writing XML
2010-09-15 13:57:06,478 INFO [Thread-18837]
[heliodyne.data.socket.SocketHandler] run Connection 143 closed"


Any ideas why I'm not able to see the outputed XML like shown in the
debug message? Any help is much appreciated.

Thanks,

Adam
 
B

Brian Candler

Don't use recv on a TCP socket.

* Use read(n) if you know you need to read exactly n bytes.
* Use read() if you want to read until the other end closes the socket.
* Use gets() if you want to read up to the next end-of-line.

As a last resort, you can use readpartial(n) to get however many bytes
are available in the receive buffer at the time, between 1 and n. But
you'll need to repeat this until you're sure you have the complete
message.

Which to use depends on how the other end frames its response. I'd try
read(), on the assumption that the other end sends its response and then
closes the socket.
 

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,795
Messages
2,569,644
Members
45,358
Latest member
TreyTritt8

Latest Threads

Top