Network study notes (TCP/I communication protocol, TCP communication, etc.)

network

network communication protocol

To enable the network of computers to communicate with each other, it is necessary to formulate a set of standards for data transmission rate, transmission code, code structure, transmission control steps, error control, etc. This set of common communication standards is the network communication protocol. Different computers must use the same communication protocol to communicate.

seven layer model

The seven-layer model, also known as the OSI (Open System Interconnection) reference model, is a standard system developed by the International Organization for Standardization (ISO) for the interconnection between computers or communication systems. It is a seven-layer, abstract model body, including not only a series of abstract terms or concepts, but also specific protocols.
ISO stands for Internationalization Standard Organization (International Organization for Standardization).

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-cYtCji5C-1661734006742) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823085931473.png)]

TCP/IP protocol

The TCP/IP protocol is the most widely used communication protocol in the Internet. TCP/IP is the abbreviation of English Transmission Control Protocol/Internet Protocol, which means "Transmission Control Protocol/Internet Protocol"

**Network:** Communication between two or more computers

Wide Area Network (WAN) is what we usually call the Internet, which is a network throughout the world.

Local area network ((LAN) intranet) , as opposed to wide area network (WAN), mainly refers to the computer interconnection network in a small area. This "small scope" can be a family, a school, a company, or a government department.

The TCP/IP protocol defines the standard for how electronic devices (such as computers) are connected to the Internet and how data is transmitted between them

A unique address of an IP computer, which is convenient for communication between computers. It consists of four bytes, and each byte is separated by ".": The range of each bit: 0~255

IP addresses are divided into five classes: Class A is reserved for government agencies, Class B is assigned to medium-sized companies, Class C is assigned to anyone who needs it, Class D is used for multicasting, and Class E is used for experiments. Each class can accommodate different address data.

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-mCNygiEv-1661734006743) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823095748320.png)]

Domain name Use domain name (English symbol) to replace ip, each domain name is mapped to an IP address, for example, Baidu domain name www.baidu.com is bound to an IP address 14.215.177.39, to visit Baidu: visit according to ip

The IP address can be used to determine which computer to communicate with, the bottom layer: convert the domain name to an IP address

Port Port number, 0~65535, the identification number of the application software with network function, which can specify which program to communicate with this computer. 0-1023 is a recognized port number, that is, it has been recognized or reserved for software that will be recognized and defined; 1024-65535 is a port number that is not publicly defined, and users can define the functions of these ports by themselves.

http: default port: 80

https: default port: 443

Default port for mysql: 3306

Port: TCP: 0~65535 UDP: 0~65535

TCP is a connection-oriented, reliable, byte stream-based transport layer protocol; when TCP communicates, before sending data, establish a connection with the other party, (TCP protocol requires three sessions (handshake), three-way handshake mechanism)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-UuqKDiGP-1661734006744) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823093236623.png)]

TCP communication

Socket programming:

Socket (client's socket)

ServerSocket (server socket (Socket))

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-qYIfgtCT-1661734006746) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823103822975.png)]

When the connection is established, the server creates a Socket object. The client and server can now communicate by writing and reading (byte stream) the Socket object.

Stream sockets: TCP, connection-oriented

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-baAQQ8Oo-1661734006746) (D:\Feisi Training 3\Blog\My\15.Network\assets\image-20220823105048585.png)]

core class

ServerSocket: server socket, waiting for client connection

Constructor (bind to specified port)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-exxuFu0w-1661734006747) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823105301424.png)]

common method

Listening for and receiving connections from this ServerSocket, this method blocks until a connection is passed in

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-xsjn10ld-1661734006748) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823105432115.png)]

Listen to the port and wait for the connection from the client. Once a client connects, get a one-to-one Socket with the client

Socket class

There are 9 types of Socket structures. Here are 2 commonly used construction methods:

Construction method illustrate
Socket(String host, int port) Initiate a connection request to the port port of the host host
[Socket](mk:@MSITStore:C:\DOCUME 1\ADMINI 1\LOCALS~1\Temp\Rar DI a 0.501 % 5 b J ava 参考下载 DIa0.501\%5bJava参考下载%5d.JDK_API_1_6_zh_CN.CHM::/java/net/Socket.html#Socket(java.lang.String, int, java.netAddress, int)) MSITStore:C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RarD I a 0.501%5 b J a v a reference document DIa0.501%5bJava reference document%5d.JDK_API_1_6_zh_CN.CHM::/java/lang/String.html) host, int port, InetAddress localAddr, int localPort) Initiate a connection request to the port port of the host host, the computer that initiates the request is localAddr, and the port is localPort

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-glLPeIhM-1661734006749) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823105552852.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-9pNoMVQy-1661734006750) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823110444218.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-YxEWLvqD-1661734006751) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823110559349.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-U8Mz4jxd-1661734006752) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823110626378.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-CqcNULqa-1661734006752) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823110637452.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-ZybEEgov-1661734006753) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823110637452.png)]

TCP communication steps

Service-Terminal

1:) Create a ServerSocket

2:) Call accept() and wait until the client connects

3:) Get the Socket object of the client, call getInputStream() getOutputStream() to get the input and output stream

4:) The input stream receives data, and the output stream sends data

5:) close resource

client

1:) Create a Socket, request the server address, port, and connect to the server

2:) Call getInputStream() getOutputStream() to get the input and output stream

3:) The output stream sends data, the input stream receives data,

4:) close resource

Start sequence: start the server first, then start the client
server programming
public class Server {
    
    

	public static void main(String[] args) {
    
    
		ServerSocket server = null;
		Socket socket = null;
		try {
    
    
			//准备服务器端用的通讯对象(套接字),指明端口号为8888
			server = new ServerSocket(8888);
			//到指定端口去阻塞监听,一旦有客户端请求发送过来,那么立即自动与客户端建立连接
			socket = server.accept();
			System.out.println("服务器端准备ok");
            
			//我要发送到客户端的内容
			String msg = "你好,我是服务器,这是我的第一次通讯,请问你收到了吗";
			OutputStream os = socket.getOutputStream();//准备输出对象
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(msg);
			bw.newLine();
			bw.flush();
            
			//接收客户端发送的信息
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String reply = br.readLine();
			System.out.println("我是服务器,接收到信息:"+reply);
			
			br.close();
			is.close();
			bw.close();
			os.close();
			socket.close();
			server.close();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
	}
}
client programming
public class Client {
    
    

	public static void main(String[] args) {
    
    
		Socket socket = null;
		try {
    
    
			socket = new Socket("localhost", 8888);
			System.out.println("客户端准备完成");
			
			//客户端开始接受请求
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String msg = br.readLine();
			System.out.println("我是客户端,接收到信息:"+msg);
			
			//客户端接收到服务器端的信息之后,需要反馈信息给服务器
       	 //客户端想要反馈给服务器端的信息 
			String reply = "我是客户端,收到你的信息,这是我的反馈";
			OutputStream os = socket.getOutputStream();//准备客户端的输出流
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(reply);
			bw.newLine();
			
			bw.flush();
			bw.close();
			os.close();
			br.close();
			is.close();
			socket.close();
		} catch (UnknownHostException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
	}
}

idea set a program to run multiple times

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-XOJ58gpG-1661734006754) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823114549421.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-NO1RPl2d-1661734006755) (D:\Feisi Training 3\Blog\My\15.Network\assets\image-20220823114719745.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Nc0x4dS6-1661734006755) (D:\Feisi Training Three\Blog\My\15.Network\assets\image-20220823114752213.png)]

UDP communication

ception e) {
e.printStackTrace();
}
}
}




#### idea设置一个程序运行多次

[外链图片转存中...(img-XOJ58gpG-1661734006754)]

[外链图片转存中...(img-NO1RPl2d-1661734006755)]

[外链图片转存中...(img-Nc0x4dS6-1661734006755)]



## UDP通信

Guess you like

Origin blog.csdn.net/m0_48895748/article/details/126577685