Network programming
Basic knowledge of network
-
Network Communications:
Data transmission between two devices is realized through the network, Java Net package provides a series of classes and interfaces to complete network communication -
According to different network coverage, the network is classified as follows:
(1) LAN: minimum coverage, covering only one teacher or one computer room
(2) Man: it covers a large area and only covers one city
(3) Wan: it has the largest coverage and can cover the whole country or even the whole world. The world wide web is the representative of Wan -
ip address: used to uniquely identify each computer in the network
(1) View ip address: ipconfig
(2)ip address representation: dotted decimal XX xx. xx. xx
(3) Range of each decimal number: 0 ~ 255
(4)ip address composition: network address + host address
(5)IPv6 is the next generation IP protocol designed by the Internet engineering task to replace IPv4. Its number of addresses claims to be an address for every grain of sand in the world; Because the biggest problem of IPv4 lies in the limited network address resources, the use of IPv6 can not only solve the problem of the number of network address resources, but also solve the obstacles of various access devices to the Internet -
Domain name: map the ip address into a domain name, such as www.baidu.com com
-
Port number: used to identify a specific network program on the computer. It is identified in the form of an integer. The range of 0 ~ 65535 and 0 ~ 1024 has been occupied. Common network program port numbers: tomcat: 8080, mysql: 3306, oracle: 1521
-
Network communication protocol:
Short for TCP/IP(Transmission Control Protocol/Internet Protocol), i.e. network communication protocol; Internet is composed of IP protocol in network layer and TCP protocol in transport layer -
TCP protocol:
(1) Before using TCP protocol, a TCP connection must be established to form a data transmission channel
(2) Before transmission, the method of "three handshakes" is reliable
(3) Two application processes of TCP protocol communication: client and server
(4) A large amount of data can be transmitted in the connection
(5) After transmission, the established connection needs to be released, which is inefficient -
UDP protocol:
(1) Encapsulate the data, source and destination into data packets without establishing a connection
(2) The size of each datagram is limited to 64K
(3) Because there is no connection, it is unreliable
(4) At the end of sending data, there is no need to release resources (because it is not connection oriented), which is fast -
InetAddress class related methods
public class InetAddress_ { public static void main(String[] args) throws UnknownHostException { //1. Get the InetAddress object of this machine InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); //2. Obtain the InetAddress object according to the specified host name InetAddress l = InetAddress.getByName("L"); System.out.println(l); //3. Obtain an InetAddress object according to the domain name InetAddress host = InetAddress.getByName("www.baidu.com"); System.out.println(host); //4. Reverse obtain the corresponding host address according to the InetAddress object String hostAddress = host.getHostAddress(); System.out.println(hostAddress); //5. Reverse obtain the corresponding host name / domain name according to the InetAddress object String hostName = host.getHostName(); System.out.println(hostName); } }
TCP network programming
-
Basic introduction to Socket:
(1) Socket is widely used to develop network applications, so that it has become a de facto standard
(2) Socket s should be provided at both ends of the communication, which is the end point of communication between two machines
(3) Network communication is actually the communication between sockets
(4)Socket allows the program to treat the network connection as a stream, and the data is transmitted between the two sockets through IO
(5) Generally, the application that initiates communication actively belongs to the client, and the one waiting for communication request is the server -
TCP network communication programming:
(1) Network communication based on client server
(2) The bottom layer uses TCP/IP protocol
(3) Application scenario: the client sends data, and the server accepts and displays the console
(4) TCP programming based on Socket -
TCP byte stream programming
/** * Server */ public class SocketTCP01Server { public static void main(String[] args) throws IOException { //Listen on the 9999 port of this machine and wait for connection //Note: no other service is listening for 9999 on this machine ServerSocket serverSocket = new ServerSocket(9999); System.out.println("Waiting for connection"); //When no client is connected to port 9999, the program will block and wait for connection //If there is a client, a Socket object will be returned and the program will continue Socket socket = serverSocket.accept(); System.out.println("Server" + socket.getClass()); //Through socket Getinputstream() reads the data written to the data channel by the client and displays it InputStream inputStream = socket.getInputStream(); //read byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = inputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readLen)); } //Write data and send it back to the Client //Gets the output stream associated with the socket OutputStream outputStream = socket.getOutputStream(); outputStream.write("hello,client".getBytes()); //End tag must be set socket.shutdownInput(); //The stream object and socket must be closed outputStream.close(); inputStream.close(); socket.close(); serverSocket.close(); } } /** * client */ public class SocketTCP01Client { public static void main(String[] args) throws IOException { //Connect server Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("client" + socket.getClass()); //After connecting, generate a Socket through Socket Getoutputstream(), send hello OutputStream outputStream = socket.getOutputStream(); //Through the output stream, write data to the data channel outputStream.write("hello".getBytes()); //End tag must be set socket.shutdownOutput(); //Read the data sent by the server InputStream inputStream = socket.getInputStream(); byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = inputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readLen)); } //The stream object and socket must be closed inputStream.close(); outputStream.close(); socket.close(); } }
-
TCP character stream programming
/** * Server */ public class SocketTCP02Server { public static void main(String[] args) throws IOException { //Listen on the 9999 port of this machine and wait for connection //Note: no other service is listening for 9999 on this machine ServerSocket serverSocket = new ServerSocket(9999); System.out.println("Waiting for connection"); //When the client is not connected, 9999 will be blocked //If there is a client, a Socket object will be returned and the program will continue Socket socket = serverSocket.accept(); System.out.println("Server" + socket.getClass()); //Through socket Getinputstream() reads the data written to the data channel by the client and displays it InputStream inputStream = socket.getInputStream(); //read BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s = bufferedReader.readLine(); System.out.println(s); //Write data and send it back to the Client //Gets the output stream associated with the socket OutputStream outputStream = socket.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("hello,client"); bufferedWriter.newLine(); bufferedWriter.flush(); //The stream object and socket must be closed bufferedReader.close(); bufferedWriter.close(); socket.close(); serverSocket.close(); } } /** * client */ public class SocketTCP02Client { public static void main(String[] args) throws IOException { //Connect server Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("client" + socket.getClass()); //After connecting, generate a Socket through Socket Getoutputstream(), send hello OutputStream outputStream = socket.getOutputStream(); //Through the output stream, write data to the data channel BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("hello"); bufferedWriter.newLine();//Insert a newline character to indicate the end of writing. Note that the other party must be required to use readLine()!!! bufferedWriter.flush();//Using character stream requires manual refresh //Read the data sent by the server InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s = bufferedReader.readLine(); System.out.println(s); //The stream object and socket must be closed bufferedReader.close(); bufferedWriter.close(); socket.close(); } }
-
netstat instruction:
(1)netstat -an can view the current host network, including port listening and network connection
netstat -anb can check which program is listening on the port
(2)netstat -an|more can be displayed in pages. Enter a space to continue the display
(3) It is required to execute under dos console
Note:
Listening indicates that a port is listening
If an external program (client) is connected to the port, a connection message ESTABLISHED is displayed
-
TCP connection:
When the client connects to the server, in fact, the client communicates with the server through a port, which is allocated by TCP/IP