# Best method to use TCPSocket

**URL:** <https://www.ruby-forum.com/t/best-method-to-use-tcpsocket/195057>\
**Category:** Ruby\
**Created:** [September 15, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/best-method-to-use-tcpsocket/195057 "2010-09-15T23:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [September 15, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/best-method-to-use-tcpsocket/195057/1 "2010-09-15T23:45:48Z")

</div>

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](http://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  
2010-09-10 12:19:01.0197.64154.8160.4159.04165.3164.2181.9517.580.059.54138.90959  
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

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [September 16, 2010, 10:01am UTC](https://www.ruby-forum.com/t/best-method-to-use-tcpsocket/195057/2 "2010-09-16T10:01:01Z")

</div>

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.
