Talk about sockets, TCP/IP, HTTP, FTP, and network programming __socket

Source: Internet
Author: User
Tags ack ftp client file transfer protocol

1 What's all this?

Since it is a network transmission, involving several systems, the first thing to consider is how to accurately navigate to one or several hosts on the network, and the other is how to reliably and efficiently

of data transfer. The TCP/IP protocol will be used here. 1.1 TCP/IP protocol group

The TCP/IP protocol is composed of the IP protocol of the network layer and the TCP protocol of the Transport layer.

IP layer is responsible for the location of the network host, the routing of Data transmission, the IP address can uniquely determine a host on the Internet.

The TCP layer is responsible for application-oriented reliable or unreliable data transmission mechanisms, which are the main objects of network programming.

TCP/IP is a protocol group that can be divided into three tiers: the network layer, the transport layer, and the application layer:

Network layer: IP protocol, ICMP protocol, ARP protocol, RARP protocol and BOOTP protocol;

Transport layer: TCP protocol and UDP protocol;

Application layer: FTP, HTTP, TELNET, SMTP, DNS and other protocols;

HTTP is the application layer protocol, and its transmission is packaged into TCP protocol transmission. HTTP can be implemented with a socket. A socket is a programming API that implements the Transport layer protocol, either TCP or UDP. 1.2 TCP

The Tcp-Transmission Control Protocol provides a connection-oriented, reliable byte throttling service. Before the client and server Exchange data with each other, a TCP connection must be established between the two parties before

to transmit data. TCP provides timeouts, discards duplicate data, verifies data, and controls traffic to ensure that data is transferred from one end to the other. Ideally, TCP connects a

The TCP connection will be maintained until either side of the communication actively closes the connection. Both the server and the client can initiate a disconnected TCP connection when disconnected

The request received.

TCP is a connection-oriented protocol to ensure reliable transmission. Through the TCP protocol transmission, obtains is a sequential error-free data stream. Two pairs of pairs of sender and receiver

A connection must be established between the sockets to communicate on the basis of the TCP protocol, and when one socket (usually the server socket) waits for the connection to be made, the other socket

You can require a connection, once these two sockets are connected, they can be two-way data transmission, both sides can send or receive operations.

TCP Features: TCP is a connection-oriented protocol, through three handshake to establish a connection, communication completed to remove the connection, because TCP is a connection-oriented protocol, so can only be used for point-to-point communication. And it also takes time and overhead to establish a connection. TCP transmission data is not limited in size, and large data transmission is carried out. TCP is a reliable protocol that guarantees that the receiver can receive all the data sent by the sender in a complete and correct manner.

To understand TCP, be sure to know "three handshake, four times goodbye" so-called three handshake, is to send data must be established before the connection is called three handshake, shaking hands after the beginning of hair, this

That is, the connection-oriented meaning.

First handshake: The client sends the SYN packet (SYN=J) to the server, and enters the Syn_send state, waiting for the server to confirm;

Second handshake: The server received the SYN packet, must confirm the customer's SYN (ACK=J+1), but also send a SYN packet (syn=k), that is Syn+ack packet, at this time the server into the SYN_RECV state;

Third handshake: The client receives the server Syn+ack packet, sends the confirmation packet ack (ACK=K+1) to the server, this packet sends completes, the client and the server enters established state, completes three times handshake;

"Applicable Situation"

TCP sent packets have serial number, the other side received the packet to give a feedback, if more than a certain amount of time has not received feedback automatically execution timeout, so the biggest advantage of TCP is reliable. One

-like web pages (HTTP), Mail (SMTP), remote connection (Telnet), file (FTP) transfer with TCP

TCP has great vitality in network communications, such as remote Connection (Telnet) and file Transfer (FTP) that require indefinite length of data to be transmitted reliably. But the reliable transmission is to

Pay the price, the test of the correctness of the data content must occupy the processing time of the computer and the bandwidth of the network, so the TCP transmission efficiency is not as high as UDP. 1.3 UDP

