[erlang-questions] Matching IP address in socket module

Micael Karlberg micael.karlberg@REDACTED
Thu Oct 24 15:07:55 CEST 2019


case socket:recvfrom(Sock, 0, nowait) of
       {ok, {#{family := Domain,
               port   := Port,
               addr   := Addr}, Data}} ->
       .
       .
       .


/BMK

On 2019-10-24 14:02, Mark Jones wrote:
>    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 <mailto:thp%2Berlang@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 <mailto:erlang-questions@REDACTED>
>     http://erlang.org/mailman/listinfo/erlang-questions
>     <https://protect2.fireeye.com/v1/url?k=898d5b67-d5048120-898d1bfc-0cc47ad93e96-c86930fd02912db8&q=1&e=dc31b8ad-08b4-405c-b0df-ce3ba6216af4&u=http%3A%2F%2Ferlang.org%2Fmailman%2Flistinfo%2Ferlang-questions>
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> https://protect2.fireeye.com/v1/url?k=27955f28-7b1c856f-27951fb3-0cc47ad93e96-051349a7d0786a77&q=1&e=dc31b8ad-08b4-405c-b0df-ce3ba6216af4&u=http%3A%2F%2Ferlang.org%2Fmailman%2Flistinfo%2Ferlang-questions
> 



More information about the erlang-questions mailing list