Web Services Basics

Source: Internet
Author: User

First, HTTP overview


HTTP is the abbreviation for Hypertext Transfer Protocol, the Hypertext Transfer Protocol. HTTP is an application-layer protocol that, like other application-layer protocols, implements a specific type of protocol for a particular application and is implemented by an application running in user space. HTTP is a protocol specification, a specification that is documented on a document and is an implementation of HTTP that actually communicates through the HTTP protocol. We know that the HTTP protocol is based on the C/s architecture of Communication, and the HTTP protocol server-side implementation program has httpd, Nginx, etc., its client implementation program is mainly web browser, such as Firefox, Internet Explorer, Google Chrome, Safari, opera, and more, the client's command-line tools are Elink, Crul, and so on. The Web service is TCP-based, so the Web server needs to listen on the 80/TCP port in order to be able to respond to client requests at any time. This can be communicated between the client browser and the Web server via the HTTP protocol.



II. ports defined by the IANA


We know that the Web server usually needs to listen on port 80 in order for all the client browser programs to communicate with it. When a user types a URL in a browser, for example: www.baidu.com, the default port is 80. This port is assigned by the IANA (Internet Assigned Numbers Authority) assignment. In addition, the IANA is administered by ICANN, and its defined ports are as follows:

0-1023: A well-known port that is permanently assigned to a fixed application, the use of these ports requires a specific application to register, belonging to a privileged port, such as the 21/tcp of a Web service 80/tcp,ftp. This type of port is enabled only by the system administrator and allows the process to listen, while the normal user does not have permissions.

1024-41951: also for registered ports, but the requirements are not particularly stringent, these ports are assigned to programs and registered for use by an app, but these apps are not very common, such as the 3306/tcp,memcache 11211/tcp of the MySQL service.

41952+: The client program uses a random port, either a dynamic port, or a private port. On Linux, this type of port range is defined in/proc/sys/net/ipv4/ip_local_port_range.



Third, BSD Socket


BSD socket is the first process communication mechanism of BSD Unix system. A socket is a socket, an implementation of the IPC (inter-process communication, interprocess communication) that allows interprocess communication mechanisms that reside on different hosts (or on the same host). Socket can be understood as "socket", as long as the wire plugged in the "socket", you can communicate. The socket is essentially the socket API, which encapsulates the communication details related to socket communication in the kernel, which encapsulates the specific communication details to be implemented by the communication subnet (transport layer, network layer, data link layer, physical layer), and the programmer calls the socket The API can write programs that implement network communication.


The socket describes the IP address and port. different services have different port numbers, each service needs to bind and listen on a port, open a socket, and the client can connect to the socket on the server through a socket, so that the two can communicate, We refer to a pair of sockets that are in a communication state as connected sockets. In general, the socket is the bridge between the application layer and the transport layer.


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/93/27/wKioL1kIgo3D31PtAAGooR0HqMg162.jpg "title=" sockets. jpg "alt=" wkiol1kigo3d31ptaagoor0hqmg162.jpg "/>

As mentioned earlier, the socket is a description of the IP address and port number, an IP address + a port is a socket (socket). The protocols used by the socket in the transport layer can be divided into the following three categories:

(1) SOCK_STREAM:TCP sockets

(2) SOCK_DGRAM:UDP sockets

(3) Sock_raw:raw sockets


The raw sockets here refer to a way of communicating directly by sending or receiving IP packets without using any Transport layer protocol (such as TCP, UDP, etc.), that is, the application layer directly bypasses protocols such as TCP/UDP directly based on IP packet traffic.


Depending on the address format of the socket listener, socket domain can be divided into:

(1) Af_inet:address Family,ipv4 Address format

(2) Af_inet6:address Family,ipv6 Address format

(3) Af_unix:address Family, an address format used by different processes on the same host based on socket sockets communication, which is represented by a socket file (all Linux files); This address is also known as Unix_sock.


Next, we describe how the TCP client and the server communicate through the TCP socket interface.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/93/57/wKiom1kKhs7y4B0rAAKvFPOyito246.jpg "title=" How are TCP clients and servers communicating through the TCP socket interface?. jpg "alt=" wkiom1kkhs7y4b0raakvfpoyito246.jpg "/>

For the TCP server side, the first call to the socket () function, the kernel registration request to create a TCP socket (IP address + port); Once the application is successful, it is necessary to pass the bind () function binding program to the socket; After binding, you need to call the Listen () function. Enables the server process to listen on the socket, allowing the socket to connect, but at this point, it is not possible to receive a user request, only after the Accept () function is called to receive the client request, and until the client request arrives, the status of the TCP server process is listen.


For TCP clients, it is also necessary to call the socket () function to create a socket, but not to listen, and when the client initiates a connection request to the server, the Connect () function is called, where the parameters in parentheses include the IP address and port of the opposing server, and if the server receives the connection request, The two parties have a TCP three handshake, establish a two-way virtual link, when the client and the server between the socket is in the connected State (established), after the HTTP request/response messages are based on the previously established virtual link to send.


On Linux, when the connection is established, the client can fill in the requested data on the socket file to which the server is connected, and writing the data is equivalent to sending the data to the server, which is done by calling the write () function. The server side can read the data on the socket file that the client is connected to, i.e. call the read () function, which is equivalent to receiving the data requested by the client, and the specific communication details are completed by the underlying communication subnet. After the server reads the request message sent by the client, it then parses the request, processes the request and constructs the response message, and then writes the response message to the socket file by calling the Write () function, which is transmitted to the client's socket file via a communication subnet. The client can get the server's response message through the socket file. This loop is repeated several times until the client requests a disconnection (typically by the client's unsolicited request to disconnect, when using the Keep-alive connection, the server may be actively requesting a disconnection).


Once the client requests a disconnection, the TCP server agrees to disconnect by calling the read () function to read the disconnect request data within the socket file.



This article is from the "tab" blog, so be sure to keep this source http://xuweitao.blog.51cto.com/11761672/1921842

Web Services Basics

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.