Created
March 20, 2018 06:41
-
-
Save voluntas/3f1a9808e68646cb0244486895a54f01 to your computer and use it in GitHub Desktop.
UDP エコーサーバー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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