Summary of network programming/data collection/interaction mode of system operation and maintenance series

This blog is intended to summarize the methods of data collection/interaction. To put it bluntly, it is the problem of interface/communication, that is, how to establish a communication connection with the peer, how to transmit data, and how to store data. Our world is an interconnected world. In essence, we are all doing information interaction in our daily life, like the beacon in ancient wars, to the telegraph/telephone of modern warfare, to the current Internet/informatization, information The interaction is very important.
The three elements of network programming :
IP address: the unique identification of each device in the network, the local loop address is 127.0.0.1, the broadcast address is 255.255.255.255, IPV4 4 0-255, IPV6 total 8 groups, each group has 4 hexadecimals system;
port number: unique identification of each program on the device, each network program needs to bind a port number, the transmission of data in addition to explicitly made clear where machines have to be sent to which program;
protocol: the computer network The rules established for data interaction, where UDP is oriented to connectionless, data is insecure but fast, and does not distinguish between server and client; TCP connection-oriented (three-way handshake) data is secure but the speed is slightly lower, distinguishing between server and client, The client sends a request to the server, and the server responds to the request and transmits data.
part1: socket interface
Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into a few simple interfaces. The application layer calls the implemented processes to communicate in the network. Only the uniquely identified IP address and port number on the network can be combined to form a uniquely identifiable socket identifier socket. Two important classes in java are socket class and ServerSocket class. The former is java.net.Socket, which is generally used for writing client code; the latter is java.net.ServerSocket, which is generally used for server code. In the preparation of Socket, ServerSocket is different from Socket. ServerSocket waits for the client's request. Once a connection request is obtained, a Socket example is created to communicate with the client. Of course, the client and server here are also a relative concept. Both the client and the server can read and write each other’s data. Generally speaking, the server is the data source, and the client fetches data from the server or the server will The data is pushed to the client. The scenario that I focus on here is how I, as a server, receive data from the client.
Step 1: Test whether the server and client routing ports are open, and open the port on the server and make sure that this port is not occupied.
telnet ip address port number to check whether the port is open or not. If the test is open under Windows, there will be a black box without any prompt message, then exit: CTRL+] and then enter quit;
lsof -i:A check whether port A is occupied;
if the port is If it is not activated, use firewall or iptables to activate it under the linux server. For details, please refer to the link information.
Supplement: The port number ranges from 0 to 65535, but ports with smaller numbers are generally occupied by default, such as:
ftp(21) SSH(22) smtp(25) web(80) oracle(1521)
mysql(3306) QQ( 4000) tomcat(8080)
Step 2: Code

public void server() {
    
    
		ServerSocket serverSocket = null;
		Socket client = null;
		OutputStream dos = null;    //输出流
		InputStream dis = null;    //输入流
		try{
    
    
		serverSocket = new ServerSocket(port);
		System.out.println("Server is starting...\n");
		client = serverSocket.accept();
		dis = client.getInputStream();
		BufferedReader inReader = new BufferedReader(new InputStreamReader(dis,"GB2312"));
		StringBuffer sBuffer = new StringBuffer();
        while(true) {
    
     
        	String line = inReader.readLine();
        	System.out.println("客户端发过来的内容:" + line); 
         }
     }
 }

part2: http interface
HTTP (HyperText Transfer Protocol) is a set of rules for computers to communicate over the network. The HTTP protocol uses a request/response model. The client sends a request message to the server. The request message contains the request method, URL, request header, and request data. The server responds with a status line. The content of the response includes the protocol version, status code, server information, response header, and response data.
Step 1: Same as the tomcat step, you need to ensure that the routing status and port are free.
Step 2: Code

public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
    
    
		String ip = req.getRequestURI();
        Map<String, String[]> map = req.getParameterMap();
        StringBuffer inputString = new StringBuffer();
        for (String key:map.keySet()) {
    
    
            inputString.append(key);
            inputString.append("=");
            inputString.append(Arrays.toString(map.get(key)));
        }
        System.out.println(inputString.toString());
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
    
    
		String ip = req.getRequestURI();
		InputStream dis = req.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(dis,"utf-8"));
        String line = "";
        StringBuffer inputString = new StringBuffer();
        while ((line = reader.readLine())!=null) {
    
    
            inputString.append(line);
        }
        JSONObject jsonObject = JSONObject.fromObject(inputString.toString());
        System.out.println(jsonObject.toString());
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
    }

part3: ftp file transfer
FTP (File Transfer Protocol) is one of the protocols in the TCP/IP protocol suite. The FTP protocol includes two components, one is the FTP server, and the other is the FTP client. The FTP server is used to store files, and users can use the FTP client to access the resources located on the FTP server through the FTP protocol.
See references for the code.

Reference link:
https://blog.csdn.net/qq_41517936/article/details/81015711 Brief introduction and examples of Socket and ServerSocket
https://blog.csdn.net/zx110503/article/details/78787483 CentOS7 open port (permanent )
Https://blog.csdn.net/qq_39946015/article/details/104374427?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevantFrom.none-Commend2blog -1.add_param_isCfhttp http request and response, client and server interface development
https://www.cnblogs.com/huzi007/p/4236150.html java realizes the upload and download of ftp files

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/113769868