Forum: Ruby Socket: Getting EADDRNOTAVAIL in bind method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anukul S. (Guest)
on 2009-02-02 23:50
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 #<Socket:0x2b85694>)

     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
Sean O. (Guest)
on 2009-02-03 02:00
(Received via mailing list)
On Mon, Feb 2, 2009 at 9:50 PM, Anukul S.
<removed_email_address@domain.invalid> wrote:
>     puts socket.inspect  (returns #<Socket:0x2b85694>)
> 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-Programm....

HTH

Regards,
Sean
Gary W. (Guest)
on 2009-02-03 02:14
(Received via mailing list)
On Feb 2, 2009, at 4:50 PM, Anukul S. wrote:
>
> 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.


Usually you don't pick the local address to bind to but instead
let the system pick it. Is 114.23.42.42 your local address or is
the address of the device you are trying to connect to?

If you are trying to bind a local socket to the address of the
remote host/device you'll get EADDRNOTAVAIL since the address
doesn't belong to the local host.

Gary W.
Anukul S. (Guest)
on 2009-02-03 04:19
Gary, thanks for your reply. 114.23.42.42 is the address of my device
which is not on my local system. My code works fine till I connect to
the socket, but after that I need to receive buffer from it, for which I
am not able to use the "recvfrom" method. So, I tried to use "bind"
method which did not work (since the device is remote but I am able to
ping it from my local machine and it is also on the same network). Is it
some way I can receive data from the socket after I connect to it?

Thanks,
Anukul

Gary W. wrote:
> On Feb 2, 2009, at 4:50 PM, Anukul S. wrote:
>>
>> 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.
>
>
> Usually you don't pick the local address to bind to but instead
> let the system pick it. Is 114.23.42.42 your local address or is
> the address of the device you are trying to connect to?
>
> If you are trying to bind a local socket to the address of the
> remote host/device you'll get EADDRNOTAVAIL since the address
> doesn't belong to the local host.
>
> Gary W.
Anukul S. (Guest)
on 2009-02-03 19:02
Gary,

I tried using read, recvfrom, sysread and recv to read the data from the
remote device using socket but my program does not terminate at all.
Here is the code snippet that I am trying:

require 'socket'
include Socket::Constants

 socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
 puts socket.inspect
 sockaddr = Socket.pack_sockaddr_in(5555, '143.91.34.205')
 puts sockaddr.inspect (returns
                 "\002\000\025\263\217[\"\315\000\000\000\000\000\000\000\000")

 puts sockaddr.length  # returns 16

 sock = socket.connect(sockaddr)
 puts sock.inspect # returns 0
 receive = socket.read(16) #does not return at all,unless forcefully
terminated
 puts receive.inspect

Is there something I am missing here, or is there any other way to
retrieve data? Basically, I need to read an image buffer (size 704 x
480)

Thanks,
Anukul

Anukul S. wrote:
> Gary, thanks for your reply. 114.23.42.42 is the address of my device
> which is not on my local system. My code works fine till I connect to
> the socket, but after that I need to receive buffer from it, for which I
> am not able to use the "recvfrom" method. So, I tried to use "bind"
> method which did not work (since the device is remote but I am able to
> ping it from my local machine and it is also on the same network). Is it
> some way I can receive data from the socket after I connect to it?
>
> Thanks,
> Anukul
>
> Gary W. wrote:
>> On Feb 2, 2009, at 4:50 PM, Anukul S. wrote:
>>>
>>> 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.
>>
>>
>> Usually you don't pick the local address to bind to but instead
>> let the system pick it. Is 114.23.42.42 your local address or is
>> the address of the device you are trying to connect to?
>>
>> If you are trying to bind a local socket to the address of the
>> remote host/device you'll get EADDRNOTAVAIL since the address
>> doesn't belong to the local host.
>>
>> Gary W.
This topic is locked and can not be replied to.