Ruby socket does not get reply

R

Robert Garrido

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
 
7

7stud --

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

Roger Pack

puts socket.read

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

7stud --

Robert Garrido wrote in post #998661:
This is the fix:

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

Thanks :D

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.
 
7

7stud --

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 actualy ascii code
for a newline instead of "\n":


"\r\n" in octal:

"\015\012"

"\r\n" in hex:

"x0D\x0A"

To avoid the ugliness of those escape sequences, perl has the constant
CRLF, which can be used in strings, when correct reading of the data
requires that line endings be marked by exactly one "\r" and one "\n".
However, I don't think ruby copied that feature from perl.
 
G

Gary Wright

Just be aware that ruby is going to convert a "\n" to "\r\n" on = windows,=20
so on windows you will actually be wrting:
=20
"\r\n"
=3D "\r" + "\n"
=3D "\r" + "\r\n"
=3D "\r\r\n"
=20
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 =3D TCPSocket.new("www.google.com", 80)
=3D> #<TCPSocket:fd 4>=20
irb > socket.binmode?
=3D> true=20

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 Wright=
 

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