Gen_udp Messages - Getting Peer Address

Hey guys,

I have a UDP server listening for connections. When receiving a message from the handle_info, ie:

def handle_info({:udp, socket, ip, in_port, packet}, state) do

the ip and in_port are the IP and port of the server that the socket is listening on ( I expected it to be the IP and port of the peer sending messages.

How do I get the IP and port of the sender?

Many thanks,

Should be able to call :inet.peername(socket)

Okay, actually, using :inet:peername(socket) returns {:error, :enotconn} because the socket is passive.

Ah, too bad. I have an old udp server and I was able to use the ip and port from the handle_info to reply to the client, but that was on a much older version of erlang and elixir, not sure if something changed, or it was the way my network interface was configured.

To be sure, your server and client are on different physical machines, correct?

1 Like

Strange, the documentation says that the IP and port are the ones of the sender of the packet:

IP and InPortNo define the address from which Packet comes. Packet is a list of bytes if option list is specified. Packet is a binary if option binary is specified.

@LSylvester did you manage to solve this?