"Web Backend Notes" Simple Web server setup based on socket implementation

Source: Internet
Author: User
Tags domain name server



What we enter in the address bar is called a common resource marker (Universal Resource Identifier,URI) and it has many styles, which we often call Uniform Resource Locators (Uniform Resource Locator,url) in the form of its format as follows:



Protocol://host [. Port number] [absolute path [? parameters]]



In http://www.cnblogs.com/DebugLZQ/, HTTP represents the protocol name, www.cnblogs.com represents the address of the host, and the optional port number does not appear, then the HTTP protocol default port number 80 is used; absolute path is/ debuglzq/; no parameters appear in this example.



In. NET, whether it is a URI or a URL, it is processed using the URI class defined in the System namespace. Corresponding to the above description, this class defines 5 properties, corresponding to 5 components, as follows:



Scheme: The name of the protocol



Host: Gets the master portion of the URI address



Port: Get port number



Absolutepath: Absolute Path section



Query:uri the parameter portion of the address



The following example shows the various parts of the address:


 
 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5  
 6 namespace URI说明
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             System.Uri DebugLZQAddress = new Uri("http://www.cnblogs.com/DebugLZQ/");
13             Console.WriteLine("Scheme: {0}",DebugLZQAddress.Scheme );
14             Console.WriteLine("Host: {0}", DebugLZQAddress.Host );
15             Console.WriteLine("Port: {0}", DebugLZQAddress.Port );
16             Console.WriteLine("AbsolutePath: {0}", DebugLZQAddress.AbsolutePath );
17             Console.WriteLine("Query: {0}", DebugLZQAddress.Query );
18         }
19     }
20 }


The output results are as follows:






The absolute path section uses a Unix-like file directory to describe the resources in the server, which are typically referred to as virtual paths on the Web server after the absolute path is routed to the server.



How do we find the server after we enter the URL in the address bar? Host thousands on the Internet, the server we want to access is one of the tens of millions of servers on the Internet, and it's probably far away on the other side of the Earth. To locate the server, the browser needs to provide the server's network address.



Under the current TCP/IP protocol, the so-called network address of the server is an IP address, currently we use the address of the IPv4, that is, the IP protocol 4th version of the address, each address by four bytes total 32 bits. Theoretically, you can represent a 4G network address. Usually we use the distance to separate four numbers to represent an address, each number corresponds to a byte of the address, for example, Microsoft's IP address is: 207.46.19.254, directly in the address bar input http://207.46.19.254 can also access the Web page.



But these numbers are hard to remember, and people prefer to find a host with a meaningful name. After a brief initial phase of the Internet, in 1983, Paul Moca (Paul mockapetris) invented the domain Name system so that, on the Internet, we could make a meaningful name for the IP address to conveniently find the host, which became the domain name. For example, the domain name of the Microsoft Web server is www.microsoft.com, which corresponds to the actual IP address of 207.46.19.254.



Although this name is very good to remember, but only this name can not directly find the Microsoft Web server, you must establish a name and IP address between the corresponding relationship. This work is done by the DNSof the domain name server, or domain name. The DNS server provides a column-whispering hierarchical Address book that allows the user to locate the corresponding addresses through the domain name, or to complete the address to find the corresponding domain name. Usually, the Internet service provider has set up a DNS server for us, so we can simply find the Microsoft Web server through the www.microsoft.com domain name.



After we find the server, we need to transfer the request from our client to the server, so how does the two computers communicate? How can they understand the data they send to each other? This requires a reference to the agreement.



When the browser looks for the address of the Web server, the browser helps us to convert the request from the server to a series of parameters to the Web server. After the server receives the request parameters from the browser, the data is parsed and processed. It then responds to the browser with the result of the processing, which is some new data, which is usually an HTML page or a picture. After the browser is received, parse the data and present them in the browser window, which is the page we see.



In a browser-to-Web server conversation, you need to use a syntax specification that both parties can understand to communicate, which is what we call a protocol. There are many kinds of protocols, according to the International Organization for Standardization ISO Network reference Model, the communication between program and program can be divided into 7 layers, from low to high in order: Physical layer, Data link layer, network layer, transport layer, Session layer, presentation layer, application layer. Each layer has its own corresponding protocol. For example, the protocol between application tiers is called the Application layer protocol. Different applications may have different application-layer protocols. There may be many kinds of protocols on the same layer.






