Skip to content

Instantly share code, notes, and snippets.

@PeteDevoy
Created October 19, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteDevoy/30476594a1488c78106af1efe7de44bd to your computer and use it in GitHub Desktop.
Save PeteDevoy/30476594a1488c78106af1efe7de44bd to your computer and use it in GitHub Desktop.
Erlang gen_tcp {packet, line} with null byte line delimiter
%Open two shells
%In first shell
{ok, ListenSock} = gen_tcp:listen(8091, [{active,true}, binary]).
{ok, AcceptSock} = gen_tcp:accept(ListenSock).
%it waits, so proceed to connect in second shell:
%In second shell
{ok, ClientSock} = gen_tcp:connect({127,0,0,1}, 8091, [binary, {active, true}]).
%In first shell
Opts = [{active, true}, {packet, line}, {line_delimiter, $\0}].
ok = inet:setopts(AcceptSock, Opts).
%In second shell
gen_tcp:send(ClientSock, "abcd\0efg\0").
%In first shell
flush().
%Result:
%Shell got {tcp,#Port<0.333>,<<97,98,99,100,0>>}
%Shell got {tcp,#Port<0.333>,<<101,102,103,0>>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment