I am trying to setup a simple UDP client and server using Ruby. The code looks like this:
require 'socket.so'
class UDPServer
def initialize(port)
@port = port
end
def start
@socket = UDPSocket.new
@socket.bind(nil, @port) # is nil OK here?
while true
packet = @socket.recvfrom(1024)
puts packet
end
end
end
server = UDPServer.new(4321)
server.start
This is the client:
require 'socket.so'
class UDPClient
def initialize(host, port)
@host = host
@port = port
end
def start
@socket = UDPSocket.open
@socket.connect(@host, @port)
while true
@socket.send("otiro", 0, @host, @port)
sleep 2
end
end
end
client = UDPClient.new("10.10.129.139", 4321) # 10.10.129.139 is the IP of UDP server
client.start
Now, I have two VirtualBox machines running Linux. They are in the same network, they can ping to each other.
But when I start the UDP server on machine A, and then try to run the UDP client on machine B I get the following error:
client.rb:13:in `send': Connection refused - sendto(2) (Errno::ECONNREFUSED)
I suspect that the error is in the bind method on the server. I don't know which address I should specify there. I read somewhere that you should use the address of your LAN/WAN interface, but I don't how to obtain that address.
Can anyone help me with this one?