Reprinted: Master http://hi.baidu.com/zai215837829/blog/item/2e8323dc3763c02e5982dd80.html
1. Proactive message acquisition (non-blocking)
The first example is to open the socket in active mode and then accept data from the socket:
{OK, listen} = gen_tcp: Listen (port, [..., {active, true}...]),
{OK, socket} = gen_tcp: accept (Listen), loop (socket ).
Loop (socket)->
Receive
{Tcp, socket, data}->... output processing...
{Tcp_closed, socket}->...
End.
In this process, messages sent to the server are not controlled. If the client generates data faster than the server consumes data, the system will receive a flood of messages-Message Buffer Overflow, the system will crash and behave strangely.
This type of server is called a non-blocking server because it cannot block the client. The non-blocking server is used only when the client is trusted.
2 passive message acquisition (blocking)
In this section, we write blocking server: the server usesOpen socket in passive mode, Using the {active, false} Option. This server will not be attacked by dangerous client flood.
The code in the server loop calls gen_tcp: Recv to receive data. The client is blocked before the server calls Recv. Note that the OS will buffer the data sent from the client to allow the client to continue sending a small segment of data before the server calls Recv.
{OK, listen} = gen_tcp: Listen (port, [..., {active, false}...]),
{OK, socket} = gen_tcp: accept (Listen), loop (socket ).
Loop (socket)->
Case gen_tcp: Recv (socket, n) of {OK, B}->... data processing... loop (socket );
{Error, closed }...
End.
3. mixed message acquisition (partially blocked)
You may think it is appropriate to use the passive mode on all servers. Unfortunately, when we are in passive mode, we can only wait for data from one socket. This is not applicable to servers that need to wait for data from multiple socket sources.
Fortunately, we can use a hybrid approach, neither blocking nor non-blocking. We open the socket in active once mode {active, once. In this mode,The socket is active, but only one message can be received.. After the control process sends a message, it must be clear thatCall
Inet: setopts for socket recovery and receiving the next message. The system will be congested until this happens. This is the best combination of the two worlds. The following code is used:
{OK, listen} = gen_tcp: Listen (port, [..., {active, once}...]),
{OK, socket} = gen_tcp: accept (Listen), loop (socket ).
Loop (socket)->
Receive
{Tcp, socket, data}->... data processing... % when you are ready to enable the next message
Inet: setopts (socket, [{active, once}]),
Loop (socket );
{Tcp_closed, socket}->...
End.
With the {active, once} Option, You can implement High-Level Data Flow Control (sometimes called traffic control) and prevent the server from being overwhelmed by excessive message floods.