The protocol between the browser and the Web server is the application layer protocol, and currently, we mainly follow the protocol as http/1.1. The HTTP protocol is the foundation of web development, a stateless protocol that completes a session between the client and the server through the request and corresponding. In each session, the data sent by both parties is called a message, and there are two types of messages: a request message and a response message.



The format of the message.






Figure Debuglzq with a drawing, not very beautiful. Roar ... Bo friends voice: Really ugly ...



Each message may consist of three parts, the first part is the request line or the status line of the response, the second part is the head of the message, the third part of the message body. The message header section and the Message body section are delimited with a blank line.



Typically, we use the browser to access the server at the client, and the browser software helps us construct all the request messages. The use of fiddler software can help us detect the content of communication between the browser and the server.






At the top right, the content requested by the browser, you can see the first behavior request line, the content of the request is:



GET http://www.microsoft.com/en-us/default.aspx http/1.1



The following continuous n behavior requests the header part, then a blank line, because there is a GET request, so there is no request body part.



The lower right part of the server responds to the content, the first behavior response of the status line, http/1.1 OK indicates that the requested content can be found, but need to go to another address to fetch. The following 15 behavior responds to the head. A blank line separates the head of the response and the body part of the response, in response to a simple HTML page.



The HTTP protocol defines the format of the content, which is an application-layer protocol, where the content of the application-layer protocol needs to be transmitted between the browser and the server through the transport layer, and the TCP/IP protocol is an implementation of the ISO network reference model. In the TCP/IP protocol, there are two main layers related to network programmers: the transport layer and the application layer.



The Transport Layer protocol is responsible for solving the data transmission problem, including the reliability problem. The transport layer relies on the lower level of the network layer to complete the actual data transmission, in the TCP/IP network protocol, the Transport Layer protocol responsible for reliable communication is the TCP protocol. and the network layer is generally implemented by the network driver, the ordinary programmer will not be involved; in the TCP/IP protocol, the Network layer protocol is the IP protocol.



The application tier is used to transfer data between specific applications. The HTTP protocol is the application layer protocol specifically used in the TCP/IP protocol for communication between the browser and the Web server. The application layer protocol relies on the Transport layer protocol to complete the data transmission, and the Transport Layer protocol relies on the network layer protocol to transmit data, the relationship between them (the transmission process of network communication between browser and server):






Here, our preparation theory is too much to read, oh, we have to know the socket.



In the remote UNIX era, in order to solve the programming problem of the transport layer, starting with 4.2BSD Unix, UNIX provides a network operation mode similar to file operation----Socket. Through sockets, programmers can complete network programming like files by opening, writing, reading, and closing. This allows network programming to be unified to file operations. Through the socket to help programmers solve the problem of the network transport layer, and the system is responsible for the network system to handle the complex operations inside the network, so that programmers can easily write Web applications. It is important to note that the application layer protocol needs to be handled specifically for the network program, and the socket is not responsible for the application layer protocol, only the transport layer protocol.



Of course, after all, the network is not a simple file, so when using the socket, the programmer still need to set some network-related details of the problem parameters.



When developing a Web application through a socket, you first need to consider the type of network you are using, including the following three areas:



1) socket type, using the type of network protocol, such as IPV4, is pf_inet.



2) Type of data communication, common datagram (SOCK_DGRAM), data stream (SOCK_STREAM).



3) Use of the network protocol, such as: TCP protocol.



On the same network address, in order to differentiate between different applications that use the same protocol, you can assign a numeric number to different applications, which is called the network port number (port). The port number is a two-byte certificate with a range of values from 0~65535. The IANA (Internet Assigned Number Authority, Internet address assignment Agency) maintains a list of port assignments in three categories, the first of which is 0~1023, known as a port, controlled and distributed by the IANA, Used by a specific network program, for example, the TCP protocol uses port 80th to complete the transmission of the HTTP protocol. The scope of the second class is 1024~49151, called the enlistment port, which is not controlled by the IANA, but the IANA Commission has a list of registrations that should not be used in the program if it is not registered with the IANA. However, most systems can also be used by user programs in the absence of conflicts. The scope of the third class is 49152~65535, which is called dynamic or like a port number, which can be used by ordinary user programs.



