# Ruby socket does not get reply

**URL:** <https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943>\
**Category:** Ruby\
**Created:** [May 13, 2011, 7:19pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943 "2011-05-13T19:19:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 13, 2011, 7:19pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/1 "2011-05-13T19:19:50Z")

</div>

Hi all,

I use a TCP connection and send some data like this:

begin  
session = “mysession”  
socket = TCPSocket.new(tcpaddress, port)  
socket.setsockopt(Socket::IPPROTO\_TCP, Socket::TCP\_NODELAY, 1)  
puts "sending to socket HELO " + session  
socket.write ('HELO ’ + session)  
puts socket.read  
socket.close  
rescue Exception =\> myException  
puts “Exception rescued : #{myException}”  
end

The socket never gets a reply, however telnet does:

$ telnet some\_ip port  
Trying some\_ip…  
Connected to some\_ip  
Escape character is ‘^]’.  
HELO mysession  
OK

As you can see the remote server replies “OK” as expected. What’s wrong?

Thanks in advanced!

PS: I have used puts and send methods also

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 13, 2011, 7:55pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/2 "2011-05-13T19:55:55Z")

</div>

I think you may want TCPServer, instead of TCPSocket

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 13, 2011, 8:53pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/3 "2011-05-13T20:53:09Z")

</div>

> ```
> puts socket.read
> 
> ```

This call (read) blocks until the other end of the connection closes it.  
-r

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 13, 2011, 9:03pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/4 "2011-05-13T21:03:19Z")

</div>

Roger P. wrote in post #998583:

> > ```
> > puts socket.read
> > 
> > ```
> 
> This call (read) blocks until the other end of the connection closes it.  
> -r

Good one. From IO#read docs:

===  
ios.read([length [, buffer]]) → string, buffer, or nil

# If length is omitted or is nil, it reads until EOF…

And when the server closes the socket, ruby sends an EOF signal to the  
other side.

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 14, 2011, 9:41am UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/5 "2011-05-14T09:41:03Z")

</div>

This is the fix:

socket.write("HELO " + session + “\r\n”)

Thanks 😃

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 14, 2011, 7:16pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/6 "2011-05-14T19:16:27Z")

</div>

Robert Garrido wrote in post #998661:

> This is the fix:
> 
> socket.write("HELO " + session + “\r\n”)
> 
> Thanks 😃

Just be aware that ruby is going to convert a “\n” to “\r\n” on windows,  
so on windows you will actually be wrting:

“\r\n”  
= “\r” + “\n”  
= “\r” + “\r\n”  
= “\r\r\n”

That may or may not make a difference to a particular program.

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 13, 2011, 8:07pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/7 "2011-05-13T20:07:59Z")

</div>

Robert Garrido wrote in post #998563:

> As you can see the remote server replies “OK” as expected. What’s wrong?

If the server is expecting ‘line oriented input’, then the server will  
continue trying to read from the socket until it encounters a newline,  
i.e. you need to end the data you send with a newline.

When you use sockets, both sides have to communicate with an agreed upon  
protocol. If one side deviates from that protocol, then you are going  
to end up with deadlock, where both sides are waiting for the other side  
to do something.

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 16, 2011, 2:11am UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/8 "2011-05-16T02:11:23Z")

</div>

On May 14, 2011, at 1:16 PM, 7stud – wrote:

> Just be aware that ruby is going to convert a “\n” to “\r\n” on windows,  
> so on windows you will actually be wrting:
> 
> “\r\n”  
> = “\r” + “\n”  
> = “\r” + “\r\n”  
> = “\r\r\n”
> 
> That may or may not make a difference to a particular program.

I don’t have a Windows system to test on but I’m pretty sure that  
sockets are always automatically opened in binary mode. On my Mac OS  
system:

irb\> socket = TCPSocket.new(“[www.google.com](http://www.google.com)”, 80)  
=\> #\<TCPSocket:fd 4\>  
irb \> socket.binmode?  
=\> true

The HTTP protocol requires “\r\n” line endings though so they have to be  
written explicitly on the binary socket as described in the earlier  
messages.

Gary W.

---

<div class="post-metadata">

**Author:** ![sovok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sovok](https://www.ruby-forum.com/u/sovok)\
**Post date:** [May 14, 2011, 7:42pm UTC](https://www.ruby-forum.com/t/ruby-socket-does-not-get-reply/206943/9 "2011-05-14T19:42:16Z")

</div>

7stud – wrote in post #998709:

> That may or may not make a difference to a particular program.

One way to avoid the newline conversion is to use the actual ascii code  
for a newline instead of “\n”:

“\r\n” in octal:

“\015\012”

“\r\n” in hex:

“\x0D\x0A”

To avoid having to put those ugly escape sequences in your string, the  
perl socket library has the constant $CRLF, which can be used at the end  
of strings when correct reading of the data requires that line endings  
be marked with exactly one “\r” and one “\n”. However, I don’t think  
ruby copied that feature from perl.
