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