Android Network Programming Series 1 Socket abstraction layer, android Network Programming

Source: Internet
Author: User

Android Network Programming Series 1 Socket abstraction layer, android Network Programming
In the Android network programming series, we have already roughly described the underlying communication layer of Java. We have learned about the TCP/IP communication family architecture and its principles, next we will begin to understand the Socket abstraction layer based on the TCP/IP protocol layer. This article will give us a clear understanding of and learn to use Socket.
What is Socket? How does it work? What is its form? Wait for these questions, and this article will be gradually revealed. 1>. what is Socket? It is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a set of interfaces that you can use to establish TCP connections, http is an application layer protocol, which is also based on the TCP protocol. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. for users, a set of simple interfaces are all, let the Socket organize the data to conform to the specified protocol. However, the Socket itself is not a protocol. It only encapsulates the TCP/IP protocol and is a call interface (API). Through the Socket, we can use the TCP/IP protocol. The emergence of Socket only makes it easier for programmers to use the TCP/IP protocol stack. It 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, let's take a look at the internal communication principle. To communicate with the application program S of another host, the application program C of one host must establish a connection through Socket, to establish a Socket connection, the underlying TCP/IP protocol must be used to establish a TCP connection. Establishing a TCP connection requires the underlying IP protocol to address hosts in the network. We know that the IP protocol used at the network layer can help us locate the target host based on the IP address, but multiple applications may run on one host, to communicate with a specified application, you must specify the TCP or UPD address, that is, the port number. In this way, a Socket instance can uniquely represent the communication link of an application on a host. To establish a Socket connection, you must have at least one Socket. One of the sockets runs in client C, which is called ClientSocket, And the other runs in server program S, which is called ServerSocket.

The connection process between sockets is divided into three steps: the server waits for listening, client requests, and the connection is established.

1. Server waiting for listening: ServerSocket does not locate the specific client socket, but is waiting for connection. It monitors the network in real time and waits for client connection requests. This process is a blocking process until a client is connected.

2. 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.

3. 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. Before the constructor for creating a Socket instance returns a correct result, the three-way handshake protocol of TCP will be executed. After the TCP handshake protocol is complete, the Socket instance object will be created. Otherwise, an IOException error will be thrown. For details about the three-way handshake protocol, refer to "Android network programming a JavaSecurity JCE (SSL/TLS)". 3. What is its form? All of the above are in the scope of java I/O. We can see that we use Socket and ServerSocket in I/O programming. The communication data depends on the channel between sockets, next, call InputStream. the read () method is blocked and will be returned until the data arrives (or times out). Similarly, the ServerSocket method is called. the accept () method is blocked until a client connection is established. After each client is connected, the server starts a thread to process the client request. When there are too many clients, a large number of processing threads will be created. Each thread occupies the stack space and some CPU time. Therefore, Java NI/O programming came into being. It can solve these problems well. NI/O programming can be called non-blocking I/O. java NIO uses a two-way channel for data transmission, rather than a one-way stream ), you can register events of interest on the channel. There are four types of events: read, write, client link event, and server side receive link event. NI/O it processes all I/O events by a dedicated thread and is responsible for distribution. The event is triggered at the time of arrival, rather than synchronously monitoring the event. Threads communicate with each other through wait and notify. Make sure that each context switch is meaningful. Reduces unnecessary thread switching. Channels, Buffers, and Selectors are three core NIO programming classes. I will not talk much about NIO in this document. I will give a rough introduction to it. In addition, its other form is to add a layer of security protection (SSL/TLS) to its original (socket and serversocket. For more information about this layer, see Android network programming-JavaSecurity JCE (SSL/TLS). 4. In summary, the Socket and its related concepts are roughly described. In actual development, we may encounter a variety of problems, which are not feasible. What is terrible is that we do not understand the problem. It is up to you to learn more deeply.

Related Article

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.