The teaching catalogue of day26:
26.01 Network Programming (Overview of Network Programming) (Understanding)
- A: Computer Network
- It refers to a computer system that connects multiple computers with independent functions in different geographical locations and their external devices through communication lines, realizes resource sharing and information transmission under the management and coordination of network operating system, network management software and network communication protocol.
- B: Network programming
- It means that data can be exchanged between programs running on different computers to realize network interconnection.
26.02 Network Programming (IP Overview of Three Elements of Network Programming) (Master)
- Unique identity of each device in the network
- Each network terminal has a separate address in the network, which we use to transmit data in the network.
- ipconfig: View native IP192.168.12.42
- ping: Test connection 192.168.40.62
- Local loop address: 127.0.0.1 255.255.255.255 is broadcast address
- IPv4: 4 bytes, 4 0-255. About 4.2 billion, 3 billion in North America and 400 million in Asia. It was exhausted in early 2011.
- IPv6:8 groups with 4 hexadecimal digits in each group.
- 1a2b:0000:aaaa:0000:0000:0000:aabb:1f2f
- 1a2b::aaaa:0000:0000:0000:aabb:1f2f
- 1a2b:0000:aaaa::aabb:1f2f
- 1a2b:0000:aaaa::0000:aabb:1f2f
- 1a2b:0000:aaaa:0000::aabb:1f2f
26.03 Network Programming (Port Number Overview of Three Elements of Network Programming) (Master)
- Unique identification of each program on the device
- Every network program needs to bind a port number. When transferring data, in addition to determining which machine to send, it also needs to specify which program to send.
- Port number range from 0-65535
- Writing network applications requires binding a port number, try to use 1024 or more, 1024 or less are basically occupied by system programs.
- Common ports
- mysql: 3306
- oracle: 1521
- web: 80
- tomcat: 8080
- QQ: 4000
- feiQ: 2425
26.04 Network Programming (Three Elements Protocol of Network Programming) (Master)
- A set of rules, standards, or conventions established for data exchange in computer networks.
- UDP
- For connectionless, data is insecure and fast. There is no distinction between client and server.
- TCP
* Connection-oriented (three handshakes), data security, slightly lower speed. It is divided into client and server.- Three handshakes: the client first initiates the request to the server, the server responds to the request, and transmits the data.
26.05 Network Programming (Socket Communication Principle Diagram) (Understanding)
- A:Socket socket overview:
- Only when IP addresses and port numbers with unique identifiers are combined on the network can a unique identifier socket be formed.
- Socket s are available at both ends of the communication.
- Network communication is actually the communication between Socket s.
- Data is transmitted between two Socket s through IO streams.
- Socket is created in the application program. It establishes a relationship with the driver through a binding mechanism and tells itself the corresponding IP and port.
26.06 Network Programming (UDP Transport) (Understanding)
- 1. send Send
- Create DatagramSocket with random port number
- Create Datagram Packet, specify data, length, address, port
- Sending Datagram Packet with Datagram Socket
- Close Datagram Socket
- 2. Receive Receive
- Create DatagramSocket, specify port number
- Create Datagram Packet, specify the array, length
- Receiving Datagram Packet with Datagram Socket
- Close Datagram Socket
- Getting data from Datagram Packet
- 3. Receiver obtains ip and port number
- String ip = packet.getAddress().getHostAddress();
- int port = packet.getPort();
26.07 Network Programming (UDP Transmission Optimization)
- Receive
DatagramSocket socket = new DatagramSocket(6666); //Creating a socket is equivalent to creating a wharf. DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //Creating packet s is equivalent to creating containers while(true) { socket.receive(packet); //Receiving cargo byte[] arr = packet.getData(); int len = packet.getLength(); String ip = packet.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(arr,0,len)); }
-
Send sender
DatagramSocket socket = new DatagramSocket(); //Creating a socket is equivalent to creating a wharf. Scanner sc = new Scanner(System.in); while(true) { String str = sc.nextLine(); if("quit".equals(str)) break; DatagramPacket packet = //Creating packet s is equivalent to creating containers new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666); socket.send(packet); //Deliver goods } socket.close();
26.08 Network Programming (UDP Transfer Multithreading)
-
A Send and receive in one window
public class Demo3_MoreThread { /** * @param args */ public static void main(String[] args) { new Receive().start(); new Send().start(); } } class Receive extends Thread { public void run() { try { DatagramSocket socket = new DatagramSocket(6666); //Creating a socket is equivalent to creating a wharf. DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //Creating packet s is equivalent to creating containers while(true) { socket.receive(packet); //Receiving cargo byte[] arr = packet.getData(); int len = packet.getLength(); String ip = packet.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(arr,0,len)); } } catch (IOException e) { e.printStackTrace(); } } } class Send extends Thread { public void run() { try { DatagramSocket socket = new DatagramSocket(); //Creating a socket is equivalent to creating a wharf. Scanner sc = new Scanner(System.in); while(true) { String str = sc.nextLine(); if("quit".equals(str)) break; DatagramPacket packet = //Creating packet s is equivalent to creating containers new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666); socket.send(packet); //Deliver goods } socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
26.09 Network Programming (UDP Chat Graphical Interface)
26.10 Network Programming (UDP Chat Sending Function)
26.11 Network Programming (UDP Chat Recording Function)
26.12_Network Programming (UDP Chat Screen Clearing Function)
26.13 Network Programming (UDP Chat Vibration Function)
26.14 Network Programming (UDP Chat Shortcuts and Code Optimization)
26.15 Network Programming (UDP Chat Generates jar Files)
26.16 Network Programming (TCP Protocol) (Mastery)
- 1. client
- Create Socket Connection Server (specify ip address, port number) to find the corresponding server through ip address
- Call the getInputStream() and getOutputStream() methods of Socket to obtain IO flows connected to the server
- The input stream can read the data written by the output stream of the server.
- The output stream can write the input stream of data to the server.
- 2. server side
- Create ServerSocket (need to specify port number)
- Call the accept() method of ServerSocket to receive a client request and get a Socket
- Call the getInputStream() and getOutputStream() methods of Socket to get the IO stream connected to the client
- Input stream can read data written by client output stream
- Output streams can write data to the client's input stream
26.17_ Network Programming (TCP Protocol Code Optimization)
-
Client
Socket socket = new Socket("127.0.0.1", 9999); //Create Socket to specify ip address and port number InputStream is = socket.getInputStream(); //Get the input stream OutputStream os = socket.getOutputStream(); //Get the output stream BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintStream ps = new PrintStream(os); System.out.println(br.readLine()); ps.println("I want to enroll in an employment class."); System.out.println(br.readLine()); ps.println("Grandpa didn't learn."); socket.close();
-
Server side
ServerSocket server = new ServerSocket(9999); //Create a server Socket socket = server.accept(); //Accept client requests InputStream is = socket.getInputStream(); //Get the input stream OutputStream os = socket.getOutputStream(); //Get the output stream BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintStream ps = new PrintStream(os); ps.println("Welcome to the consultation Podcast"); System.out.println(br.readLine()); ps.println("The newspaper is full.,Please report the next issue."); System.out.println(br.readLine()); server.close(); socket.close();
26.18 Network programming (server is multi-threaded) (master)
ServerSocket server = new ServerSocket(9999); //Create a server while(true) { final Socket socket = server.accept(); //Accept client requests new Thread() { public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println("Welcome to the consultation Podcast"); System.out.println(br.readLine()); ps.println("The newspaper is full.,Please report the next issue."); System.out.println(br.readLine()); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
26.19 Network Programming (Exercise)
- The client writes the string to the server (keyboard input), the server (multi-threaded) writes the string back after inversion, and the client reads the inverted string again.
26.20 Network Programming (Exercise)
- Client uploads files to server