[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Socket: Getting EADDRNOTAVAIL in bind method

Anukul Singhal

2/2/2009 9:50:00 PM

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
--
Posted via http://www.ruby-....

4 Answers

Sean O'Halpin

2/3/2009

0

On Mon, Feb 2, 2009 at 9:50 PM, Anukul Singhal <anukul.singhal@gmail.com> 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 #<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

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-Programmi....

HTH

Regards,
Sean

Gary Wright

2/3/2009 12:14:00 AM

0


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




Anukul Singhal

2/3/2009 2:20:00 AM

0

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

--
Posted via http://www.ruby-....

Anukul Singhal

2/3/2009 5:02:00 PM

0

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

--
Posted via http://www.ruby-....