Tiny server-1: an in-depth understanding of computer systems
I recently learned Chapter 1 network programming in "deep understanding of computer systems". The last section is the implementation of a small web server named Tiny, which has been provided in the source code book, here we will not copy and paste it. This blog mainly records answers to question 10 after class. Original question:
Write the HTML form of the CGI adder function. Your form should include two text boxes. You can add two numbers to the two text boxes. Your form should use the GET method to request content.
Labels:
1 <!DOCTYPE html> 2
This is displayed in the browser after localhost: 8888 is entered. 8888 is the tiny port I set. as to why there is no backend/index.html, it is because I set index.html as the main interface in the source code.
You can enter two numbers and click the Submit button. The result is definitely incorrect ..
As shown in, the result is 0. Note: In the address bar of the webpage, we can see "localhost: 8888/cgi-bin/adder? Num1 = 22 & num2 = 22 ", we will know after reading the tiny source code, adder. c. The data to be analyzed is a pure number on both sides of the & symbol, that is, to be correctly adder. c program summation, the address bar should display "localhost: 8888/cgi-bin/adder? 22 & 22. The error occurs because the adder program does not obtain the value of two parameters. The solution is simple. You only need to extract two numbers in the adder. c program. The entire adder. c code is as follows:
1 # include "net. h "2 3 int main (void) 4 {5 char * buf, * p; 6 char arg1 [MAXLINE], arg2 [MAXLINE], content [MAXLINE]; 7 char tmp [MAXLINE]; 8 int n1 = 0, n2 = 0; 9 10 if (buf = getenv ("QUERY_STRING "))! = NULL) {11 p = strchr (buf, '&'); 12 * p = '\ 0'; 13 14 strcpy (arg1, buf); 15 strcpy (arg2, p + 1); 16 17 // code used to retrieve two parameters 18 p = strchr (arg1, '='); 19 strcpy (arg1, p + 1 ); 20 p = strchr (arg2, '='); 21 strcpy (arg2, p + 1); 22 23 n1 = atoi (arg1); 24 n2 = atoi (arg2 ); 25} 26 27 sprintf (content, "QUERY_STRING = % s", buf); 28 sprintf (content, "Welcome to add.com:"); 29 // sprintf (content, "arg1 = % s, arg2 = % s \ n", arg1, arg2); debug output Parameter 30 sprintf (content, "% sThe Internet addition portal. \ r \ n <p> ", content); 31 sprintf (content," % sThe answer is: % d + % d = % d \ r \ n <p> ", 32 content, n1, n2, n1 + n2); 33 sprintf (content, "% sThanks for visiting! \ R \ n ", content); 34 35 // generate the http response36 printf (" Connection: close \ r \ n "); 37 printf (" Content-length: % d \ r \ n ", (int) strlen (content); 38 printf (" Content-type: text/html \ r \ n "); 39 printf ("% s", content); 40 fflush (stdout); 41 42 exit (0); 43}
After the adder. c is re-compiled, enter the URL: localhost: 8888 in the browser and enter two numbers.
So far, we have completed the first phase of Tiny, and completed the requirements for question 11.10 after class, which can process static and dynamic requests from browsers. However, because our Tiny can only process one connection at a time, the efficiency is too low. In the next section, we need to improve Tiny to support concurrent processing.