Select and keepalive notes for LWIP

Source: Internet
Author: User
Tags web hosting keep alive

Recently, rt_thread, the real-time operating system of Chinese users, was used on stm32f103. Here, we use the Web Hosting process.

The most basic select () cannot escape while setsockopt () cannot. The following is an example of your usage.

Let's talk about the select () function.

In fact, it is not limited to the website platform. It mainly refers to changing permanent blocking into waiting for a certain period of time. The so-called ultra-time mechanism is also-in fact, what is better understood is: Change undefined imprisonment to imprisonment. It is like this: it will introduce the operational nature of the elements in a large collection, once the operator changes the operational nature of a certain or several elements, a value will be returned immediately, this value indicates the number of elements in the set with variable operation characteristics. Then, you can use the operator to root your mood and operate these elements according to the operable nature.

So, what is this set? Similar to a data group. Each grid of a Data Group contains one element.

So what is the element? File descriptor: for example, when a text file is opened, a descriptor is generated. When a UART is opened, a descriptor is generated. When a Socket socket is opened, it is also a descriptor... these descriptors are these elements .. it is about 1024. Check whether it is true.

So what are these elements, or what are the operational features of these file descriptors? Well, Linux only seems to have three rwx or 421. in this case, the features of these file descriptors include: <"R">, <"W">, and <sorry, it's not "X">.

So, when will these changes happen? For example, when the serial port UART is waiting for the data volume, the descriptor value is 0. all of a sudden, when the opposite end was set up, the descriptor was changed to 1; after the TCP server and a client are connected, wait for the number of clients on the client end. At this time, the socket layer of the server is always 0, when the client suddenly throws a bunch of data records, in this case, the server socket volume changes to 1. <In the preceding statement, the Delimiter is a false setting, and the identifier of a specific file descriptor is set. It depends on how the Linux kernel is implemented.>.

As a result, the variation of the latency between 0 and 1 caused the attention of select ()-in the back, it will tell the Program Members about this situation, and the Program Members will handle it themselves: if the program changes, the data will continue, when there is a change, it's just a matter of data. But there must be some questions: When the select () statement of a descriptor changes, it will be returned immediately, still, the elements in the entire set will be deleted. When the entire set is finished, what will be returned after the execution? <Returned only when the time exceeds the upper limit>

Well, let's talk about the mood of programmers. What kind of mood? I only care about whether the UART data can be written or not, and whether the UART data can be written. I only care about whether my socket can be written to the opposite end, no matter whether there is a data volume or not .. or, the text file that I don't trust in cannot be mixed up .. or, I have no energy, so I have to worry about these connections .. that's fine. No problem. The SELECT statement has been completed.

Since we have all said this, should we immediately use select () to represent the entire tall generation? Don't worry, there is another fatal point:

That is, select () is also a character-each row, and some of its values must be reset once. Otherwise, it will work again <of course, in my hand, when I used to work for a dish, the level of my team was 1, and there was no such thing as 1, this is the reason for this record>.

Well, in other aspects, there is no limit to the Internet resources, and I am also very clear about it. I will not talk more about it. Next I will go directly to the next generation of products, mainly focusing on the while (1) of the reset part of the Memory number:

  

 1 int client_hearbet(void) 2 { 3   int retval = -1, maxfdp = -1, heart_count = 0; 4   struct timeval timeout; 5   fd_set readset; 6 /**/ 7 //...... 8   maxfdp = client_sock + 1; 9   FD_ZERO(&readset);10   FD_SET(client_sock, &readset);11   while(1) {12 //......13       timeout.tv_sec = 20;14       timeout.tv_usec = 0;15       maxfdp = client_sock + 1;16       FD_ZERO(&readset);17       FD_SET(client_sock, &readset);      18       retval = select(maxfdp, &readset, NULL, NULL, &timeout);19 //....20       else if( !retval ) {21 //....22         FD_CLR(client_sock,&readset);23         continue ;24       }25       else {26         if(FD_ISSET(client_sock, &readset)) {27            /*recv*/ 28     29             recv_bytes = recv(client_sock, heart_buff, LEN, 0);30    //.....31              FD_CLR(client_sock,&readset);32     //.....33         }34     //......    35         FD_CLR(client_sock,&readset);36       }37   }38 }

Let's figure out the network route, which is actually the LWIP socket route. Note 13 ~ The 17 rows are in the while (1) Statement. Note that the first 15th rows are the largest descriptor + 1 --> which indicates the size of the set before the current time.

Well, the long-lasting select () failover problem on the last day has been solved. I wrote this part of my notebook for my friends who need it to take the test. <I have learned it before>

 

--------------------------------------------------------------------------------

The following describes the problem of using setsockopt () in LWIP.

LWIP is also a protocol for TCP/IP. However, it does not have the TCP/IP on the PC, so it is completely integrated-the TCP/IP version that is going to run on the embedded device. At this point, in some places, you need to change your hands and try your clothes.

The result is as follows:

The port and address should be used for failover. Generally, setsockopt () is used to set the so_reuseaddr option, I can immediately feel that someone has unplugged the website. Generally, setsockopt () is used to set the keepalive option. Of course, this is also supported in LWIP.

Let's take a look at how clothing and food works:

1 open the following macros so_reuse lwip_tcp_keepalive in OPT. h. <Define this macro as 1 and deem it as 0. Of course, some other options are also in this opt. h File>

2 In tcp_impl.h, the following macro values are adjusted:

  

1 A # define tcp_keepidle_default 3000ul/7200000 MS ---> 3000 ms/2 3 B # define tcp_keepintvl_default 1000ul/75000 MS ---> 1000 ms/4 5 C # define tcp_keepcnt_default 3u/9 times --> 3 times/

Of course, these three macros are actually related to keepalive. Let's do it here.

This is probably the case in the Agency:

1/* sock_reuse */2 err = setsockopt (client_sock, sol_socket, 3 so_reuseaddr, 4 (const char *) & sock_reuse, sizeof (sock_reuse); 5 If (ERR <0) {6 my_debug ("setsockopt faild! \ N \ r "); 7 return false_reuseaddr; 8} 9 10 11 int keepalive = 1; // enable the keepalive attribute 12 // int keepidle = 60; // if the connection does not have any data exchange within 60 seconds, the test will be performed. 13 // int keepinterval = 5; // The packet sending time interval is 5 seconds. 14 // int keepcount = 3; // The number of test attempts. If the response is received for 1st times, the next two times will not be sent again. 15 err = setsockopt (sockfd, sol_socket, so_keepalive, (void *) & keepalive, sizeof (keepalive); 16 if (ERR <0) {17 my_debug ("% s, % d: Keep Alive faild! \ N \ r ",__ func __,__ line _); 18 return false_keepalive; 19}

Over!

 

PS: The version used by individual users is rt_thread 1.2.2 + LWIP 1.4.1. <other versions may include microservices>

 

Keyword: Select LWIP keepalive so_reuse

 

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.