[Simple Web Server construction] The simplest Web server implemented based on Socket [ASP. NET Principle Analysis]

Source: Internet
Author: User

Generally, we use a browser (usually IE, FireFox, or Chrome) to browse the Web page. For example, we enter the DebugLZQ blog URL in the address bar:

Behind this simple operation, the huge complexity is hidden.

The content we enter in the address bar is called the Universal Resource Identifier,URI) It has many styles. In the Web, we usually call it the form of a Uniform Resource Locator (URL). Its format is as follows:

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

In.

In. NET, both URI and URL use the URI class defined in the System namespace for processing. According to the above introduction, this class defines five attributes, which correspond to five components, as shown below:

Scheme: protocol name

Host: Obtain the Host part in the URI address.

Port: Get the Port number.

AbsolutePath: absolute path

Query: The parameter section in the URI address.

The following example shows the parts of an address:

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace URI description {class Program {static void Main (string [] args) {System. uri DebugLZQAddress = new Uri ("http://www.cnblogs.com/DebugLZQ/"); Console. writeLine ("Scheme: {0}", DebugLZQAddress. scheme); Console. writeLine ("Host: {0}", DebugLZQAddress. host); Console. writeLine ("Port: {0}", DebugLZQAddress. port); Console. writeLine ("AbsolutePath: {0}", DebugLZQAddress. absolutePath); Console. writeLine ("Query: {0}", DebugLZQAddress. query );}}}

The output result is as follows:

The absolute path describes the resources on the server in a format similar to a Unix file directory. This absolute path is usually called a virtual path on the Web server after it is transferred to the server.

After entering the URL in the address bar, how can we find the server? Hosts on the Internet are tens of millions. The server we want to access is one of tens of millions of servers on the Internet. It is likely to be on the other side of the earth. To find the server, the browser must provide the network address of the server.

In the current TCP/IP protocol, the so-called server network address is an IP address. Currently, we use an IPv4 address, that is, the address specified in version 4th of the IP protocol, each address consists of a total of 32 characters in four bytes. Theoretically, it can represent 4G network addresses. Generally, we separate the four numbers in a long distance to represent an address. Each number corresponds to a byte of the address. For example, if the IP address of Microsoft is 207.46.19.254, enter http in the address bar: // You can also access the webpage at 207.46.19.254.

However, these numbers are hard to remember, and people prefer to use a meaningful name to find a host. After a short period of time on the Internet, in 1983, Paul Mockapetris invented the domain name system, we can create a meaningful name for the IP address to facilitate searching for the host. This name becomes the domain name. For example, if the Domain Name of the Microsoft Web server is www.microsoft.com, the actual IP address corresponding to this name is 207.46.19.254.

Although this name is easy to remember, only this name cannot be used to directly find Microsoft's Web server. It is necessary to establish a correspondence between the name and the IP address. This task is performed by the Domain Name Server.DNS(Domain Name Server. The DNS server provides a column-based private address book that allows you to search for the corresponding address through the domain name or locate the corresponding domain name through the address. Generally, Internet service providers have automatically set up DNS servers for us. Therefore, you can simply find Microsoft Web Servers through www.microsoft.com domain names.

After finding the server, we need to transmit the request from our client to the server. How do we communicate with the two computers? How can they understand the data sent from each other? This requires the protocol.

When the browser finds the address of the Web server, the browser helps us convert requests to a series of parameters and send them to the Web server. After the server receives the request parameters from the browser, it analyzes and processes the data. Then respond to the browser with the processing result, that is, some new data, which is usually HTML webpages or images. After the browser receives the data, it parses the data and presents it in the browser window. This is the webpage we see.

In the conversation between a browser and a Web server, you need to use the syntax specifications that both parties can understand to communicate. The syntax norms for communication between such programs are called protocols. There are many protocols. According to the ISO network reference model, communication between programs can be divided into seven layers, from low to high: physical Layer, data link layer, network layer, transmission layer, Session Layer, presentation layer, and application layer. Each layer has its own protocol. For example, protocols between application layers are called application layer protocols. Different applications may have different application layer protocols. There may also be many protocols at the same layer.

The protocol between the browser and the Web server is the application layer protocol. Currently, the main protocol we follow is HTTP/1.1. HTTP is the basis of Web development. It is a stateless protocol. The client and the server complete a Session through a request and corresponding Session ). In each session, the data sent by both parties is called a Message. There are two types of messages: Request Message and Response Message.

Message format.

Figure DebugLZQ: it is not very beautiful. Roar... Bo you 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 header of the message, and the third part is the message body. The header and body are separated by a blank line.

Generally, we use a browser on the client to access the server. The browser software helps us construct all request messages. UseFiddler SoftwareTo help us detect the communication content between the browser and the server ,.

The upper right corner shows the content requested by the browser. You can see that the first row of the request contains the following content:

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

The following n consecutive behavior request headers are followed by an empty line. Because the request is a GET request, there is no request body.

The bottom right of the figure shows the content responded by the server. The status line of the first response. HTTP/1.1 200 OK indicates that the request content can be found, but it needs to be retrieved from another address. The following 15 response headers. An empty line separates the response header and response body, and the response body is a simple HTML webpage.

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

The Transport Layer Protocol solves data transmission problems, including the reliability of data traffic. The transport layer relies on the lower-layer network layer to complete actual data transmission. In the TCP/IP network protocol, the transport layer protocol responsible for reliable communication is TCP. The network layer is generally implemented using a network driver, which is not involved by common programmers. In TCP/IP, the network layer protocol is an IP protocol.

The application layer is used to transmit data between specific applications. HTTP is the application layer protocol used by TCP/IP to communicate with Web servers. The application layer protocol depends on the transport layer protocol to complete data transmission. The Transport Layer Protocol depends on the network layer protocol Wang Cheng data transmission. The relationship between them is as follows ):

Here, our preparation theory is too much to read.

In the distant Unix era, to solve the Programming Problem of the transport layer, Unix provides network operations similar to file operations starting from 4.2BSD Unix ----Socket. Through Socket, programmers can complete network programming by opening, writing, reading, and closing files like files. This allows network programming to be unified under file operations. Socket helps programmers solve the problem of the network transmission layer. In the system, the network system is responsible for handling complex operations inside the network, so that programmers can easily write network applications. Note that the protocol at the application layer must be specially processed by network programs. The Socket is not responsible for the protocol at the application layer, but for the protocol at the transport layer.

Of course, the network is not a simple file after all, so when using Socket, programmers still need to set some network-related detailed problem parameters.

When developing network applications through Socket, you must first consider the network types used, including the following three aspects:

1) Socket type. The network protocol type is used. For example, the IPv4 type is PF_INET.

2) Data Communication types, common datagram (SOCK_DGRAM) and data stream (SOCK_STREAM ).

3) The network protocol used, such as TCP.

To distinguish different applications that use the same protocol from different applications on the same network address, you can assign a number to different applications. This number is calledPort). The port number is a two-byte certificate with a value ranging from 0 ~ 65535. IANA (Internet Assigned Number Authority, Internet Address Allocation Organization) maintains a port allocation list. These ports are divided into three types. The first type ranges from 0 ~ Port 1023, known as a well-known port, is controlled and allocated by IANA and used by specific network programs. For example, TCP uses port 80 to complete HTTP transmission. The range of the second category is 1024 ~ 49151 is called the registration port. These ports are not controlled by IANA, but IANA has a registration list and should not be used in the program if they are not registered in IANA. However, in most systems, user programs can be used without conflict. The third category is 49152 ~ 65535 is called a dynamic port or a port number. These ports can be used by common user programs.

For a network application, an application on the network can be uniquely identified by the address, protocol, and port number.The combination of address and port is calledEndPoint). Each Socket needs to be bound to one endpoint to communicate with other endpoints.

In. NET, the System. Net namespace provides most data types and common operations for network programming. The common types are as follows:

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

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

3) The System. Net. Sockets namespace provides Socket-based data types.

4) The Socket class encapsulates Socket operations.

Common Operations are as follows:

1) Listen: sets the connection-based Socket to enter a strong State, and sets the length of the waiting queue.

2) Accept: Wait for a new connection. When the new connection arrives, a pointer is returned to the Socket object of the new connection. The new Socket object can communicate with the new connection.

3) Receive: receives byte data through the Socket, saves it to a byte array, and returns the actual number of accepted bytes.

4) Send: data is pre-stored in byte arrays through Socket transmission.

Boyou VOICE: That's enough. After talking so much, DebugLZQ is not too troublesome... Fast !!!!!

DebugLZQ: with the above foundation, the following code demonstrates how to create a simple Web server through Socket programming. Note: This server provides access through port 49152 and returns a fixed static webpage to the browser. In this solution, the requested message is generated by the browser and sent to the server. This program will simply display the request information. The Response Message is generated by the server program and returned to the browser through the Socket transport layer.

Using System; using System. collections. generic; using System. linq; using System. text; using System. net; // using System. net. sockets; // The simplest Socket-based Web server in namespace {class Program {static void Main (string [] args) {IPAddress address = IPAddress. loopback; // obtain the local loopback network address, that is, 127.0.0.1 IPEndPoint endPoint = new IPEndPoint (address, 49152); // create an accessible endPoint. 49152 indicates the port number, if it is set to 0, an idle port number is used. Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // create a socket using an IPv4 address. The data communication type is byte stream and the TCP socket. bind (endPoint); // Bind the socket to an endPoint socket. listen (10); // set the length of the connection queue Console. writeLine ("start listening, Port: {0}", endPoint. port); while (true) {Socket client = socket. accept (); // start listening. This method will block thread execution until a client request is received to connect to the Console. writeLine (client. remoteEndPoint); // output client address byte [] buffer = new byte [4096]; // prepare to read client request data, the read data is saved in an array with int length = client. receive (buffer, 4096, SocketFlags. none); // accept data // translate request data into UTF-8 System. text. encoding utf8 = System. text. encoding. UTF8; string requestString = utf8.GetString (buffer, 0, length); Console. writeLine (requestString); // display the request // response status line string statusLine = "HTTP/1.1 200 OK \ r \ n"; byte [] statusLineBytes = utf8.GetBytes (statusLine ); // string responseBody = "

After running, enter http: // localhost: 49152/in the browser window. The following result is displayed in the browser.

The following output is displayed in the command line:

Haha, complete the work.

Tomorrow is the birthday of DebugLZQ. Happy birthday to DebugLZQ! Wishing Sarah more and more beautiful! I wish you all a happy and progressive life !!!!!!!!!!!!! ----- 2011-12-6

Please click the Green Channel below to follow DebugLZQ, and DebugLZQ is willing to share progress with you !!!

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.