Erlang three socket message Loops

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.