TCP-based Network Communication
** Key content ** 1. Use InetAddress (IP Address class)
This class is a bit odd and does not provide a constructor. There are two static methods for instantiation.
· GetByName (String host) obtains the corresponding InetAddress object through the host name
· GetByAddress (byte [] addr) obtains the corresponding InetAddress object through the IP address.
GetCononicalHostName () obtains the Fully Qualified Domain Name of the IP address.
GetHostAddress () returns the IP address string
GetHostName () obtains the Host Name of the IP address.
GetLocalHost () gets the InetAddress instance corresponding to the local IP Address
Obtain the IP address of the local machine:
InetAddress address = InetAddress.getLocalHost();String ipAddr = address.getHostAddress();System.out.println(ipAddr);
2. TCP-based Network Programming
Establish a Socket at each end of the communication to form a virtual network link between two points. Programs at both ends can communicate through virtual links.
Java uses the Socket object to represent the communication ports at both ends, and uses the IO stream generated by the Socket for network communication.
IP protocol: transmits messages from one host to another. messages are divided into packets during transmission.
The problem that may occur during the transmission of data groups cannot be solved. To avoid errors. Introduce the TCP protocol.
TCP protocol: End-to-End protocol. When two hosts are connected, TCP establishes a connection for them: a virtual link (Socket) for sending and receiving data)
Data can be correctly transmitted to the other end. Because when the data is transmitted to the other end, the receiver sends a confirmation message. If no confirmation message is received, it will be resend.
Use ServerSocket to create a TCP Server
It is used to listen for Socket connections from the client. If there is no connection, it will remain in the waiting state.
Method for listening to connection requests from clients:
When Socket accept () receives a Socket request, a Socket corresponding to the client request is returned.
Create a ServerSocket
ServerSocket (int port) specifies the port to create a ServerSocket
After use, call the close method of ServerSocket to close the Socket.
To keep receiving client requests, accept is usually placed in an endless loop.
Example:
ServerSocket ss = new ServerSocket (20000 );
While (true)
{
Socket socket = ss. accept ();
// Communicate through Socket
}
Ss. close ();
Use Socket for communication
Directly use the Socket object on the client.
Socket (InetAddress, int port)
Create a Socket to connect to the remote host and remote port
For example, Socket s = new Socket ("192.1.1.168", 30000 );
The getInputStream input stream extracts data from the Socket.
GetOutputStream output stream outputs data to Socket
Example:Send messages to mobile phones using the local host as the server
ServerSocket ss = new ServerSocket (6500); while (true) {// obtain the Socket s = ss of the corresponding port number. accept (); // obtain the output stream OutputStream OS = s. getOutputStream (); OS. write (hello ~. GetBytes (UTF-8); OS. close (); s. close ();}
The mobile phone receives information as a client.
// The IP address here should be the Server IP address, and the port number must be the same as the server port Socket = new socket (192.168.1.1, 6500); InputStream is = Socket. getInputStream (); // It is a normal IO operation. If this parameter is left blank ~~