Skip to content

Instantly share code, notes, and snippets.

@voluntas
Created March 20, 2018 06:41
Show Gist options
  • Save voluntas/3f1a9808e68646cb0244486895a54f01 to your computer and use it in GitHub Desktop.
Save voluntas/3f1a9808e68646cb0244486895a54f01 to your computer and use it in GitHub Desktop.
UDP エコーサーバー
-module(udp_echo).
-export([main/0]).
-include_lib("eunit/include/eunit.hrl").
-define(PORT, 5500).
main() ->
case gen_udp:open(?PORT, [binary, {active, false}]) of
{ok, Socket} ->
spawn(fun() -> echo(Socket) end),
ok;
{error, Reason} ->
error(Reason)
end.
echo(Socket) ->
case gen_udp:recv(Socket, 1400) of
{ok, {IpAddress, Port, Data}} ->
ok = gen_udp:send(Socket, IpAddress, Port, Data),
echo(Socket);
{error, Reason} ->
error(Reason)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment