Introduction notes to network programming
Basic science popularization
summary
give an example
Call - TCP
SMS - UDP
Purpose of network programming
Dissemination and exchange of information, data exchange and communication
Elements of network communication
- Addresses of both sides of communication, including IP address and port number
- Network communication protocol
tcp/ip reference model
OSI seven layer network model is an idealized reference model, which has no practical application. It is actually a TCP/IP reference model.
IP address
IP address: InetAddress
- IP is used to uniquely locate a computer on a network
- Native: 127.0.0.1 or localhost
- Classification of IP addresses
- IPv4: 32 bits, composed of 4 bytes, each byte is an unsigned integer ranging from 0 to 255, with a total of 4.2 billion, that is, the 32nd power of 2. It is unevenly distributed, with 3 billion in North America and 400 million in Asia. It was exhausted in 2011.
- IPv6: 128 bits, 8 unsigned integers, 16 bits for each integer.
- Example: 2001:DB8:0:23:8:800:200C:417A
- Public (Internet) / private IP (local area network)
- ABCD class address
- 192.168. 10. X addresses are specially used within the organization, intranet addresses
- Domain name: used to memorize IP addresses and the mapping between multiple IP addresses and domain names during clustering
extend
IPv6 notation
The address length of IPv6 is 128 bits, which is 4 times that of IPv4. Therefore, the IPv4 dotted decimal format is no longer applicable and is expressed in hexadecimal. IPv6 has three representations.
1, Decimal notation
The format is X:X:X:X:X:X:X:X: X, where each X represents 16b in the address, expressed in hexadecimal, for example:
ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
In this representation, the leading 0 of each X can be omitted, for example:
2001:0DB8:0000:0023:0008:0800:200C:417A→ 2001:DB8:0:23:8:800:200C:417A
2, 0-bit compressed representation
In some cases, an IPv6 address may contain a long segment of 0 in the middle, and a continuous segment of 0 can be compressed into ":". However, in order to ensure the uniqueness of address resolution, the address can only appear once, for example:
FF01:0:0:0:0:0:0:1101 → FF01::1101
0:0:0:0:0:0:0:1 → ::1
0:0:0:0:0:0:0:0 → ::
3, Embedded IPv4 address representation
In order to realize IPv4-IPv6 interworking, the IPv4 address will be embedded in the IPv6 address. At this time, the address is often expressed as X: X: X: X: X: X: X: d.d.d. the first 96b is expressed in bold hexadecimal, while the last 32b address is expressed in dotted decimal of IPv4. For example:: 192.168.0.1 and:: FFFF:192.168.0.1 are two typical examples. Note that in the first 96b, the method of compressing 0 bits is still applicable
The difference between Class A, class B and class C IP addresses
IP addresses are represented differently:
A class a IP address means that among the four segment numbers of the IP address, the first segment number is the network number, and the remaining three segment numbers are the number of the local computer. If the IP address is represented in binary, the class a IP address consists of a 1-byte network address and a 3-byte host address, and the highest bit of the network address must be "0". The length of network identification in class a IP address is 8 bits, and the length of host identification is 24 bits.
A class B IP address refers to the network number in the first two segments of the four segments of the IP address. If the IP address is represented in binary, the class B IP address consists of a 2-byte network address and a 2-byte host address, and the highest bit of the network address must be "10". In class B IP address, the length of network identification is 16 bits, and the length of host identification is 16 bits.
A class C IP address refers to the four segment numbers of the IP address. The first three segment numbers are the network numbers, and the remaining segment numbers are the numbers of the local computer. If the IP address is represented in binary, the class C IP address consists of a 3-byte network address and a 1-byte host address. The highest bit of the network address must be "110". The length of network identification in class C IP address is 24 bits, and the length of host identification is 8 bits.
2. Different IP address ranges:
Class A IP addresses range from 1.0.0.1 to 127.255.255.254 (binary: 00000001 00000000 00000000 00000001 - 01111111 11111111 11110). The last one is the broadcast address.
Class B IP addresses range from 128.0.0.1-191.255.255.254 (binary representation: 10000000 00000000 00000000 00000001-10111111 11111111111 11111110). The last one is the broadcast address.
Class C IP addresses range from 192.0.0.1-223.255.255.254 (binary representation: 11000000 00000000 00000000 00000001 - 11011111 11111111111 11111110). The last one is the broadcast address.
3. Different subnet masks:
The subnet mask of class A IP address is 255.0.0.0
The subnet mask of class B IP address is 255.255.0.0
The subnet mask of class C IP address is 255.255.255.0
4. Different scope of application:
Class A is applicable to large networks. There are 126 networks with a small number of class a network addresses. The maximum number of hosts supported by each network is 256 to the power of 3 -2=16777214;
Class B applies to medium-sized networks. The number of class B network addresses is moderate. There are 16384 networks. The maximum number of hosts supported by each network is the power of 256 - 2 = 65534;
Class C is applicable to small networks. There are a large number of class C network addresses, with more than 2.09 million networks. It is suitable for small-scale local area networks. The maximum number of hosts supported by each network is 256 to the power of 1 - 2 = 254.
ABCD address memory
A: 0-127 middle 128 bits
B: 128-191 middle 64 bit
C: 192-223 middle 32 bits
D: 224-239 middle 16 bits
E: 240-255 middle 16 bits
If you remember, start from 0, add 128, and then add one-half of it. If you go on like this, you can calculate it.
Port port
If we compare the IP address to a building, the Port is the name of each house. The program on our host is equivalent to a house, and each program must be assigned a Port number when running.
- Different processes have different port numbers to distinguish software.
- Specified range: 0 ~ 65535
- Different protocols TCP\UDP have a corresponding port number range of 0 ~ 65535, so there are 65535 * 2 total available port numbers. The port numbers of different protocols can be the same. For example, under the same protocol, TCP can only have one 80 port used by one process without conflict.
- Port classification
- Public port 0 ~ 1023
- HTTP: 80 (default port)
- HTTPS: 443
- FTP: 21
- SSH: 22
- Telent : 23
- Program registration port range: 1024 ~ 49151, the port assigned to the user or program
- Tomcat: 8080
- MySQL: 3306
- Oracle: 1521
- Private and dynamic port range: 49152 ~ 65535
- Public port 0 ~ 1023
Simple command
#View a list of all ports netstat -ano #View the list of ports corresponding to the specified port or string| It means pipeline filtering. It will filter first and then find netstat -ano|findstr "8080" #View the process of the specified port or program tasklist|findstr "chrome" tasklist|findstr "1736"
Code example
public class INetAddressTest { public static void main(String[] args) throws UnknownHostException { //Three ways to query local address ///127.0.0.1 System.out.println(InetAddress.getByName("127.0.0.1")); //DESKTOP-G590BHK/172.16.57.150 computer name plus host System.out.println(InetAddress.getLocalHost()); InetAddress localhost = InetAddress.getByName("localhost"); // System.out.println(localhost.getAddress());// Return binary array address, byte [] //localhost/127.0.0.1 System.out.println(localhost); //activate.navicat.com returns the standard host name, because 127.0.0.1 activate. Com is configured for hosts on the local computer navicat. com // System.out.println(localhost.getCanonicalHostName()); //localhost System.out.println(localhost.getHostName()); System.out.println(localhost.getHostAddress()); System.out.println("-----------------------------------"); InetAddress qq = InetAddress.getByName("www.qq.com"); System.out.println(qq); System.out.println(qq.getCanonicalHostName()); System.out.println(qq.getHostName()); System.out.println(qq.getHostAddress()); System.out.println("----------------port-------------------"); //IP port object InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); } /** * These two classes have little effect, just understand * /127.0.0.1 * DESKTOP-G590BHK/172.16.57.150 * localhost/127.0.0.1 * localhost * 127.0.0.1 * ----------------------------------- * www.qq.com/121.14.77.201 * www.qq.com * www.qq.com * 121.14.77.201 * ----------------port------------------- * localhost/127.0.0.1 * localhost * 8080 */ }
communication protocol
Agreement: it's an agreement, just as we agreed to use Mandarin.
Because of the complexity of the communication protocol, we make it small and deal with it hierarchically.
TCP/IP protocol cluster: it is actually a group of protocols
TCP: Transmission Control Protocol
UDP: User Datagram Protocol
TCP/IP protocol is commonly used and well-known.
Comparison between TCP and UDP
TCP
- phone
- Establish connection and stabilize
- Three handshakes and four waves
- There are concepts of client and server
- The transmission is completed, the connection is released, and the efficiency is low
What would the interviewer like to hear about three handshakes and four waves
UDP
- send message
- No connection established, unstable
- There is no clear boundary between client and server
- You can send messages whether you are ready or not
- DDOS saturation brute force attack is the form of UDP protocol
TCP communication steps
client
- Connect to the server Socket
- send message
Server
- Establish service port ServerSocket
- Wait for the user to connect to ServerSocket accept()
- Accept user messages
Code example
Communication under TCP protocol
public class TCPClient { public static void main(String[] args) throws IOException { //1. The client knows the address and port number of the server and establishes a connection. Socket is a client socket try (Socket socket = new Socket("localhost", 9999)) { try (OutputStream outputStream = socket.getOutputStream()) { /* Scanner scanner = new Scanner(System.in); System.out.println("!"); //The server can't read the data, so it can only read the last part. As long as the scanner keeps on, that is, the client doesn't end, the read flow of the server will be blocked all the time while (scanner.hasNext()) { if ("exit".equalsIgnoreCase(scanner.next())) { scanner.close(); System.out.println("The client exits the chat room! "); break; } //Send message IO stream outputStream.write(scanner.next().getBytes());*/ outputStream.write("The client can send information to intelligent Xiaobai".getBytes()); // } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }
public class TCPServer { public static void main(String[] args) throws IOException { //The server creates a server socket ServerSocket through ip address and port number try (ServerSocket serverSocket = new ServerSocket(9999)) { //Waiting for client connection try (Socket socket = serverSocket.accept()) { //Get the read byte stream of the client and read the message of the client try (InputStream is = socket.getInputStream()) { //Pipe flow try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } System.out.println(outputStream.toString()); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } /** * The message can be sent to the smart client */ }
Upload files under TCP protocol
public class TCPUploadClient { public static void main(String[] args) throws Exception { //1. Create Socket connection Socket socket = new Socket("127.0.0.1", 8090); //2. Create output stream OutputStream os = socket.getOutputStream(); //If the pathname of file is not written, the default is the relative path, that is, the location under the root directory of the project, under springboot demo //read file FileInputStream is = new FileInputStream(new File("asfasf.jpg")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } //Notify the server that I have finished sending messages and finished the output stream, so that the server will not block the waiting socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } System.out.println(outputStream.toString()); //First on and then off inputStream.close(); outputStream.close(); is.close(); os.close(); socket.close(); } //File transfer is complete! bye! }
public class TCPUploadServer { public static void main(String[] args) throws Exception { //Create service ServerSocket ServerSocket serverSocket = new ServerSocket(8090); //Listening client Socket socket = serverSocket.accept(); //Get input stream InputStream is = socket.getInputStream(); //Create output file name and output stream FileOutputStream os = new FileOutputStream(new File("hello.jpg")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } //Notify the client that the file transfer is completed, that is, write a message to the client //Get the output stream first OutputStream outputStream = socket.getOutputStream(); outputStream.write("File transfer is complete! bye!".getBytes()); //close resource outputStream.close(); os.close(); is.close(); socket.close(); serverSocket.close(); } }
UDP
UDP Client can directly send messages to the server or other people without establishing a connection. Therefore, it is not necessary to require the server or other people to be online to succeed; When a TCP message occurs, a connection needs to be established first. At this time, if the connected server is unavailable (for example, it is not started), an error will be reported when the TCP Client starts. As follows:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
However, if the UDP receiver, such as the server, is unavailable, it will not receive data. Therefore, theoretically, although there will be no error, the UDP server should be started first and wait for the data to be received.
Code example
Communication chat under UDP protocol - contract letting for chat
It is preferred to create two UDP tool classes: send message and receive message, and then create two user objects to create two threads to listen for execution.
Accept API classes
public class UDPChatReceiver implements Runnable { private DatagramSocket datagramSocket; private int fromPort; private String userName; public UDPChatReceiver(int fromPort, String userName) { this.fromPort = fromPort; this.userName = userName; try { //1 create a datagram socket to open the receiving port datagramSocket = new DatagramSocket(fromPort); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { try { while (true) { //Prepare container to receive data byte[] buffer = new byte[1024]; //2 create and receive data packets and transfer them into the cache container DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); //3 block receiving data datagramSocket.receive(packet); System.out.println(packet.getAddress()); //4 after reading the data, transfer the bytes to the string String data = new String(packet.getData()); System.out.println(userName + ":" + data); if ("exit".equalsIgnoreCase(data)) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { //5 close flow datagramSocket.close(); } } }
Send API class
public class UDPChatSender implements Runnable { //Target IP private String toIP; //Target port private int toPort; //Own port private int fromPort; private DatagramSocket datagramSocket; private BufferedReader reader; public UDPChatSender(String toIP, int toPort, int fromPort) { this.toIP = toIP; this.toPort = toPort; this.fromPort = fromPort; try { //1 create a datagram socket datagramSocket = new DatagramSocket(fromPort); reader = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { try { while (true) { //Read console data String str = reader.readLine(); //2 create a data packet for sending, including the data and the destination IP and port of sending DatagramPacket packet = new DatagramPacket(str.getBytes(), 0, str.getBytes().length, InetAddress.getByName(toIP), toPort); //3 send packet datagramSocket.send(packet); if ("exit".equalsIgnoreCase(str)) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { //4 close flow datagramSocket.close(); } } }
User object
public class Customer { public static void main(String[] args) { new Thread(new UDPChatReceiver(9800,"sale")).start(); new Thread(new UDPChatSender("localhost",9500,9601)).start(); } } public class Sale { public static void main(String[] args) { //Receive thread new Thread(new UDPChatReceiver(9500,"customer")).start(); //Send thread new Thread(new UDPChatSender("localhost",9800,9600)).start(); } }
Java execution class should pay attention to returning to the root path of package name before executing java package name File name to execute the file
Tomcat
The Tomcat log is garbled because the default code of Windows is GBK. Change the logging in the conf directory Properties file.
URL
Uniform resource locator: used to locate a resource on the Internet.
DNS domain name resolution refers to resolving domain names into IP addresses, such as www.kugou.com com ——> 117.27.241.66
URL format
agreement://IP address: port number / project name / resource
Code example - url resource download
The essence of web crawler is to crawl the URL address of various resource files, then create a connection, open the download stream, download to the address, or crawl to the data for analysis.
public class URLTest { public static void main(String[] args) throws Exception { URL url1 = new URL("https://www.bilibili.com/video/BV1LJ411z7vY?p=12"); //It should be noted that if the host is a domain name, the host obtained is still a domain name. If there is no port, the default is - 1 System.out.println(url1.getProtocol());//agreement System.out.println(url1.getHost());//host System.out.println(url1.getPort());//port System.out.println(url1.getPath());//file System.out.println(url1.getFile());//Full path System.out.println(url1.getQuery());//parameter /** * https * www.bilibili.com * -1 * /video/BV1LJ411z7vY * /video/BV1LJ411z7vY?p=12 * p=12 * null */ //Create a url object through which you can access, download resources, and crawl data URL url = new URL("https://webfs.yun.kugou.com/202102241417/c9463283bf6cecf8eead45be1614a275/part/0/960125/KGTX/CLTX001/a46e805441a932ed0f5ef78dd229f688.mp3"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream is = connection.getInputStream(); FileOutputStream os = new FileOutputStream(new File("hope.mp3")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { os.write(buffer); } os.close(); is.close(); connection.disconnect(); } }