Socket in Ruby


(Cedric Foll) #1

Hi,

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

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

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

i’ve wrote the folowing ugly Ruby code:

s=Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)

build the address of the remote machine

sockaddr_server=[Socket::AF_INET, 80,
Socket.gethostbyname(‘www.ruby-lang.org’)[3], 0, 0
].pack(“snA4NN”)
#connect
begin
s.connect(sockaddr_server)
rescue
print "error: #{$!}\n"
end

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

Regards


Cédric Foll


(ts) #2

s=Socket.new(Socket::AF_INET, 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).