[erlang-questions] Matching IP address in socket module
Mark Jones
markalanj@REDACTED
Thu Oct 24 14:02:35 CEST 2019
Thomas
While I have not used the socket module, the documentation indicates that
the Source variable returned from the recvfrom function is a map not a
record. So you would just need to use map matching syntax to extract the
data you want.
Mark
On Thu, Oct 24, 2019 at 6:42 AM Thomas Pircher <thp+erlang@REDACTED> wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20191024/35b3bf77/attachment.htm>
More information about the erlang-questions
mailing list