Socket requests Web server process, socket requests web

Source: Internet
Author: User
Tags file transfer protocol

Socket requests Web server process, socket requests web

At first, we need to understand one thing, because this is the premise of this article:

HTTP is only an application layer protocol, which transmits data through TCP at the underlying layer. Therefore, the browser must first establish a connection when accessing the Web server.

Some may ask: As we all know, HTTP has two major features: "connectionless" and "stateless. Isn't there a conflict with the above saying "No connection" here? In fact, there is no conflict here, but there are differences in people's understanding of the word "connection. First, let's take a look at the process in which the browser sends an Http request to the Web server and the Web server replies to the browser:

1) the browser creates a Socket and connects to the server based on the given IP address (Domain Name) and Port (80 by default. For example, use methods such as Socket. Connect () and Socket. BeginConnect;

2) After the connection is successful, the browser sends request data to the Web server according to the HTTP protocol specification (as mentioned later. For example, "request line", "Request Header mark", and "request data", methods such as Socket. Send () and Socket. BeginSend () may be used here. [For more information about HTTP Request lines and request header labels, see http://www.cnblogs.com/riky/archive/2007/04/09/705848.html]

3) the browser waits for the server to process and return data;

4) after the Web server uses Socket. Accept () and Socket. BeginAccept () to listen to the browser connection, it starts to receive the data sent by the browser. After receiving the request data, parse the data according to the HTTP protocol specification, and then process the data, and finally send the processing result (such as the html document) back to the browser. The Socket may be used here. send (), Socket. beginSend () and other methods;

5) after the Web server sends the processing result, close the Socket;

6) The browser receives data from the Web server (such as html) and displays the data on the browser UI. Disable socket;

7) an http request from the browser to the Web server ends;

8) the next time the browser needs to request the Web server, jump to step 1.

The preceding process is shown in a diagram:

Figure 1

As shown in 1. Before the browser sends an http request to the Web server, you must establish a connection. Yes, the process of establishing connections between them is similar to that of developing socket programs. Therefore, the "no connection" feature of HTTP does not mean that no connection is required when the browser exchanges data with the Web server. So what does the "connectionless" feature mean? As shown in figure 1, the browser will be "disconnected" from the server after each request is completed, and the next request will re-establish a connection with the server. The non-connection feature of HTTP means that each request of the browser must be re-connected to the server. Normally, the browser will not be connected to the Web server for a long time. The two features of the HTTP protocol are summarized as follows:

No connection:

One connection between the server and the browser processes only one http request. After the request is processed, the connection is disconnected. The next request re-establishes the connection.

Stateless:

The server does not save the browser information. That is to say, on the server side, the results of the first http request processing will not be retained to the second request. If the result of the first request is required for the second request processing, the browser must re-return the result to the Web server (such as using cookies) during the second request ).

 

About the "Agreement ":

This topic is a little big, not something I can control. However, I 'd like to make the most out of this article. There are a wide range of protocols in the computer, and there are countless protocols in single network communication. There are many protocols in each layer of the OSI Layer 7. So what is a protocol in essence? For a single communication protocol, the essence of the Protocol is actually a data structure. Similar to the structure in the Code, the underlying layer is a byte stream, specify what the first byte represents and what the second byte represents.

The role of the agreement is similar to what we usually call "contract" and "agreement". For a team cooperation task, the partner must abide by the prior agreement at the same time, and the final work can proceed normally. In the same way in network communication, both parties must send/receive data according to the structure stipulated in implementation. If one party does not comply with this specification, the communication will fail. The structure specification mentioned here is actually "protocol ". The agreement has the following functions:

1) since it is a standard, it is easier for others to understand and communicate according to the standard;

2) write the specification into a document and provide it to others for later extension. As long as you know the communication specifications, you can easily compile the expansion module to coordinate with the original system.

3) in computer network communication, some factors determine that we must send and receive data in the specified format. For example, in TCP communication, because data is transmitted in a "stream" manner, if we do not define data transmission specifications in advance, it is difficult to determine the data boundary of TCP transmission.

For network communication protocols, the application layer protocol is the most closely related to program development (at least for people who use c # and java ), other transport layer protocols such as tcp and udp are rarely used. The communication programs we develop must comply with the defined application layer protocol. For example, browsers and Web servers must comply with the HTTP application layer protocol. Only in this way can they interact normally. If we develop a program by ourselves and follow the HTTP protocol correctly, our program can also access the Web server like chrome, IE and other browsers.

There is a demo at the end of the article that uses a socket to simulate browser requests to the Web server. We can use WebClient, WebRequest, and Other types to implement the functions. The demo function is as follows:

1) Use Socket to connect to the Web server (arbitrary );

