PDA

View Full Version : Ruby: Socket woes


Skorche
2004.06.26, 03:05 AM
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.

anarchie
2004.06.26, 04:25 AM
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.

Skorche
2004.06.26, 01:16 PM
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.