In the "Android Network Programming" series, the previous Java communication is described in the bottom of the general description, before we understand the TCP/IP communication family Architecture and its principles, then we begin to understand the TCP/IP protocol layer based on the socket abstraction layer. This article will give us a clear understanding of and learn to use sockets.
What is a socket? And how does it work? What is the form of its expression? Wait for these questions, this article will be gradually revealed. 1>. What is a socket socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces that you can use to establish TCP connections and so on, while HTTP is an application-level protocol. It is also actually built on top of the TCP protocol. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol. However, the socket itself is not a protocol, it is only the TCP/IP protocol encapsulation, is 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. 2> How does it work? First we look at its internal communication principle, One host application C to be able to communicate with another host's application S, must pass socket establishes the connection, and the build Socket connection must require the underlying tcp/ip protocol to establish TCP connect. Establishing a TCP connection requires the underlying IP protocol to address the hosts in the network. We know that the IP protocol used by the network layer can help us to locate the target host based on the IP address, but there may be multiple applications running on a single host, and how to communicate with the specified application is going through TCP Or the address of UPD is specified by the port number. This allows a Socket instance to uniquely represent the communication link for an application on one host. establishing a socket connection requires at least one pair of sockets, one running in client program C, called Clientsocket, and the other running in the server-side program s, called ServerSocket.
The connection between sockets is divided into three steps: The server waits for listening, the client requests, and the connection is established.
1. The server waits for listening: ServerSocket does not target a specific client socket, but is in a state of waiting for the connection, monitoring the network status in real time, waiting for the client's connection request. The process is a blocking process until a client is linked.
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. before the constructor of the Create Socket instance returns correctly, the three-time handshake protocol that will be performed TCP after the,tcp handshake protocol is complete, The socket instance object will be created, or the IOException error will be thrown. The specific three-time handshake protocol can be seen in the "Android Network programming Javasecurity" JCE (SSL/TLS). 3. What is the form of its expression? Above is the Java I/O category, we can see that I/O programming we use sockets and ServerSocket, communication data by the channel between the socket, followed by the call Inputstream.read () Method is blocked, it waits until the data arrives (or times out) to return, and similarly, when the Serversocket.accept () method is called, it is blocked until a client connection is returned, and after each client connects, the server initiates a thread to process the client's request. When the client is a long time, a large number of processing threads are created. And each thread consumes the stack space and some CPU time. So Java NI/O programming comes into being, it can solve these very well. NI/O programming can be said to be non-blocking I/o,java NiO uses two-way channels (channel) for data transmission, rather than one-way stream (stream), on the channel can register the events we are interested in. There are four types of events: Read, write, client link event, server receive link event. ni/o It consists of a dedicated thread that handles all I/O events and is responsible for distributing them. events are triggered when they arrive, rather than synchronized to monitor events. Threads communicate through wait,notify and other means. Ensure that each context switch is meaningful. Reduce unnecessary thread switching. channels, buffers, selectors three NIO programming core class. About NIO This paper is not much to say, the general introduction. In addition, its other form of expression is on its original basis (socketand ServerSocket) plus a layer of security is the SSL/TLS protocol layer. An introduction to this layer can be found in the "Javasecurity (SSL/TLS) for Android network programming." 4. Summary above, the socket and its related concepts are described roughly. In the actual development, we may encounter a variety of problems, encounter problems can not be photographed, it is terrible that we do not understand the problem. More and more knowledge is on its own accumulation.
Android Network programming Series A socket abstraction layer