2) Send an HTTP request in the HTTP format (using the Socket. Send method );

3) parse the data returned by the Web server in the HTTP format (actually displayed on the UI)

(Developing such a program requires us to be fully familiar with socket programming and HTTP protocol formats)

The following code sends an HTTP request:

1 /// <summary> 2 /// send request 3 /// </summary> 4 /// <param name = "socket"> </param> 5 private void sendRequest (Socket socket) 6 {7 string h1 = "GET" + _ path + "HTTP/1.1 \ r \ n"; 8 string h2 = "Accept: */* \ r \ n "; 9 string h3 = "Accept-Language: zh-cn \ r \ n"; 10 string h4 = "Host:" + _ host + "\ r \ n "; 11 string h5 = "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 \ r \ n "; 12 string h7 = "Connection: close \ r \ n"; 13 14 byte [] send_buffer = Encoding. UTF8.GetBytes (h1 + h2 + h3 + h4 + h5 + h7); 15 socket. send (send_buffer); 16 Print ("the request has been sent. Wait for the Web Server to reply... "); 17 socket. beginReceive (_ buffer, 0,640*1024, SocketFlags. none, new AsyncCallback (OnReceive), socket); 18}View Code

Below is:

Figure 2

Source code download: http://files.cnblogs.com/xiaozhi_5638/socket_browser.rar


How does the web server access the socket server interface?

When the browser accesses the web server, the server script is made in JAVA. If the JAVA Script accesses the JAVA interface of Netty, it is only an instant access. For long access, you have to use JAVA to create a program to access NETTY.

Compile a WEB server program SOCKET function

Network Communication basics-
I. TCP/IP protocol
IP: The internetprotocol is used to route data between hosts and store data on the network. It also provides Group Sending services for ICMP, TCP, and UDP.
User processes generally do not need to be involved in this layer.
ARP: Address Resolution Protocol, which maps network addresses to hardware addresses.
RARP: Reverse Address Resolution Protocol. This Protocol maps hardware addresses to network addresses.
ICMP: inter-network Message Control Protocol (Internet Control Message Protocol). This Protocol handles communication and host errors and transmission Control.
TCP: Transfer Control Protocol (Transmission Control Protocol) is a reliable full-duplex byte stream connection-oriented Protocol provided to user processes.
UDP: User Data Protocol (UDP) is a connectionless Protocol provided for User processes. It is used to transmit data without performing correctness checks.
FTP: File Transfer Protocol (File Transfer Protocol) allows users to communicate with another host in the form of File Operations (File addition, deletion, modification, query, Transfer, etc.
SMTP: Simple Mail Transfer Protocol (SMTP) is used to send emails between systems. TELNET: the Terminal protocol (Telnet Terminal Procotol) allows users to access the remote host through virtual terminals.
HTTP: Hypertext Transfer Protocol (Hypertext Transfer Procotol)
TFTP: simple File Transfer Protocol (Trivial File Transfer Protocol) II. Client/Server mode
In a TCP/IP network, the host mode in which two processes interact is the Client/Server model ).
The establishment of this mode is based on the following two points: 1. Non-peer effect; 2. Communication is completely asynchronous.
In the Client/Server mode, the following request methods are used: Server:
1. Open a channel and inform the local host that it is willing to receive customer requests at a recognized address.
2. Wait for the customer's request to reach the port.
3. Receive a duplicate service request, process the request, and send a response signal.
4. Return step 2, waiting for another customer's request
5. Disable the server. Customer:
1. Open a channel and connect to the specific port of the host where the server is located.
2. Send a service request message to the server, wait for and receive the response; Continue to make the request ......
3. Close and terminate the communication channel after the request ends. Iii. socket c/S model
Typical Client/Server process
2.1 connection-oriented Socket System Call
Service provider: socket (s)-> bind ()-> listen ()-> [accept ()-> recv ()/send ()-> closesocket (ns)] -> cosesocket (s)
Customer: socket ()-> connect ()-> recv ()/send ()-> closesocket ()
2.2 SOCKET call without connection protocol
Service provider: socket ()-> bind ()-> recv ()/send ()-> colsesocket ()
Customer side: socket ()-> bind ()-> recv ()/send ()-> colsesocket () 4. socket function
1. Create a socket-socket ()
Function: Create a new socket...
 

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.