The udp-User Datagram Protocol is a simple, connectionless transport-level protocol for datagrams. UDP does not provide reliability, it simply sends an application to the IP layer of the datagram

, but there is no guarantee that they will reach their destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and does not have the mechanism such as time out to repeat, therefore transmits

The speed of transmission is very fast.

UDP is a connectionless protocol, and each datagram is a separate piece of information, including a complete source address or destination address, which is transmitted to the destination on the network on any possible path

, so it is impossible to reach the destination, the time of arrival and the correctness of the content is not guaranteed.

UDP features: UDP is for connectionless communication protocol, UDP data including destination port number and source port number information, because the communication does not need to connect, so can realize broadcast send. UDP transmits data with a size limit, each transmitted datagram must be limited to 64KB. UDP is an unreliable protocol, and datagrams sent by the sender do not necessarily reach the receiver in the same order.

"Applicable Situation"

UDP is a message-oriented protocol, communication does not need to establish a connection, the transmission of data is naturally unreliable, UDP is generally used for multicast and real-time data services, such as voice broadcasting,

Video, QQ, TFTP (Simple File transfer), SNMP (Simple Network Management Protocol), RTP (Real-time Transfer Protocol) RIP (Routing Information Protocol, such as reporting the stock market, airmail

), DNS (Domain name interpretation). Focus on speed and fluency.

UDP is simple and requires less monitoring, so it is usually used for client/server applications in decentralized systems with high reliability on the LAN. Video conferencing systems, for example, do not require

Audio and video data is absolutely correct, as long as the consistency is guaranteed, in which case it is obviously more reasonable to use UDP. 1.4 Socket

The socket is also commonly referred to as a socket, which describes the IP address and port, and is a handle to a communication chain. Two programs on the network realize the intersection of data through a two-way communication connection

In exchange, the end of this two-way link is called a socket, and a socket is uniquely determined by an IP address and a port number. Applications typically make requests to the network through "sockets"

or answer a network request. Socket is a very popular programming interface of TCP/IP protocol, but the type of protocol supported by the socket is not only TCP/IP, so the

is not necessarily connected. In the Java environment, socket programming mainly refers to the network programming based on TCP/IP protocol.


Socket communication process: The server listens to a port for connection requests, the client sends a connection request to the server, and the server receives the connection request to send a message to the client, so

A connection is established. Both the client and the server can communicate with each other by sending messages to each other.


The socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, which puts the complex

TCP/IP protocol family hidden behind the socket interface, for users, a set of simple interface is all, let the socket to organize data to meet the specified protocol.


Since the socket connection is typically a TCP connection, once the socket connection is established, the two sides of the communication can start sending the data to each other until the connection is disconnected. But in the actual

In the network application, the communication between the client and the server often needs to traverse multiple intermediate nodes, such as routers, gateways, firewalls and so on, most firewalls will shut down for a long time by default.

An inactive connection causes the Socket connection to be disconnected, so a polling is required to tell the network that the connection is active. Socket (socket) Concept: socket (socket) is the cornerstone of communication, the TCP/IP protocol to support the basic operating unit of network traffic. It is the abstract representation of the endpoint in network communication, including the five kinds of information that must be used for network communication: The Protocol of the connection, the IP address of the local host, 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 is communicating through the transport layer, TCP encounters problems that simultaneously provide concurrent services for multiple application processes. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide socket (socket) interfaces for applications to interact with the TCP/IP protocol. The application layer can communicate with the transport layer through the socket interface to distinguish the communication from different application processes or network connections, and realize the concurrent service of data transmission. Establish a socket connection: Establish a socket connection requires at least a pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket. The connection process between sockets is divided into three steps: Server listening, client requests, connection confirmation.

Server listening: server-side sockets do not locate specific client sockets, but are in the state of waiting for connections, real-time monitoring of network status, waiting for client connection requests;

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