for a Web application, an application on the network can be uniquely identified by address, protocol, and port number. where the combination of address and port is called the endpoint (EndPoint). Each socket needs to be bound to one endpoint to communicate with other endpoints.



In. NET, the System.Net namespace provides most of the data types for network programming, as well as common operations, which are commonly used in the following types:



1) The IPAddress class is used to represent an IP address.



2) The IPEndPoint class is used to represent a combination of an IP address and a port number, called the endpoint of the network.



3) The data type based on socket programming is provided in the System.Net.Sockets namespace.



4) The socket class encapsulates the operation of the socket.



The usual operations are as follows:



1) Listen: Set the socket based on the connection communication into a firm state and set the length of the wait queue.



2) Accept: Wait for a new connection, and when the new connection arrives, returns a pointer to the newly connected socket object. With the new socket object, you can communicate with the new connection.



3) Receive: Accepts byte data through the socket, saves it in a byte array, and returns the number of bytes actually accepted.



4) Send: Sends a pre-saved data in a byte array via the socket.



Bo friends Voice: Enough, said so much, debuglzq really not too troublesome ... Fast!!!!!



DEBUGLZQ: Roar, with the foundation above, the following code shows how to create a simple Web server with socket programming. Note: This server provides access through port No. 49152 and returns a fixed static web page to the browser. In this solution, the requested message is generated by the browser and sent to the server, and the program simply displays the request information. The message of the response is generated by the server program and returned to the browser through the socket transport layer.





1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Net; //
 6 using System.Net.Sockets; //
 7
 8 namespace The simplest web server based on Socket
 9 {
10 class Program
11 {
12 static void Main (string [] args)
13 {
14 IPAddress address = IPAddress.Loopback; // Get the loopback network address of this machine, that is, 127.0.0.1
15 IPEndPoint endPoint = new IPEndPoint (address, 49152); // Create an accessible endpoint, 49152 represents the port number, if it is set to 0, it means use an idle port number
16 Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Create a socket, use IPv4 address, the data communication type is byte stream, TCP protocol
17 socket.Bind (endPoint); // Bind the socket to an endpoint
18 socket.Listen (10); // Set the length of the connection queue
19 Console.WriteLine ("Start listening, port number: {0}", endPoint.Port);
20 while (true)
twenty one             {
22 Socket client = socket.Accept (); // Start listening, this method will block the execution of the thread until a client request connection is received
23 Console.WriteLine (client.RemoteEndPoint); // Output the client's address
24 byte [] buffer = new byte [4096]; // Ready to read the data requested by the client, the read data will be stored in an array
25 int length = client.Receive (buffer, 4096, SocketFlags.None); // Accept data
26 // Translate request data to UTF-8
27 System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
28 string requestString = utf8.GetString (buffer, 0, length);
29 Console.WriteLine (requestString); // Display request
30 // Response status line
31 string statusLine = "HTTP / 1.1 200 OK \ r \ n";
32 byte [] statusLineBytes = utf8.GetBytes (statusLine);
33 // Web page ready to be sent back to the client
34 string responseBody = "<html> <head> <title> From Socket Server </ title> </ head> <body> <h1> Hello world. <H1> </ body> </ html>";
35 byte [] responseBodyBytes = utf8.GetBytes (responseBody);
36 // response head
37 string responseHeader = string.Format ("Content-Type: text / html; charset = UTF-8 \ r \ nContent-Length: {0} \ r \ n", responseBody.Length);
38 byte [] responseHeaderBytes = utf8.GetBytes (responseHeader);
39
40 // Send status information to the client
41 client.Send (statusLineBytes);
42 // Send a response header to the client
43 client.Send (responseHeaderBytes);
44 // Header and content separated lines
45 client.Send (new byte [] {13,10});
46 // Send content to client
47 client.Send (responseBodyBytes);
48
49 // Disconnect from the client
50 client.Close ();
51 if (Console.KeyAvailable)
52 break;
53}
54 socket.Close ();
55}
56}
57} 


After running, in the browser's window enter: http://localhost:49152/, the browser can see the following display results.






See the following output on the command line:






Reference Original: http://www.cnblogs.com/DebugLZQ/archive/2011/12/06/2278234.html



"Web Backend Notes" Simple Web server setup based on socket implementation


Related Article

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.