Socket errors

B

Bruce Woodward

Howdy,

The documentation for Socket#accept gives an example of a ruby script
to bind to a socket and listen for incoming connections. Unfortunately
the script fails with the bind method.

Here is the script with the addition of the line to display the
returned value from the Socket.pack_sockaddr_in call.

#!/usr/bin/env ruby
# In one script, start this first
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
p sockaddr
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

Here is the output;

"\034\036\b\230\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000\000"
/r.rb:9:in `bind': Invalid argument - bind(2) (Errno::EINVAL)
from ./r.rb:9

bruce:~/tmp$ uname -aDarwin carlpowerbook 8.8.0 Darwin Kernel Version
8.8.0: Fri Sep 8 17:18:57 PDT 2006;
root:xnu-792.12.6.obj~1/RELEASE_PPC Power Macintosh powerpc
bruce:~/tmp$ ruby -vruby 1.8.5 (2006-08-25) [powerpc-darwin8.8.0]

I tried the same code on a Solaris 9 box at work and got a similar
error message.

However this code works with Linux (ubuntu) with the same version of
ruyb (1.8.5)

Has anyone heard of this or similar problems before?

thanks,
Bruce.
 

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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top