Connection confirmation: When the server-side socket supervisor hears or says that a connection request for a client socket is received, in response to the client socket request, create a new thread, the server-side socket description to the client, once the client confirmed this description, the two sides formally establish a connection. While the server-side socket continues to be listening, it continues to receive connection requests from other client sockets. When a socket connection creates a socket connection with a TCP connection, you can specify the transport layer protocol used, which can support different transport layer protocols (TCP or UDP), which is a TCP connection when the TCP protocol is used to connect.

"Applicable Situation"

In many cases, a server-side initiative is required to push data to the client, keeping the client and server data in real-time and synchronized. At this point if the two sides to establish a socket connection, the server can be

To transmit data directly to the client; 1.5 HTTP

HTTP protocol is an application based on TCP protocol, HTTP connection uses "request-response" way, not only need to establish TCP connection when request, and need guest

When the client makes a request to the server, the request contains the request method, URI, protocol version, and the associated MIME-style message, and the server can reply to the data, including the protocol version of the message.

This, a success and failure code, and related MIME style messages. The connection is actively released after the request is completed. The process from establishing a connection to closing a connection is called a "once connection."

Because HTTP actively frees the connection after each request, HTTP connections are a "short connection" that keeps the client online and needs to be continuously sent to the server

The connection request. The usual practice is to get no data at once, and the client maintains a "Stay connected" request to the server at regular intervals, and the server

When the request is received, a reply is made to the client, indicating that the client is "online". If the server can not receive the client's request for a long time, the client is considered "offline" if the client is long

Time does not receive a reply from the server, the network is considered disconnected.


Http/1.0 creates a new TCP link for each HTTP request/response, so a page containing HTML content and pictures will need to establish multiple short TCP links. One

The establishment of the secondary TCP link will require 3 handshake.


In addition, in order to obtain the appropriate transmission speed, TCP will need to spend additional loop link time (RTT). The creation of each link requires this recurring overhead, and it does not carry

The actual useful data, only guarantees the link the reliability, therefore http/1.1 proposed the sustainable link realization method. http/1.1 will only establish a TCP link once and use it repeatedly

It transmits a series of request/response messages, thus reducing the number of links established and the frequent link overhead.


Conclusion: HTTP is an application layer protocol, and its transmission is packaged into TCP protocol transmission. HTTP can be implemented with a socket. A socket is a programming API that implements the Transport layer protocol, either TCP or UDP.

"Applicable Situation"

If the two sides establish an HTTP connection, the server needs to wait until the client sends a request before the data can be passed back to the client, so the client sends the connection to the server at timed intervals.

Ask, not only to stay online, but also in the "ask" whether the server has new data, if there is to pass the data to the client. 1.6 FTP

File Transfer Protocol (Files Transfer Protocol, FTP) is the protocol for transferring files on two computers on a TCP/IP network, the earliest used on TCP/IP networks and the Internet

protocol, which belongs to the application layer of the Network protocol group. The FTP client can send commands to the server to download files, upload files, create or change directories on the server. 2 N-tier switching technology 2.1 Two-layer Exchange

Switching principle: Based on the second layer of Data link layer MAC address to achieve end-to-end data exchange;

Work Flow:

(1) A switch port receives the packet, reads the source MAC address, obtains the source MAC address machine to connect the port;

(2) Read the destination MAC address and find the corresponding port in the Address table;

(3) If the Address table has a destination MAC address corresponding to the port, directly copy data to this port;

(4) If the Address table does not have a destination MAC address corresponding port, broadcast all the ports, when the target machine response, update the Address table, the next time do not need to broadcast;

The continuous cycle of the above process, the whole network of MAC address information can be learned, the two-tier switch to learn and maintain its address table. The second layer switch transmits the data according to the MAC selection port, and the algorithm is simple, it is convenient to use the inexpensive chip to realize, and the speed is fast.

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.