Ruby: Socket woes

Sage
Posts: 1,482
Joined: 2002.09
Post: #1
So I've started dabbling around with Ruby, and it seems like an awesome language.

Upon seeing it's rich and easy socket library, I decided that this might be a good time to learn how to use UDP. Instead of an easy time, I seem to have stumbled onto some really confusing errors...

calling: aSocket.bind('domain name', port) only works if you use the local machine for the domain name. Otherwise it gives an error saying that the name can't be found. Messages sent from the local machine have the ip address 127.0.0.1 when the machine's ip address is really 192.168.1.100. If you use the real ip address for the DN, it doesn't work, but the 127 address works. Calling the send method to a remote machine works, but the same name used in bind gives errors.

Also, IPSocket.getaddress('domain name') is supposed to return the ip address, but instead returns the ipv6 address.

Is there something particularly important point that I'm missing? I can post code if needed.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
anarchie
Unregistered
 
Post: #2
The important point that you're missing is that bind() can only bind sockets to ports on the local machine, as with TCP. If you want a connection to a port on a remote machine, use connect() or send(). For UDP, the only benefit of connect() is that you don't need to specify the remote machine's address for each call of send().

Whenever you send data from the local machine to itself, it uses the loopback network interface instead of any physical (ethernet or otherwise) network interface. The loopback interface always has the address 127.0.0.1. Using this virtual interface saves the overhead of making the ethernet controller talk to itself over the network.

NB: I've never used Ruby sockets, this is just my knowledge of BSD sockets speaking.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
anarchie Wrote:The important point that you're missing is that bind() can only bind sockets to ports on the local machine, as with TCP. If you want a connection to a port on a remote machine, use connect() or send(). For UDP, the only benefit of connect() is that you don't need to specify the remote machine's address for each call of send().

Whenever you send data from the local machine to itself, it uses the loopback network interface instead of any physical (ethernet or otherwise) network interface. The loopback interface always has the address 127.0.0.1. Using this virtual interface saves the overhead of making the ethernet controller talk to itself over the network.

NB: I've never used Ruby sockets, this is just my knowledge of BSD sockets speaking.

I figured out that you have to use the local machines address for bind. The fact that it allowed it to be changed made me assume you had to set the intended sender as well. I also eventually figured that the 127 address must be a special one, but I thought that the loopback address was 0.0.0.0. So I think that I've got bind covered. (I think)

Problem still stands that it doesn't seem to receive packets from remote machines. It gives no errors when sending, so I don't know even know where to start. If you enter an invalid name or ip for connect to use, it does give an error. So I'm not even sure what end the problem is on.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ruby on Rails setting Time to NULL BeyondCloister 3 3,792 Nov 21, 2007 09:56 AM
Last Post: AndyKorth
  Ruby, MacPorts, Firewalls BeyondCloister 3 4,008 Nov 6, 2007 04:57 PM
Last Post: OneSadCookie
  Ruby: Resources for Learning iefan 10 6,817 Jul 4, 2007 08:54 AM
Last Post: ALX99066
  Ruby Gems and OpenGL Jaden 2 3,027 Apr 2, 2007 01:15 AM
Last Post: Jaden
  Embedding Ruby DesertPenguin 3 3,352 Jan 5, 2007 09:11 PM
Last Post: DesertPenguin