Java socket and HTTP differences

Source: Internet
Author: User

We all know that the TCP/IP protocol is divided into four tiers:

The ① link layer , sometimes referred to as the data Link layer or network interface layer, typically includes the device drivers in the operating system and the corresponding network interface cards in the computer. They work together with the physical interface details of the cable (or any other transmission medium).

The ② Network layer , sometimes referred to as the Internet layer, handles the grouping of activities in the network, such as the routing of groupings. In the TCP/IP protocol family, the network layer protocol consists of the IP Protocol (Internet Protocol), the ICMP Protocol (Network Internet Control Message Protocol), and the IGMP Protocol (Internet Group Management Protocol).

The ③ Transport layer, which provides end-to-end communication primarily for applications on two hosts. In the TCP/IP protocol family, there are two different transport protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). TCP provides high-reliability data communication for two hosts. The work that it does involves putting the data that the application gives to it into the appropriate small pieces to the following network layer, confirming the received packet, setting the timeout clock to send the last acknowledgment packet, etc. Because the transport layer provides high-reliability end-to-end communication, the application layer can ignore all of these details. On the other hand, the U D P provides a very simple service for the application layer. It only sends packets called datagrams from one host to another, but does not guarantee that the datagram will reach the other end. Any required reliability must be provided by the application layer. These two transport layer protocols have different uses in different applications, which will be seen later.

The ④ Application layer is responsible for handling specific application details. Almost all of the different TCP/IP implementations provide the following common applications: Telnet telnet. FTP file Transfer Protocol. SMTP Simple Mail delivery protocol. SNMP Simple Network Management protocol.

When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but then, if there is no application layer, it will not be able to identify the data content, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, such as HTTP, FTP, Telnet, etc., You can also define the application layer protocol yourself. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol.

<1>socket is an interface for TCP and UDP programming that you can use to establish TCP connections and so on.   The TCP and UDP protocols belong to the transport layer. HTTP is an application-layer protocol that is actually built on top of the TCP protocol (HTTP is a sedan that provides a specific form of encapsulation or display of data; the socket is the engine that provides the ability to communicate on the network).

<2>socket is the encapsulation of TCP/IP protocol, the socket itself is not a protocol, but a calling interface (API), through the socket, we can use the TCP/IP protocol. The advent of sockets only makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know.

Here are some of the important concepts, special here to do excerpts and summaries.

One. What is a three-time handshake for a TCP connection

First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waits for the server to confirm; second handshake: The server receives the SYN packet, must confirm the customer's SYN (ACK=J+1), and also sends a SYN packet (SYN=K), which is the SYN +ack packet, when the server enters the SYN_RECV state, the third handshake: the client receives the server's Syn+ack package, sends the acknowledgement packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, complete three handshake.

The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four handshake" (the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)

Two. To establish a network connection using a socket

Establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket.

The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.

1. Server monitoring: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client connection request.

2. Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

3. Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.

Three. Features of HTTP links

The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the most commonly used protocols for mobile networking, an application built on the TCP protocol.

The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended. The process from establishing a connection to closing a connection is called a "one-time connection."

Four. The difference between TCP and UDP

1. TCP is link-oriented, although the network's insecure instability determines how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at least (in fact, to a large extent guaranteed) to ensure the reliability of the connection, and UDP is not a connection-oriented, UDP transmission data before the connection with the other party, the data received is not sent to confirm the signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol.

2. Also due to the characteristics of 1, so that the cost of UDP more small data transmission rate is higher, because there is no need to send and receive data confirmation, so UDP real-time better.

Know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than UDP transmission file slow, but can not say that QQ communication is not safe, because the programmer can manually verify the data sent and received by UDP, For example, the sender of each packet number and then by the receiver to verify AH what, even so, UDP because the package on the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.

Java socket and HTTP differences

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.