* * Key content **1. Using inetaddress (IP address Class)
This class is a bit of a rarity and does not provide a construction method. Instead, there are two static methods to instantiate.
Getbyname (String host) gets the corresponding InetAddress object from the host name
getbyaddress (byte[] addr) Get the corresponding InetAddress object by IP address
Getcononicalhostname () Get the fully qualified domain name of the IP address
Gethostaddress () returns the IP address string
GetHostName () Gets the host name of the IP address
Getlocalhost () Gets the inetaddress instance corresponding to the native IP address
Get the IP address of this machine:
InetAddress address = InetAddress.getLocalHost();String ipAddr = address.getHostAddress();System.out.println(ipAddr);
2. Network programming based on TCP protocol
A socket is established at each end of the communication to form a virtual network link between two points. The programs on both ends can communicate through the virtual link.
Java uses the socket object to represent the communication ports on both ends and network traffic through the IO stream generated by the socket.
IP protocol: is responsible for the transmission of messages from one host to another, the message is split into a packet during the transmission process.
Problems that may occur during the transmission of data groupings cannot be resolved. For error-free. Introduce the TCP protocol.
TCP protocol: End-to-end protocol, when two hosts are connected, the TCP protocol establishes a connection for them: Virtual link (Socket) for sending and receiving data
The data can be transferred to the other end correctly. Because, when data is transferred to the other end, a confirmation message is sent to the receiving end. If you do not receive a confirmation message, it will be re-sent.
using ServerSocket to create a TCP server side
Used to listen for a socket connection from the client, and if there is no connection, it will remain in the waiting state.
To listen for a method from a client connection request:
The socket accept () receives a socket request and will return a socket corresponding to the client request.
Create ServerSocket
ServerSocket (int port) specifies the port to create a ServerSocket
After use, call ServerSocket's Close method to close the socket.
To be able to receive client requests all the time, the accept is usually placed in a dead loop
Example:
ServerSocket ss = new ServerSocket(20000);
while(true)
{
Socket socket = ss.accept();
//通过Socket进行通信
}
ss.close();
using the socket for communication
Use the socket object directly on the client.
Socket (Inetaddress,int Port)
Create a socket that connects to a remote host, remote port
For example: socket s = new socket ("192.1.1.168", 30000);
getInputStream the input stream to fetch data from the socket
Getoutputstream output flow to socket output data
Example: sending a message to a phone via a local host as a server
ServerSocket ss = new ServerSocket(6500);while(true){ //获取相应端口号的Socket Socket s = ss.accept(); //获取输出流 OutputStream os = s.getOutputStream(); os.write("你好吖~".getBytes("utf-8")); os.close(); s.close();}
Cell phone receives information as a client
//此处的IP地址应该是服务端的IP,端口号也要和服务端的端口相同new Socket("192.168.1.1",6500is = socket.getInputStream();//之后就是正常的IO操作,不写了~~
Copyright notice: Just out of the original content of the pot, I hope you have help ~
Network communication based on TCP protocol