Ruby Socket: I found a misstake in the doc. Can someone confirm itand who's gonna fix this doc-error

K

kazaam

Hi,
I'm playing around with sockets in ruby and took a look at the docs(http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/index.html). There are some examples like this here(http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/classes/Socket.html#M004166):

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client, client_sockaddr = socket.accept
puts "The client said, '#{socket.readline.chomp}'"
client.puts "Hello from script one!"
socket.close

But that example is wrong! It has to be client.readline.chomp and not socket.readline.chomp. Take this example by me:

#!/usr/bin/env ruby

$Verbose=true

require 'socket'

mysocket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socketaddress = Socket.pack_sockaddr_in( 2201, 'localhost' )
mysocket.bind(socketaddress)
mysocket.listen( 5 )
client, client_sockaddr = mysocket.accept
puts client.readline.chomp #reading from the client fd
mysocket.close

execute it and connect with netcat: "nc localhost 2201" , now type something in netcat and you will see everything is fine but now let's change it to the doc way:

#!/usr/bin/env ruby

$Verbose=true

require 'socket'

mysocket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socketaddress = Socket.pack_sockaddr_in( 2201, 'localhost' )
mysocket.bind(socketaddress)
mysocket.listen( 5 )
client, client_sockaddr = mysocket.accept
puts mysocket.readline.chomp #reading from the socket fd
mysocket.close

If you connect now to it with nc the ruby-script will crash:
../sockettest.rb:13:in `readline': Transport endpoint is not connected (Errno::ENOTCONN)
from ./sockettest.rb:13


Can someone confirm it and who's gonna fix this doc-error?
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top