Skip to content

Instantly share code, notes, and snippets.

@Vagabond

Vagabond/node1 Secret

Last active April 13, 2020 20:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Vagabond/efb0c1563ef7b94b3b27 to your computer and use it in GitHub Desktop.
Save Vagabond/efb0c1563ef7b94b3b27 to your computer and use it in GitHub Desktop.
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.3 (abort with ^G)
1> {ok, LS} = gen_tcp:listen(5678, [{reuseaddr, true}]),
{ok, S} = gen_tcp:accept(LS),
{ok, FD} = inet:getfd(S),
erlang:send_term(FD, {<<"hello">>, from, "beyond the veil"}).
true
2>
1> {ok, Sock} = gen_tcp:connect("localhost", 5678, [binary, {packet, 4}, {active, true}]).
{ok,#Port<0.609>}
2> flush().
Shell got {tcp,#Port<0.609>,
<<131,104,3,109,0,0,0,5,104,101,108,108,111,100,0,4,102,114,
111,109,107,0,15,98,101,121,111,110,100,32,116,104,101,32,
118,101,105,108,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0>>}
ok
3> binary_to_term(<<131,104,3,109,0,0,0,5,104,101,108,108,111,100,0,4,102,114,
3> 111,109,107,0,15,98,101,121,111,110,100,32,116,104,101,32,
3> 118,101,105,108,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3> 0,0>>).
{<<"hello">>,from,"beyond the veil"}
BIF_RETTYPE send_term_2(BIF_ALIST_2)
{
if (is_not_small(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
Sint fd = signed_val(BIF_ARG_1);
Uint size = erts_encode_ext_size(BIF_ARG_2)+1;//version + length
char *out = malloc(size+4);
Uint network_size = htonl(size);
out[3] = (char)((network_size & 0xff000000) >> 24);
out[2] = (char)((network_size & 0xff0000) >> 16);
out[1] = (char)((network_size & 0xff00) >> 8);
out[0] = (char)((network_size & 0xff));
char *scratch = out+4;
erts_encode_ext(BIF_ARG_2, &(scratch));
send(fd, out, size+4, 0);
BIF_RET(am_true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment