Learning Catalogue:
Network programming
computer network
It refers to connecting multiple computers with independent functions in different geographical locations and their external equipment through communication lines.
Under the management and coordination of network operating system, network management software and network communication protocol,
A computer system for resource sharing and information transmission.
Network programming
It means that data can be exchanged between programs running on different computers to realize network interconnection.
Network Programming (Network Model Overview and Diagram) (Understanding)
An overview of the network model:
What kind of rules a computer communicates with, and what kind of network model it is
Common network models:
- OSI Seven-Layer Model
Physical Layer: Interface, Bit Stream Transmission
Data Link Layer: MAC Address Unencapsulation, Zheng Transport, Switch
Network Layer: IP Address Unencapsulation, Packet Transfer, Router De-encapsulation
Transport Layer: Protocol TCP/UDP
Session Layer: Connection of Data Links
Presentation Layer: Explain Encrypted Connections
Application Layer: Terminal Applications - TCP/TP four-tier model host to network layer (physical layer, data link layer), network layer, transport layer, application layer (application layer, presentation layer, session layer)
Network Programming (Overview of Three Elements of Network Programming) (Understanding)
IP: Used to identify device uniqueness
Port: Logical address used to store processes, identification of different processes
Protocol: UDP TCP
Network Programming (IP Overview of Three Elements of Network Programming) (Understanding)
- Overview: 32-bit addresses assigned to the host
- Composition: Network Address + Host Address
- Class A IP: Network + Machine + Machine + Machine 1.0.0.1-127.255.255.254
(1)10.X.X.X is a private address (a private address is an address that is not used on the Internet but used in a local area network)
(2)127.X.X.X is reserved address for cyclic testing. - Class B IP: Network+Network+Machine+Machine 92.0.0.1-223.255.255.254
2.168.x.X is a private address - Class C IP: Network+Network+Network+Machine 192.0.0.1-223.255.255.254
192.168.x.X is a private address - Category D: 224.0.0.1-239.255.255.254
- Class E 240.0.0.1-247.255.255.254
- Special address
127.0.0.1 loopback address, used to detect whether the local network problem Ping 127.0.0.1
ipconfig View Local IP
xxx.xxx.xxx.255 broadcast address
hostname gets the host name
Network Programming (Overview and Use of InetAddress Class) (Understanding)
- Overview of the InteAddress class:
In order to facilitate the acquisition and operation of IP address - The method provided by InetAddress
public static InetAddress getByName(String host)// Fill in host name or IP in parentheses
public String getHostAddress()// Get IP
public String getHostName()// Get the host name
getLocalHost();
Case 1:
Overview of InetAddress functionality
getByName Get: Know the host name or IP
GetLocal Host to get the IP of Chlamydia moth without knowing its host name
package day20190801.IP in InterAddress An overview of functions; import java.net.InetAddress; import java.net.UnknownHostException; /** * @description:InterAddress Overview of functions * @author: @Li Xiaobai * @create: 2019-08-01 15:28 */ public class Demo { public static void main(String[] args) throws UnknownHostException { //Get it with getByName knowing the host name or IP InetAddress inetAddress = InetAddress.getByName("192.168.11.233"); String hostAddress = inetAddress.getHostAddress();//ip String hostName = inetAddress.getHostName();//Get the host name System.out.println(hostAddress); System.out.println(hostName); //Get it with getLocalHost without knowing the host name or IP InetAddress localHost = InetAddress.getLocalHost(); String hostAddress1 = localHost.getHostAddress();//Get IP String hostName1 = localHost.getHostName();//Get the host name System.out.println(hostAddress1); System.out.println(hostName1); } }
Network Programming (Ports and Protocols of Three Elements of Network Programming) (Understanding)
Port:
- Physical Port: Network Card Port
- Logical Port:
Each network program will have a logical port
Used to identify different processes
Effective Port: 0 - - - 65535, of which 0 - - - 1023 is the port used by the system.
Agreement
- UDP: No connection is required, unreliable, efficient, and the size of each datagram is limited to 64KB
- TCP: Connections must be established, reliable, inefficient, and large data transmission must be carried out.
Network Programming (Socket Communication Principle Diagram) (Understanding)
- Socket=IP + Port
- Principle and mechanism of Socket socket:
Both ends of the communication have Socket s
Network communication is the communication between Socket s.
Data transmission between sockets is through IO
Network Programming (UDP Protocol to Send Data) (Master)
- Claint side: Send protocol
Step 1: New Data GramaSockert Creates UDP Communication Client Objects
Step 2: send sends a byte word created to create a new DataGramaPackage (bytes, byte length, host name, port number)
Step 3: Release resources close
Network Programming (UDP Protocol Receiving Data) (Master)
- Server Receiving Protocol
Step 1: New Data GramaSockert (Exposed Port Number) creates a datagram package to receive data
Step 2: resive receives and creates an empty datagram new DataGramaPackage (byte, byte length) to fetch data + length + ip and convert bytes [] into strings
Step 3: Release resources close
Case 1:
Data transmission between general UDP protocols
package day20190801.UDP Simple transmission between; import java.io.IOException; import java.net.*; /** * @description: claint End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPClaint { public static void main(String[] args) throws IOException { //UDP //The data source and destination are encapsulated in a data package without establishing a connection. //The size of each datagram is limited to 64k. //Because there is no connection, it is not a reliable protocol. //No need to establish connection, fast speed //Java provides a Socket for UDP protocol //DatagramSocket represents a socket used to send and receive data packets. //DatagramSocket() //Construct a datagram socket and bind it to any available port on the local host. //1. Create client Socket DatagramSocket datagramSocket = new DatagramSocket(); //2. Data transmission //Create a Number Packet byte[] bytes = "Hello, udp I sent you a package. Did you receive it?".getBytes(); InetAddress byName = InetAddress.getByName("192.168.11.239"); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length,byName,9999); datagramSocket.send(datagramPacket); //Bytes, byte length, host name, port number //3. Releasing resources datagramSocket.close(); } }
package day20190801.UDP Simple transmission between; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * @description: server End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPServer { public static void main(String[] args) throws IOException { //DatagramSocket( int port) //Create a datagram socket and bind it to a specified port on the local host. //1. Create a data socket and expose the port number DatagramSocket datagramSocket = new DatagramSocket(9999); //2. Receiving data //Create an empty package to store the received resources byte[] bytes = new byte[1024 * 8]; DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);//Array + Array Length System.out.println("Server has been started, waiting for connection........."); datagramSocket.receive(datagramPacket); //Put the resources out of the collected resource package byte[] data = datagramPacket.getData();//data int length = datagramPacket.getLength();//Actual length String ip = datagramPacket.getAddress().getHostAddress();//ip String s = new String(data, 0, length); System.out.println(ip+":Send a message----"+s); //3. Releasing resources datagramSocket.close(); } }
Network Programming (UDP Protocol Sender Data from Keyboard Entry) (Understanding)
Case 3:
Keyboard input UDP send data way 1:
package day20190801.UDP Two Ways of Keyboard Entry; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @description: claint End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPClaint { public static void main(String[] args) throws IOException { //1. Create client Socket DatagramSocket datagramSocket = new DatagramSocket(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter the information you want to enter."); String s = in.readLine(); if (s.equals("886")) { break; } //2. Data transmission //Create a Number Packet byte[] bytes = s.getBytes(); InetAddress byName = InetAddress.getByName("192.168.11.239"); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, byName, 9991);//Bytes, byte length, host name, port number datagramSocket.send(datagramPacket); } //3. Releasing resources datagramSocket.close(); } }
package day20190801.UDP Two Ways of Keyboard Entry; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * @description: server End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPServer { public static void main(String[] args) throws IOException { //DatagramSocket( int port) //Create a datagram socket and bind it to a specified port on the local host. //1. Create a data socket and expose the port number DatagramSocket datagramSocket = new DatagramSocket(9991); System.out.println("Server has been started, waiting for connection........."); while (true) { //2. Receiving data //Create an empty package to store the received resources byte[] bytes = new byte[1024 * 8]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);//Array + Array Length datagramSocket.receive(datagramPacket); //Put the resources out of the collected resource package byte[] data = datagramPacket.getData();//data int length = datagramPacket.getLength();//Actual length String ip = datagramPacket.getAddress().getHostAddress();//ip String s = new String(data, 0, length); System.out.println(ip + ":Send a message----" + s); } //3. Releasing resources // datagramSocket.close(); } }
Keyboard Input: Mode 2
package day20190801.UDP Two Ways of Keyboard Entry 2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; /** * @description: claint End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPClaint { public static void main(String[] args) throws IOException { //1. Create client Socket DatagramSocket datagramSocket = new DatagramSocket(); Scanner sc = new Scanner(System.in); System.out.println("Please enter the message you want to send."); while (sc.hasNextLine()) { System.out.println("Please enter the information you want to enter."); String s = sc.nextLine(); if (s.equals("886")) { break; } //2. Data transmission //Create a Number Packet byte[] bytes = s.getBytes(); InetAddress byName = InetAddress.getByName("192.168.11.239"); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, byName, 9992);//Bytes, byte length, host name, port number datagramSocket.send(datagramPacket); } //3. Releasing resources datagramSocket.close(); } }
package day20190801.UDP Two Ways of Keyboard Entry 2; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * @description: server End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPServer { public static void main(String[] args) throws IOException { //DatagramSocket( int port) //Create a datagram socket and bind it to a specified port on the local host. //1. Create a data socket and expose the port number DatagramSocket datagramSocket = new DatagramSocket(9992); System.out.println("Server has been started, waiting for connection........."); while (true) { //2. Receiving data //Create an empty package to store the received resources byte[] bytes = new byte[1024 * 8]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);//Array + Array Length datagramSocket.receive(datagramPacket); //Put the resources out of the collected resource package byte[] data = datagramPacket.getData();//data int length = datagramPacket.getLength();//Actual length String ip = datagramPacket.getAddress().getHostAddress();//ip String s = new String(data, 0, length); System.out.println(ip + ":Send a message----" + s); } //3. Releasing resources // datagramSocket.close(); } }
Network Programming (Multithread Chat Room Programming) (Understanding)
Case 3: UDP opens two threads
package day20190801.UDP Keyboard Input Open Thread; /** * @description: * @author: @Li Xiaobai * @create: 2019-08-01 17:22 */ public class Text { public static void main(String[] args) { new UDPClaint().start(); new UDPServer().start(); } }
package day20190801.UDP Keyboard Input Open Thread; import sun.awt.windows.ThemeReader; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; /** * @description: claint End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPClaint extends Thread { @Override public void run() { DatagramSocket datagramSocket = null; try { //1. Create client Socket datagramSocket = new DatagramSocket(); Scanner sc = new Scanner(System.in); System.out.println("Please enter the message you want to send."); while (sc.hasNextLine()) { System.out.println("Please enter the information you want to enter."); String s = sc.nextLine(); if (s.equals("886")) { break; } //2. Data transmission //Create a Number Packet byte[] bytes = s.getBytes(); InetAddress byName = InetAddress.getByName("192.168.11.239"); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, byName, 9992);//Bytes, byte length, host name, port number datagramSocket.send(datagramPacket); } } catch (IOException e) { e.printStackTrace(); } //3. Releasing resources datagramSocket.close(); } }
package day20190801.UDP Keyboard Input Open Thread; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * @description: server End-to-end transmission * @author: @Li Xiaobai * @create: 2019-08-01 16:03 */ public class UDPServer extends Thread{ @Override public void run() { try { //DatagramSocket( int port) //Create a datagram socket and bind it to a specified port on the local host. //1. Create a data socket and expose the port number DatagramSocket datagramSocket = new DatagramSocket(9992); System.out.println("Server has been started, waiting for connection........."); while (true) { //2. Receiving data //Create an empty package to store the received resources byte[] bytes = new byte[1024 * 8]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);//Array + Array Length datagramSocket.receive(datagramPacket); //Put the resources out of the collected resource package byte[] data = datagramPacket.getData();//data int length = datagramPacket.getLength();//Actual length String ip = datagramPacket.getAddress().getHostAddress();//ip String s = new String(data, 0, length); System.out.println(ip + ":Send a message----" + s); } } catch (IOException e) { e.printStackTrace(); } //3. Releasing resources // datagramSocket.close(); } }
Case 4:
Two Machines Keyboard Entry Chat
public class A { public static void main(String[] args) { new Thread() { @Override public void run() { //Open the socket for the service try { DatagramSocket socket = new DatagramSocket(9999);//Exposure port number System.out.println("Server has been started, waiting for connection..."); while (true) { //Create an empty datagram package DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024); socket.receive(datagramPacket);//Blocking method, waiting to receive the number of packets sent by the client //Get the data from the datagram byte[] data = datagramPacket.getData(); int length = datagramPacket.getLength(); //Remove sender ip String ip = datagramPacket.getAddress().getHostAddress(); System.out.println(ip + "B Send it to you:" + new String(data, 0, length)); } } catch (Exception e) { e.printStackTrace(); } } }.start(); //Send a message to B sendMsg(); } private static void sendMsg() { try { DatagramSocket socket = new DatagramSocket(); //Create datagram packages //The data to be put in the sending packet: the IP and port of the other party. And the data you want to send Scanner scanner = new Scanner(System.in); System.out.println("Please enter what you want to send to A Data"); while (scanner.hasNextLine()) { System.out.println("Please enter what you want to send to A Data"); String s = scanner.nextLine(); if (s.equals("886")) { break; } byte[] bytes = s.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.11.123"), 8888); socket.send(datagramPacket); } socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
public class B { public static void main(String[] args) { new Thread() { @Override public void run() { //Open the socket for the service try { DatagramSocket socket = new DatagramSocket(8888);//Exposure port number System.out.println("Server has been started, waiting for connection..."); while (true) { //Create an empty datagram package DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024); socket.receive(datagramPacket);//Blocking method, waiting to receive the number of packets sent by the client //Get the data from the datagram byte[] data = datagramPacket.getData(); int length = datagramPacket.getLength(); //Remove sender ip String ip = datagramPacket.getAddress().getHostAddress(); System.out.println(ip + "A Send it to you:" + new String(data, 0, length)); } } catch (Exception e) { e.printStackTrace(); } } }.start(); //Send a message to B sendMsg(); } private static void sendMsg() { try { DatagramSocket socket = new DatagramSocket(); //Create datagram packages //The data to be put in the sending packet: the IP and port of the other party. And the data you want to send Scanner scanner = new Scanner(System.in); System.out.println("Please enter what you want to send A Data"); while (scanner.hasNextLine()) { System.out.println("Please enter what you want to send A Data"); String s = scanner.nextLine(); if (s.equals("886")) { break; } byte[] bytes = s.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.11.123"), 9999); socket.send(datagramPacket); } socket.close(); } catch (IOException e) { e.printStackTrace(); } } }