SlySherZ
SlySherZ

Reputation: 1661

How to bind sockets in Ruby?

I'm tring to use ruby Socket class directly, TCPSocket is not an option. Code:

require 'socket'
include Socket::Constants

socket = Socket.new :INET, :STREAM, 0
socket.bind (Socket.pack_sockaddr_in 2200, 'localhost')

Error:

`bind': An address incompatible with the requested protocol was used. (Errno::EAFNOSUPPORT)

I'm using ruby 1.9.3 in a Windows machine.

Extra info:

Socket.pack_sockaddr_in(2200, 'localhost')

=> "\x17\x00\b\x98\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"

Hosts file:

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

Upvotes: 3

Views: 645

Answers (1)

Rico
Rico

Reputation: 61551

Can you try:

require 'socket'
include Socket::Constants

socket = Socket.new :INET, :STREAM, 0
socket.bind (Socket.pack_sockaddr_in 2200, '127.0.0.1')

Could be that localhost is returning your IPv6 address and not taking it.

Upvotes: 2

Related Questions