[erlang-questions] Matching IP address in socket module

Thomas Pircher thp+erlang@REDACTED
Thu Oct 24 12:04:44 CEST 2019


Hi,

I'm trying to use the new socket module, and I'm having difficulties
extracting the IP address from the socket:sockaddr_in4 record, as
returned from socket:recvfrom.
In my particular case I'm only dealing with IPv4 addresses for now.

> receive_data(Sock, State) ->
>     case socket:recvfrom(Sock, 0, nowait) of
>         {ok, {From, Data}} ->
>             NewState = handle_data(From, Data, State),
>             receive_data(Sock, NewState);
>         {select, _SelectInfo} -> {ok, State};
>         {error, Reason} -> {error, Reason}
>     end.

The problem is I don't really know how to match the From address. I would like
to match the IP address in the above statement, something like the following
pseudo-code:

>     case socket:recvfrom(Sock, 0, nowait) of
>         {ok, {#socket:sockaddr_in4{family=inet, addr=From}, Data}} ->

My (limited) understanding is that I would need to include a header file to use
records from another module, but as far as I can see, the socket module does
not define a header file.
If need be I can extract the address in the body of the case statement.

How would I get the sender address from the return value of the recvfrom function?

Thanks,
Thomas



More information about the erlang-questions mailing list