... -define(TCP_OPTIONS, [binary, {packet, raw}, {active, false}]). ... {ok,Socket} = gen_tcp:listen(Port, ?TCP_OPTIONS), ...
binary
- in the form of binary data;list
- as a list %% Msg = <<1,2,3>>, %% , Msg %% <<FirstByte, Tail/binary>> = Msg, %% : %% Msg 1 %% Tail <<2,3>> %% (list) Msg = [1,2,3], %% %% [FirstByte|Tail] = Msg, %% %% : %% Msg 1 %% Tail [2,3]
{active, false}
mode {active, false}
socket works in the so-called passive mode . It is recommended to use with large amounts of data and high speeds; in cases where different speeds of the network at the client and server; so that the client does not mess up the server. And all because in this mode tcp / ip flow control is used (opening for me). recv(Socket) -> case gen_tcp:recv(Socket, 0) of {ok, RcvdData} -> %% RcvdData - recv(Sock); {error, closed} -> %% {error, closed} end.
{active, true}
mode {active, true}
data received from the socket is sent to the process as messages. But the flow control is not here, so you can fill the receiving side with a large amount of data. recv(Socket) -> receive {tcp, Socket, RcvdData} -> %% RcvdData - recv(Sock); {tcp_closed, Socket} -> {error, closed} end.
inet:setopts(Socket, [{active, once}]),
{active, once}
mode {active, once}
socket is in active mode until the first message is received. After that, it goes into passive mode with flow control (tcp flow control). This is the case when we want to receive socket data as messages and at the same time we need flow control. Every time we want to receive data, we need to change the active
property of the socket. recv(Socket) -> inet:setopts(Socket, [{active, once}]), receive {tcp, Socket, RcvdData} -> %% RcvdData - recv(Sock); {tcp_closed, Socket} -> {error, closed} end.
once
you can specify a number. Read more in the documentation.{packet,0}
or {packet,raw}
mode {packet,0}
data is not packaged in any way and is transmitted for processing as is.{packet,1 | 2 | 4}
{packet,1 | 2 | 4}
{packet,1 | 2 | 4}
before the data is 1, 2 or 4 bytes, which specify the length of the message. The message will get into the process only after it is accepted completely.{packet,line}
mode {packet,line}
data will be collected until a newline character is received. Conveniently, for example, when writing a server that processes data from the terminal. The data from the socket will not come separately by letter, but already as a string.{nodelay, true|false}
understandable without translation. A value of type boolean.{buffer, Size}
. Moreover, val(buffer) >= max(val(sndbuf),val(recbuf))
. Which can be set separately. gen_tcp:controlling_process(Socket, ProcessPID)
Source: https://habr.com/ru/post/219967/
All Articles