[Network] socket, TCP/UDP, HTTP, FTP

Source: Internet
Author: User
Tags ftp client

(1) Analysis of TCP/UDP, socket, HTTP and FTP

TCP/IP is a protocol group, which can be divided into three layers: network layer, transport layer and application layer:

Network Layer: IP, ICMP, ARP, RARP, and BOOTP

Transport Layer: TCP and UDP

Application Layer: FTP, HTTP, telnet, SMTP, DNS, and other protocols


HTTP is an application layer protocol, and its transmission is encapsulated as TCP protocol. You can use socket to implement HTTP.

Socket is a programming API for implementing the transport layer protocol. It can be TCP or UDP.


(2) differences between socket connection and HTTP Connection

[Socket]

 Generally, a socket connection is a TCP connection. Once a socket connection is established, both parties can send data to each other until the connection is closed. However, in actual network applications, communication between the client and the server often needs to traverse multiple intermediate nodes, such as routers, gateways, and firewalls, by default, most firewalls disable connections that are not active for a long time and cause the socket connection to be disconnected. Therefore, the network needs to be notified through polling that the connection is active.

[Http]

 HTTPThe Protocol is an application built on the TCP protocol. The HTTP connection uses the "request-response" method. Not only do you need to establish a connection during the request, in addition, the server can reply to data only after the client sends a request to the server. After the request ends, the connection is released. The process from establishing a connection to closing a connection is called "One connection ". As HTTP actively releases connections after each request ends, HTTP connections are short connections. To keep the client program online, you must continuously initiate connection requests to the server. The common practice is to immediately get no data, and the client also keeps sending a "keep connection" request to the server at a fixed time, the server replies to the client after receiving the request, indicating that the client is "online ". If the server cannot receive a request from the client for a long time, the client is considered to be "offline". If the client cannot receive a response from the server for a long time, the network is considered disconnected.


HTTP is based on the request/response model. First, the customer establishes a TCP link with the server and sends a request to the server. The request contains the request method, Uri, Protocol version, and related mime-style messages. The server responds to a status line that contains the Protocol version of the message, a success and failure code, and related mime-style messages.

[Applicable]

 In many cases, the server needs to actively push data to the client to ensure real-time and synchronous data between the client and the server. If both parties establish a socket connection, the server can directly transmit data to the client.;

 If both parties establish an HTTP connection, the server must wait until the client sends a request before sending the data back to the client.Therefore, the client regularly sends connection requests to the server, not only to stay online, but also to "ask" whether the server has new data. If yes, the data is sent to the client.

[Socket principle]

 (1) socket concept:

Socket is the cornerstone of communication and the basic operation unit for network communication that supports TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process. It contains five types of information required for network communication: the protocol used for connection, the IP address of the local host, and the protocol port of the local process, the IP address of the remote host and the protocol port of the remote process.

When the application layer uses the transport layer for data communication, TCP may encounter concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP port. To differentiate different application processes and connections, many computer operating systems provide socket interfaces for applications to interact with TCP/IP protocols. The application layer and the transport layer can use socket interfaces to distinguish between processes or network connections of different applications to implement concurrent data transmission services.

(2) establish a socket connection:

To establish a socket connection, you must have at least one socket. One of them runs on the client, which is called clientsocket, And the other runs on the server, which is called serversocket.

The connection process between sockets is divided into three steps: server listening, client requests, and connection confirmation.

Server listening: the server socket does not locate the specific client socket, but is waiting for connection. It monitors the network in real time and waits for the client connection request.

Client request: the client socket initiates a connection request, and the target is the server socket. Therefore, the client socket must first describe the socket of the server to be connected, point out the address and port number of the socket on the server, and then submit a connection request to the socket on the server.

Connection Confirmation: when the server socket monitors or receives a connection request from the client socket, it responds to the request from the client socket and creates a new thread, send the description of the server socket to the client. Once the client confirms the description, both parties establish a connection. The server socket continues to be in the listening status, and continues to receive connection requests from other client sockets.

(3) socket connection and TCP Connection

When creating a socket connection, you can specify the transport layer protocol used. The socket can support different transport layer protocols (TCP or UDP). When using the TCP protocol for connection, the socket connection is a TCP connection.


(3) TCP and UDP

Concept]

 TCP-the transmission control protocol provides connection-oriented and reliable byte stream services. Before the customer and the server exchange data with each other, a TCP connection must be established between the two parties before data can be transmitted. TCP provides timeout resend, discard duplicate data, test data, traffic control, and other functions to ensure data can be transferred from one end to the other.Ideally, once a TCP connection is established, the TCP connection will remain until either of the two parties closes the connection. When a TCP connection is disconnected, both the server and client can initiate a request to disconnect the TCP connection.
 UDP-User Datagram Protocol is a connectionless simple datagram-oriented transport layer protocol. UDP does not provide reliability. It only sends the data from the application to the IP layer, but it cannot guarantee that the data can reach the destination. Because UDP does not need to establish a connection between the client and the server before transmitting the datagram, and there is no timeout and re-transmission mechanism, the transmission speed is very fast.
Applicability]
 The packet sent by TCP has a serial number. After receiving the packet, the other party must send a feedback. If the packet has not received the feedback for a certain period of time, it will automatically execute the timeout and resend the packet. ThereforeThe biggest advantage of TCP is reliability.. Generally, TCP is used for webpage (HTTP), email (SMTP), remote connection (Telnet), and file (FTP) transmission.
 UDP is a message-oriented protocol, and no connection is required during communication. data transmission is naturally unreliable,UDP is generally used for multi-point communication and real-time data services.Such as voice broadcast, video, QQ, TFTP (simple file transfer), SNMP (Simple Network Management Protocol), RTP (Real-Time Transfer Protocol) Rip (route information protocol, such as stock market report, aviation information), DNS (Domain Name explanation ). Focus on speed and smoothness.
Three-way handshake for TCP Connection]
  To understand TCP, you must know "three-way handshake, four-way handshake". The so-called three-way handshake means that the connection that must be established before sending data is called three-way handshake. After the handshake is complete, it starts to be sent, this is what connection-oriented means.
 The first handshake: the client sends the SYN Packet (SYN = J) to the server and enters the syn_send status, waiting for confirmation from the server;The second handshake: the server receives the SYN Packet and must confirm the customer's Syn (ACK = J + 1). At the same time, it also sends a SYN Packet (SYN = K ), That is, the SYN + ACK packet. At this time, the server enters the syn_recv state;The third handshake: the client receives the server's SYN + ACK package and sends the ACK (ACK = k + 1) Confirmation package to the server. After this package is sent, the client And the server enters the established status and completes three handshakes.
   
(4) FTP

 File Transfer Protocol (FTP) is the protocol for transferring files between two computers on the TCP/IP network. FTP is one of the earliest protocols used on the TCP/IP network and the Internet, it belongs to the application layer of the network protocol group. The FTP client can send commands to the server to download files, upload files, and create or change directories on the server.


Original translated from: http://blog.sina.com.cn/s/blog_6dc41baf01012wzf.html

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.