Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4501 > unrolled thread

Ruby socket does not get reply

Started byRobert Garrido <rob_gar_esp@hotmail.com>
First post2011-05-13 12:19 -0500
Last post2011-05-15 19:10 -0500
Articles 8 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  Ruby socket does not get reply Robert Garrido <rob_gar_esp@hotmail.com> - 2011-05-13 12:19 -0500
    Re: Ruby socket does not get reply 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 13:08 -0500
    Re: Ruby socket does not get reply Roger Pack <rogerpack2005@gmail.com> - 2011-05-13 13:53 -0500
      Re: Ruby socket does not get reply 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 14:03 -0500
        Re: Ruby socket does not get reply Robert Garrido <rob_gar_esp@hotmail.com> - 2011-05-14 02:41 -0500
          Re: Ruby socket does not get reply 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-14 12:16 -0500
            Re: Ruby socket does not get reply 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-14 12:42 -0500
            Re: Ruby socket does not get reply Gary Wright <gwtmp01@mac.com> - 2011-05-15 19:10 -0500

#4501 — Ruby socket does not get reply

FromRobert Garrido <rob_gar_esp@hotmail.com>
Date2011-05-13 12:19 -0500
SubjectRuby socket does not get reply
Message-ID<7b38b4a345bec72155b081e9245f6dc1@ruby-forum.com>
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4507

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-13 13:08 -0500
Message-ID<6c8cc09cf7ee8143a513c4d57609a659@ruby-forum.com>
In reply to#4501
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4514

FromRoger Pack <rogerpack2005@gmail.com>
Date2011-05-13 13:53 -0500
Message-ID<773171f75133a8aed0af80a33887ad43@ruby-forum.com>
In reply to#4501
>     puts socket.read

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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4515

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-13 14:03 -0500
Message-ID<40591443021fa4cb22bcd0feb32c6fe6@ruby-forum.com>
In reply to#4514
Roger Pack wrote in post #998583:
>>     puts socket.read
>
> This call (read) blocks until the other end of the connection closes it

[toc] | [prev] | [next] | [standalone]


#4533

FromRobert Garrido <rob_gar_esp@hotmail.com>
Date2011-05-14 02:41 -0500
Message-ID<e14707455e856332a6b405e50cfead5b@ruby-forum.com>
In reply to#4515
This is the fix:

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

Thanks :D

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4541

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-14 12:16 -0500
Message-ID<ed295a788f4eb4306cf32f1a1001eedf@ruby-forum.com>
In reply to#4533
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4542

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-14 12:42 -0500
Message-ID<0eed02520a1b5c7e92ea3af9fe208943@ruby-forum.com>
In reply to#4541
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4580

FromGary Wright <gwtmp01@mac.com>
Date2011-05-15 19:10 -0500
Message-ID<F0EF654C-87A3-42ED-98B7-461BE351540D@mac.com>
In reply to#4541
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", 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 Wright

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web