The relationship between the Android network programming--socket TCP/IP UDP http

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

Network seven layer from the bottom up to the physical layer, Data link layer, network layer, transport layer, Session layer, presentation layer and application layer, the general programmer is the most contact with the application layer and transport layer, and then down is the so-called media layer, not the object of our study.

The following is the application layer, Transport layer, network layer, Link Layer Communication protocol overview map. What we often come into contact with are:

HTTP protocol: The application layer protocol, and the HTTP protocol is based on TCP connection, the main solution is how to wrap the protocol

TCP Protocol: Transport layer protocol, usually also known as TCP/IP protocol, primarily addresses how data is transmitted over the network

UDP Protocol: Transport layer Protocol, User Datagram Protocol, unreliable protocol, is only responsible for the application layer of protocol data transfer to the IP layer of the datagram, regardless of whether the data arrives.

IP protocol: Network layer.

Socket connection: Long connection

HTTP connection: Short connection

The following is a detailed introduction to several concepts and principles in network communication:

(1) socket (socket):

Socket (socket) is the cornerstone of communication and is the basic operating unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process and contains five kinds of information that must be used for network communication: the protocol that the connection uses, 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 communicates data through the transport layer,TCP encounters a problem that provides concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may require data to be transmitted over the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide a socket (socket) interface for applications to interact with the tcp/IP protocol. The application layer can communicate with the transport layer through the socket interface, differentiate the communication from different application processes or network connections, and realize the concurrent service of data transmission.

PS: To be blunt, a socket is a set of calling interfaces (APIs) that encapsulate a network interface for TCP/IP development, and through a socket, we can use the TCP/IP protocol.

(2) 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), That 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 the three-time 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-time handshake".

(3) Socket connection :

Since the socket connection is usually a TCP connection, once the socket connection is established, the communication parties can start sending data content to each other until the two sides are disconnected. However, in real network applications, the client-to-server communication often needs to traverse multiple intermediary nodes, such as routers, gateways, firewalls, and so on, most firewalls will turn off long inactive connections and cause the Socket connection to be disconnected, so it needs to be polled to tell the network that the connection is active.

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.

(4) HTTP connection :

HTTP protocol is an application based on TCP protocol, the HTTP connection uses "Request-response" method, not only need to establish a connection in the request, but also require the client to make a request to the server, the server side can reply to the data. After the request is finished, the connection is released actively. The process from establishing a connection to closing a connection is called a "one-time connection." Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", which requires constant connection requests to the server to maintain the client program's online status. As a general practice, there is no need to obtain any data immediately, and the client will keep a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.

(5) The relationship between the TCP/IP protocol and the HTTP protocol :

The TPC/IP protocol is the Transport Layer protocol, which mainly solves how data is transmitted in the network, and HTTP is the application layer protocol, which mainly solves how to wrap the data, while socket is the encapsulation and application of TCP/IP protocol (programmer level). The HTTP protocol is actually built on top of the TCP/IP protocol. About the relationship between TCP/IP and HTTP protocols:

"When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but in that case, 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., also You can 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. ”

(6) The relationship between socket and TCP/IP protocol :

Socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. In fact, the socket is not necessarily associated with the TCP/IP protocol. The socket programming interface is designed to adapt to other network protocols as well. So, the advent of sockets just 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, such as Create, listen, connect, accept, send, Read and write, and so on. The network has a section on the socket and TCP/IP protocol relationship is relatively easy to understand:

"TCP/IP is just a stack of protocols, just like operating systems, which must be implemented in a specific way, as well as providing an external interface for operations." This is like the operating system will provide a standard programming interface, such as the Win32 programming interface, TCP/IP should also provide the interface for programmers to do network development, this is the socket programming interface. ”

(7) The relationship between TCP/IP protocol, HTTP protocol, socket three :

In fact, the transport layer TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, it just provides a TCP or UDP programming interface.

(8) The difference between the TCP protocol and the UDP protocol :

TCP---Transmission Control Protocol, providing a connection-oriented, reliable byte-stream service. Before the customer and the server Exchange data with each other, a TCP connection must be established between the two parties before the data can be transferred. TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other. Ideally, once aTCP connection is established, the TCP connection is maintained until either side of the communication actively shuts down the connection . Server and client can proactively initiate a request to disconnect a TCP connection when disconnected

UDP---User Datagram Protocol, is a simple non-connected Transport layer protocol for datagrams. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is fast

TCP sends the packet has the serial number, the other party receives the packet to give a feedback, if has not received the feedback to automatically perform the time-out resend, therefore the TCP biggest advantage is reliable . General Web page (HTTP), Mail (SMTP), remote connection (Telnet), file (FTP) transfer is used with TCP

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 multipoint communication and real-time data services , such as voice broadcast, video, QQ, TFTP (Simple File transfer), SNMP (Simple Network Management Protocol), RTP (Real-time delivery protocol) RIP (Routing Information protocol such as reporting stock market, aviation information), DNS (Domain name interpretation). Pay attention to the smooth speed.

(9) FTP protocol :

The File Transfer Protocol (Files Transfer Protocol, FTP) is the protocol that transmits files to two computers on a TCP/IP network, and FTP is one of the earliest protocols used on TCP/IP networks and the Internet, which belongs to the application layer of the Network protocol group. The FTP client can issue commands to the server to download files, upload files, and create or change directories on the server.

The relationship between the Android network programming--socket TCP/IP UDP http

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.