From: Sean O'Halpin Date: 2009-02-03T08:59:56+09:00 Subject: Re: Socket: Getting EADDRNOTAVAIL in bind method On Mon, Feb 2, 2009 at 9:50 PM, Anukul Singhal wrote: > Hi, > > I am new to Socket programming in ruby and want to connect to a device. > I am able to make a connection using the following code but failing in > the bind method. Here's the snippet: > > require 'socket' > include Socket::Constants > > socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) > puts socket.inspect (returns #) > > sockaddr = Socket.pack_sockaddr_in(5555,'114.23.42.42') > puts sockaddr.inspect (returns > "\002\000\025\263\217[\"\315\000\000\000\000\000\000\000\000") > > bound = socket.bind(sockaddr) > puts bound.inspect > > In the last method bind, getting the following error: > > Test.rb:50:in `bind': The requested address is not valid in its context. > - bind(2) (Errno::EADDRNOTAVAIL) > > I think I might be missing something elementary here. If anyone can > please help with it, would be really grateful. > > Thanks, > Anukul I don't think it's you - the docs on socket programming are a little frustrating. The examples in the rubydocs don't work out of the box on my Mac for instance. You could try this: require 'ipaddr' sockaddr = Socket.pack_sockaddr_in( 5555, IPAddr.new('114.23.42.42').to_i) You might also want: socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true ) There's a very good tutorial on socket programming in Ruby at http://www.scribd.com/doc/3499561/Sockets-Programming-in-Ruby. HTH Regards, Sean