I have the following code using a gen_tcp socket, which should receive using {active, true}
some binary commands that are always of size 5 (e.g. Command = <<"hello">>
)
{ok, ListenSocket} = gen_tcp:listen(5555, [{active, true}, {mode, binary},
{packet, 0}]),
{ok, Socket} = gen_tcp:accept(ListenSocket),
loop().
receive() ->
{tcp, _Socket, Message:4/binary} ->
io:format("Success~n", []),
loop();
{tcp, _Socket, Other} ->
io:format("Received: ~p~n", [Other]),
loop()
end.
But if I try to connect as a client like this:
1> {ok, S} = gen_tcp:connect("localhost", 5555, []).
{ok,#Port<0.582>}
2> gen_tcp:send(S, <<"hello">>).
ok
I have in the receiving part:
Received: {tcp,#Port<0.585>,<<"hello">>}
so I suppose my error is in the pattern matching...but where?
Your receive
clause doesn't look right, it should be:
{tcp, _Socket, <<Message:5/binary>>} ->
....