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.
F8ab7bab2f3904f7eeb1829543668ed8?d=identicon&s=25 Anukul Singhal (anukulsinghal)
on 2009-02-02 22: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
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2009-02-03 01:00
(Received via mailing list)
On Mon, Feb 2, 2009 at 9:50 PM, Anukul Singhal
<anukul.singhal@gmail.com> 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-02-03 01:14
(Received via mailing list)
On Feb 2, 2009, at 4:50 PM, Anukul Singhal 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 Wright
F8ab7bab2f3904f7eeb1829543668ed8?d=identicon&s=25 Anukul Singhal (anukulsinghal)
on 2009-02-03 03: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 Wright wrote:
> On Feb 2, 2009, at 4:50 PM, Anukul Singhal 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 Wright
F8ab7bab2f3904f7eeb1829543668ed8?d=identicon&s=25 Anukul Singhal (anukulsinghal)
on 2009-02-03 18: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 Singhal 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 Wright wrote:
>> On Feb 2, 2009, at 4:50 PM, Anukul Singhal 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 Wright
This topic is locked and can not be replied to.