Android socket communication

What is Socket?
Socket, used to describe the IP address and port, is the handle of a communication chain. The host on the Internet runs multiple service software and provides several services at the same time. Each service opens a Socket and binds it to It is fixed on one port, and different ports correspond to different services.
Constructing Socket
The construction method of Socket has the following overloaded forms:
(1) Socket()

(2)Socket(InetAddress address, int port)throws UnknownHostException,IOException

(3)Socket(InetAddress address, int port, InetAddress localAddr, int localPort)throws IOException

(4)Socket(String host, int port) throws UnknownHostException,IOException

(5)Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException

The usage of each construction method is as follows:

Set the timeout time for waiting for a connection to be established:
Socket socket=new Socket();

SocketAddress remoteAddr=new InetSocketAddress(“localhost”,8000);

//The timeout period for waiting for a connection to be established is 1 minute

socket.connect(remoteAddr, 60000);

Set the address of the server:
Socket(InetAddress address, int port)

Socket(String host, int port)

The InetAddress class represents an IP address, and its usage is as follows:

//returns the IP address of the local host

InetAddress addr1=InetAddress.getLocalHost();

//Returns the IP address representing "222.34.5.7"

InetAddress addr2=InetAddress.getByName(“222.34.5.7”);

// Return the IP address whose domain name is "www.javathinker.org"

InetAddress addr3=InetAddress.getByName(“www.javathinker.org”);

Set the address of the client:
In a Socket object, it contains both the IP address and port information of the remote server and the IP address and port information of the local client. By default, the IP address of the client comes from the host where the client program is located, and the port of the client is randomly assigned by the operating system. The Socket class also has two constructors that allow explicit setting of the client's IP address and port:

Socket(InetAddress address, int port, InetAddress localAddr, int localPort)throws IOException

Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException

Exceptions that may be thrown when the client connects to the server:
When the Socket construction method requests to connect to the server, the following exceptions may be thrown:

l UnknownHostException: If the name or IP address of the host cannot be recognized, this exception will be thrown.

l ConnectException: If there is no server process listening to the specified port, or the server process refuses to connect, this exception will be thrown.

l SocketTimeoutException: If waiting for the connection to time out, this exception will be thrown.

l BindException: If the Socket object cannot be bound to the specified local IP address or port, this exception will be thrown.

Obtaining Socket information
The following methods are used to obtain information about Socket:

l getInetAddress(): Get the IP address of the remote server.

l getPort(): Get the port of the remote server.

l getLocalAddress(): Get the client's local IP address.

l getLocalPort(): Get the client's local port.

l getInputStream(): Get the input stream. If the Socket is not connected, or has been closed, or the input stream has been closed by the shutdownInput() method, then this method will throw an IOException.

l getOutputStream(): Get the output stream. If the Socket is not connected, or has been closed, or the output stream has been closed by the shutdownOutput() method, then this method will throw an IOException.

Understanding the purpose of Socket
has said a lot about how to use socket and what is it used for?
view image

View the picture
After looking at the picture, I probably understand that the socket is the method used to communicate with the server.

Important Socket API
Important Socket API: java.net.Socket inherits from java.lang.Object, has eight constructors, and there are not many methods. The three most frequently used methods are introduced below. For other methods, you can refer to JDK document.

1) The Accept method is used to generate "blocking", until a connection is received, and a client socket object instance is returned. "Blocking" is a term that makes the program run temporarily "stay" in this place until a session is generated, and then the program continues, usually "blocking" is generated by a loop.
2) The getInputStream method obtains the network connection input and returns an InputStream object instance at the same time.
3) The other end of the getOutputStream method connection will get input and return an OutputStream object instance at the same time. Note: Both the getInputStream and getOutputStream methods may generate an IOException, so try/catch must be used to capture it, because the stream objects they return usually have Used by another stream object.

Socket connection process
According to the way the connection is started and the target of the local socket connection, the connection process between sockets can be divided into three steps: server monitoring, client request, and connection confirmation.
Server monitoring: the server socket does not locate the specific client socket, but monitors the network status in real time out of waiting for the connection state.
Client request: refers to the connection request made by the client's socket, and the target to be connected is the server's socket. To this end, the client's socket must first describe the socket of the server it wants to connect to, point out the address and port number of the server-side socket, and then make a connection request to the server-side socket.
Connection confirmation: It means that when the server-side socket listens to or receives the connection request of the client-side socket, it responds to the request of the client-side socket, creates a new thread, and transfers the connection request of the server-side socket The description is sent to the client, and once the client confirms the description, the connection is established. The server-side socket continues to be in the listening state, and continues to receive connection requests from other client-side sockets.

Guess you like

Origin blog.csdn.net/weixin_44008788/article/details/129095370