Socket in Ruby

(Cedric Foll) #1


i’m working n the pleac web site ( The idea of this
web site is to have a “cook book” (like the perl’s one) for a lot of

I’m trying to wrote the Ruby’s part about sockets.
But i’ve failed to find an easy way to use low level socket API.
For example for the folowing Perl code:


socket(TO_SERVER, PF_INET, SOCK_STREAM, getprotobyname(‘tcp’));

build the address of the remote machine

$internet_addr = inet_aton($remote_host)
or die “Couldn’t convert $remote_host into an Internet address: $!\n”;
$paddr = sockaddr_in($remote_port, $internet_addr);


connect(TO_SERVER, $paddr)
or die “Couldn’t connect to $remote_host:$remote_port : $!\n”;

i’ve wrote the folowing ugly Ruby code:, Socket::SOCK_STREAM, 0)

build the address of the remote machine

sockaddr_server=[Socket::AF_INET, 80,
Socket.gethostbyname(‘’)[3], 0, 0
print "error: #{$!}\n"

How can i manage low level socket without this ‘pack(“snA4NN”)’ ?


Cédric Foll

(ts) #2, Socket::SOCK_STREAM, 0)

Why you don't use TCPSocket ?

Guy Decoux

(Cedric Foll) #3

Why you don’t use TCPSocket ?
Because i need to use low level API. There are some kind of thing that
you can only do by using a low level API (the same as the C one).