A TCP socket local network communication optimization method and system
The hook function technology converts TCP socket local network communication in Linux system into UDS socket communication, solving the problems of low efficiency and insufficient security of TCP socket communication, and achieving more efficient, reliable and secure network communication.
Patent Information
- Application Number
- CN202510012613.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-06
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2045-01-06
AI Technical Summary
The lack of optimization of Linux systems in TCP socket local network communications leads to low communication rate, insufficient throughput, low latency, insufficient security and reliability, and large resource usage, especially in scenarios where external network connections and large number of short connections are frequently created and destroyed.
Through hook function technology, the socket network interface functions of the server and client are hijacked and rewrited, and TCP socket local network communication is converted into UDS socket local network communication, and the performance advantages, security and reliability of UDS socket are used to optimize communication.
Improves the rate and throughput of TCP socket local network communication, reduces latency, enhances security and reliability, and reduces resource usage, especially in high-frequency connection and disconnection scenarios.
Smart Images

Figure CN119420825B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of network communication of operating systems, and in particular to a TCP socket local network communication optimization method and system. Background Art
[0002] With the development of technology, Linux system has occupied an important position in various fields, especially in the fields of industrial control, server, finance, security and so on. Network communication provides an indispensable technology for data transmission, and the speed, low latency, high throughput, security and reliability of network communication are important standards for the quality of network communication. At present, Linux system has no special optimization for TCP socket local network communication in network communication. It will set the MTU to 65536 by default to increase the size of network packets and reduce the action of kernel packet assembly. TCP socket local communication requires the processing of IP protocol and TCP protocol. There is a certain overhead in performance and communication rate, low efficiency, lack of security and reliability. If the system is connected to the external network, it may even be vulnerable to network attacks, and the resource occupation is large. In the scenario where a large number of short connections are frequently created and destroyed, TCP socket network communication will occupy a large number of network port resources. Summary of the invention
[0003] Technical problem to be solved by the present invention: In view of the above-mentioned problems in the prior art, a TCP socket local network communication optimization method and system are provided. The present invention aims to solve the problems of low communication rate, insufficient throughput, low latency, insufficient security and reliability, and large resource occupation in the TCP socket local network communication scenario.
[0004] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0005] A TCP socket local network communication optimization method comprises the following steps: hijacking and rewriting the socket network interface function by using the hook function technology for the application programs using the socket communication in the server and the client, and converting the TCP socket local network communication between the server and the client into the UDS socket local network communication between the server and the client through the rewritten socket network interface function.
[0006] Furthermore, the rewritten socket network interface function includes a rewritten server-side acceptance link function accept and a rewritten client-side request link function connect.
[0007] Furthermore, the execution steps of the rewritten server-side accept link function accept include:
[0008] S101, after receiving the client's request for establishing a link communication, using the original accepting link function original_accept to accept the client's request for establishing a link communication, and obtaining attribute information of the socket connection with the client;
[0009] S102, judging whether the socket connection with the client is a TCP connection according to the attribute information of the socket connection with the client, if it is a TCP connection, jumping to step S103; otherwise, directly returning the return value ret of the original accept link function original_accept, ending and exiting;
[0010] S103, obtain the client's IP address client_ip and the server's IP address server_ip. If the client's IP address client_ip and the server's IP address server_ip are the same, it is determined that the socket connection with the client is a TCP socket local network communication, and the execution jumps to step S104; otherwise, directly return the return value ret of the original accept link function original_accept, end and exit;
[0011] S104, create a UDS socket server communication, create a new file descriptor for UDS socket server communication by calling the socket function, and then copy the file descriptor by duplicating the file descriptor dup3 and overwrite the file descriptor of TCP socket local network communication, so that only the file descriptor of UDS socket server communication exists on the server, and then use the binding port function bind, the listening port function listen, and the original acceptance link function original_accept to continue to initialize the UDS socket server communication, and wait for the client's UDS socket connection request link;
[0012] S105, using the file descriptor of the UDS socket server communication to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom between the client and the client are all converted into the communication operations of the UDS socket.
[0013] Furthermore, the execution steps of the rewritten client request link function connect include:
[0014] S201, using the original request connection function original_connect to initiate a connection communication request to the server, and obtain attribute information of the socket connection with the server;
[0015] S202, judging whether the connection with the server socket is a TCP connection according to the attribute information of the connection with the server socket, if it is a TCP connection, jumping to step S203; otherwise, directly returning the return value ret of the original request link function original_connect, ending and exiting;
[0016] S203, obtain the client's IP address client_ip and the server's IP address server_ip. If the client's IP address client_ip and the server's IP address server_ip are the same, it is determined that the socket connection with the server is a TCP socket local network communication, and the execution jumps to step S204; otherwise, directly return the return value ret of the original request link function original_connect, end and exit;
[0017] S204, create a UDS socket client communication, create a new file descriptor for UDS socket client communication by calling the socket function, and then copy the file descriptor by using the copy file descriptor function dup3 to overwrite the file descriptor of the TCP socket local network communication so that the client only has the file descriptor of the UDS socket client communication, and then use the original request connection function original_connect to request a network connection, thereby completing the client's request to connect to the server;
[0018] S205, using the file descriptor of the client communication of the UDS socket to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom with the server are all converted into the communication operations of the UDS socket.
[0019] Further, in step S102, when judging whether the socket connection with the client is a TCP connection according to the attribute information of the socket connection with the client, it includes judging whether the attribute information of the socket connection with the client contains the protocol cluster AF_INET for TCP connection; if the protocol cluster AF_INET for TCP connection is included, then the socket connection with the client is judged to be a TCP connection; otherwise, it is judged that the socket connection with the client is not a TCP connection; in step S202, when judging whether the socket connection with the server is a TCP connection according to the attribute information of the socket connection with the server, it includes judging whether the attribute information of the socket connection with the server contains the protocol cluster AF_INET for TCP connection; if the protocol cluster AF_INET for TCP connection is included, then the socket connection with the server is judged to be a TCP connection; otherwise, it is judged that the socket connection with the server is not a TCP connection.
[0020] Furthermore, when the hook function technology is used to hijack and rewrite the socket network interface function for the applications using socket communication in the server and the client respectively, it includes using the pre-loaded shared library environment variable LD_PRELOAD to load the dynamic library unsocket_hook.so, dynamically hijacking the socket network interface function through dlsym and then rewriting it, so that the application using socket communication on the server uses the rewritten server's acceptance link function accept, and the application using socket communication on the client uses the rewritten client's request link function connect.
[0021] Furthermore, before the socket network interface function is hijacked and rewritten by the hook function technology for the application programs using socket communication in the server and the client, the method also includes: the server starts the socket communication, uses the binding port function bind and the listening port function listen to bind the port for the socket communication and starts listening.
[0022] The present invention also proposes a TCP socket local network communication optimization system, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute any one of the TCP socket local network communication optimization methods.
[0023] The present invention also proposes a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute any one of the TCP socket local network communication optimization methods through a processor.
[0024] The present invention also proposes a computer program product, comprising a computer program or instructions, wherein the computer program or instructions are programmed or configured to execute any one of the TCP socket local network communication optimization methods through a processor.
[0025] Compared with the prior art, the present invention mainly has the following advantages:
[0026] The present invention adopts hook function technology to hijack and rewrite the socket network interface functions of the server and the client. The rewritten socket network interface function converts the TCP socket local network communication between the server and the client into UDS socket local network communication with better performance, reliability and security, thereby solving the problems of low TCP socket network communication rate, insufficient throughput, low delay, insufficient security and reliability, and large resource occupation. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 is a flow chart of an embodiment of the present invention. DETAILED DESCRIPTION
[0028] Before introducing the specific embodiments of the present invention, related concepts and terms are first explained.
[0029] TCP: TCP is the abbreviation of Transmission Control Protocol. It is a widely used protocol in computer networks and it is located in the transport layer of the Internet Protocol Suite. TCP provides reliable, connection-oriented communication for transferring data between devices on an IP network. TCP ensures that data packets are delivered in the correct order, without errors, and without loss.
[0030] Socket: A socket is a programming interface for network communication. It provides a mechanism that enables processes (or threads) on different computers or the same computer to communicate and exchange data over the network.
[0031] hook: hook refers to the function of hijacking a specific function through the dlsym function and rewriting the function.
[0032] socket_fd: socket_fd refers to the file descriptor returned by socket communication. This file descriptor allows different computers or processes or threads on the same computer to exchange data.
[0033] Throughput: refers to the amount of data or data flow transmitted per unit time in a computer network. It measures the speed and capacity of network transmission, indicating how much data the network can transmit per unit time.
[0034] Low latency: refers to the time required to transmit data in a computer network as short as possible. Latency refers to the time interval between the sender sending the data and the receiver receiving the data.
[0035] UDS / uds socket: UDS (Unix Domain Socket) is a mechanism for inter-process communication (IPC). It allows two or more processes to communicate on the same machine through a file system path. Compared with TCP, it ensures that data packets are delivered in the correct order and without errors, without going through the network protocol stack.
[0036] UDS has the following advantages:
[0037] Performance advantage: UDS does not need to go through the network protocol stack, and data does not need to be processed by the network interface, IP protocol, and TCP protocol. Therefore, it is usually more efficient than TCP local communication in terms of performance.
[0038] Security: UDS can only be used for local communication and does not go through the network. Therefore, it is more secure than TCP local communication and is not vulnerable to network attacks.
[0039] Reliability: UDS is not affected by factors such as network status and bandwidth when communicating between processes, so it is usually more reliable than TCP local communication.
[0040] Less resource consumption: UDS does not need to occupy network ports, reducing the occupation of system resources. Especially in the scenario where a large number of short connections are frequently created and destroyed, the advantages of UDS are more obvious.
[0041] High throughput and low latency.
[0042] unscoket_hook.so: A dynamic library that dynamically hijacks fixed functions through dlsym and then rewrites them.
[0043] LD_PRELOAD: An environment variable of the Linux system used to specify the shared libraries to be preloaded when executing the program.
[0044] dup3 function: dup3 function is a system call in Unix / Linux system, used to copy file descriptors. Its function is to copy an already opened file descriptor to another file descriptor. If the new file descriptor is already in use, dup3 will close its previous file descriptor.
[0045] int dup3(int oldfd, int newfd, int flags);
[0046] oldfd: The file descriptor to be copied.
[0047] newfd: The new file descriptor to copy to.
[0048] flags: can be 0 or O_CLOEXEC
[0049] Scoket_app: refers to the application program for TCP Scoket local communication to perform network data transmission
[0050] Embodiment 1
[0051] This embodiment proposes a TCP socket local network communication optimization method to solve the problems of low network communication rate, insufficient throughput, low latency, insufficient security and reliability, and large resource occupation. The overall idea is to intercept the socket network programming interface through hook function technology to achieve the purpose of optimizing TCP socket local network communication, mainly by intercepting and rewriting certain interfaces of socket programming to convert TCP socket local communication into UDS socket communication.
[0052] Based on the above ideas, the method of this embodiment is as follows Figure 1 As shown, the specific steps include:
[0053] The server and client start socket communication respectively. The server uses the bind port function and the listen port function to bind the port for socket communication and start listening.
[0054] The server and the client respectively use hook function technology to hijack and rewrite the socket network interface function for the applications that use socket communication in the server and the client. The TCP socket local network communication between the server and the client is converted into UDS socket local network communication between the server and the client through the rewritten socket network interface function. The rewritten socket network interface function includes the rewritten server's acceptance link function accept and the rewritten client's request link function connect.
[0055] In this embodiment, when the server and the client use the hook function technology to hijack and rewrite the socket network interface function for the application programs that use socket communication in the server and the client respectively, it includes: using the pre-loaded shared library environment variable LD_PRELOAD to load the dynamic library unsocket_hook.so, dynamically hijacking the socket network interface function through dlsym and then rewriting it, so that the application program that uses socket communication on the server uses the rewritten server's acceptance link function accept, and the application program that uses socket communication on the client uses the rewritten client's request link function connect.
[0056] like Figure 1 As shown, in this embodiment, the execution steps of the rewritten server-side accept link function accept include:
[0057] S101, after receiving the client's request for establishing a link communication, using the original accepting link function original_accept to accept the client's request for establishing a link communication, and obtaining attribute information of the socket connection with the client;
[0058] S102, judging whether the socket connection with the client is a TCP connection according to the attribute information of the socket connection with the client, specifically judging whether the attribute information of the socket connection with the client includes the protocol cluster AF_INET of the TCP connection, if it includes the protocol cluster AF_INET of the TCP connection, judging that the socket connection with the client is a TCP connection, otherwise judging that the socket connection with the client is not a TCP connection; if it is a TCP connection, jumping to step S103; otherwise, directly returning the return value ret of the original accept link function original_accept, ending and exiting;
[0059] S103, use the peer name acquisition function getpeername to acquire the client's IP address client_ip, use the socket name acquisition function getsockname to acquire the server's IP address server_ip, and determine whether the server's IP address server_ip is the same as the client's IP address client_ip. If the client's IP address client_ip is the same as the server's IP address server_ip, it is determined that the socket connection with the client is a TCP socket local network communication, and the execution jumps to step S104; otherwise, directly return the return value ret of the original accept link function original_accept, and end and exit;
[0060] S104, create a UDS socket server communication, create a new UDS socket server communication file descriptor socket_fd1 by calling the socket function, and then copy the file descriptor socket_fd1 and overwrite the file descriptor of the TCP socket local network communication by copying the file descriptor function dup3, so that only the UDS socket server communication file descriptor socket_fd1 exists on the server, and then use the binding port function bind, the listening port function listen, and the original acceptance link function original_accept to continue to initialize the UDS socket server communication, and wait for the client's UDS socket connection request link;
[0061] S105, using the file descriptor of the UDS socket server communication to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom between the client and the client are all converted into the communication operations of the UDS socket.
[0062] Through the above steps, in this embodiment, when the server converts the TCP socket local communication into UDS socket communication, the hook technology is used to intercept the accept link function accept, and the intercepted original accept link function original_accept is first used to complete the server's acceptance request, and it is determined whether this socket communication is the protocol cluster AF_INET containing the TCP connection, that is, TCP socket communication, and then the socket name acquisition function getsockname and the peer name acquisition function getpeername are used to determine whether the server and client of this socket communication are the same IP address. If so, a UDS socket server initialization is created in the hook's accept link function accept, and then the file descriptor socket_fd1 initialized by the UDS socket server is copied to replace the previous file descriptor of the TCP socket, so that the attributes of the previous file descriptor become the file descriptor of the UDS socket network communication attributes, and subsequent communication is carried out through interfaces such as write / sendto, read / recvfrom, etc., all of which are UDS socket interfaces.
[0063] like Figure 1 As shown, in this embodiment, the execution steps of the rewritten client request link function connect include:
[0064] S201, using the original request connection function original_connect to initiate a connection communication request to the server, and obtain attribute information of the socket connection with the server;
[0065] S202, judging whether the socket connection with the server is a TCP connection according to the attribute information of the socket connection with the server, specifically judging whether the attribute information of the socket connection with the server includes the protocol cluster AF_INET of the TCP connection, if it includes the protocol cluster AF_INET of the TCP connection, judging that the socket connection with the server is a TCP connection, otherwise judging that the socket connection with the server is not a TCP connection; if it is a TCP connection, jumping to step S203; otherwise, directly returning the return value ret of the original request link function original_connect, ending and exiting;
[0066] S203, use the function getsockname to obtain the IP address of the client client_ip, use the function getpeername to obtain the IP address of the server server_ip, and determine whether the IP address of the server server_ip is the same as the IP address of the client client_ip. If the IP address of the client client_ip is the same as the IP address of the server server_ip, it is determined that the socket connection with the server is a TCP socket local network communication, and the execution jumps to step S204; otherwise, directly return the return value ret of the original request link function original_connect, end and exit;
[0067] S204, create a UDS socket client communication, create a new UDS socket client communication file descriptor scoket_fd2 by calling the socket function, and then copy the file descriptor scoket_fd2 and overwrite the file descriptor of the TCP socket local network communication by copying the file descriptor function dup3, so that the client only has the UDS socket client communication file descriptor scoket_fd2, and then use the original request connection function original_connect to request a network connection, thereby completing the client's request to connect to the server;
[0068] S205, using the file descriptor of the UDS socket client communication to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom with the server are all converted into the communication operations of the UDS socket.
[0069] Through the above steps, in this embodiment, when the client converts the TCP socket local communication into UDS socket communication, the hook technology is used to intercept the request link function connect, and the intercepted original request link function original_connect is first used to complete the client's connection request, and it is determined whether this socket communication is the protocol cluster AF_INET that includes TCP connection, that is, TCP socket communication, and then the socket name acquisition function getsockname and the peer name acquisition function getpeername are used to determine whether the socket communication server and the client are the same IP address. If so, a UDS socket client initialization is created in the hook request link function connect, and then the file descriptor scoket_fd2 initialized by the UDS socket client is copied, replaced and overwritten by the file descriptor of the local network communication of the TCP socket before, so that the attributes of the previous file descriptor become the file descriptor of the UDS socket network communication attributes, and subsequent communication is carried out through interfaces such as write / sendto, read / recvfrom, etc., all of which are UDS socket interfaces.
[0070] The following takes the installation of Kylin V10 desktop system on an ARM architecture physical machine as an example to explain the steps of converting TCP socket local communication to UDS socket communication on the server and client:
[0071] 1. For the socket server, in the application that starts the socket, the accept function is called on the server through the hook technology for rewriting. The following functions need to be implemented in the rewritten accept function:
[0072] 1. First use the original_accept original acceptance function to accept the connection request from the TCP socket client, so that you can get the client's socket related attribute information.
[0073] 2. Determine whether this socket is TCP communication. If so, proceed to the next step. Otherwise, directly return the return value of the original_accept function.
[0074] 3. Use the getsockname and getpeername functions to determine whether the IP addresses of the server and the client are the same. If they are the same, it is TCP socket local communication, and then proceed to the next step. If they are not the same, directly return the return value of the original_accept function in step 2.
[0075] 4. Create a UDS socket server communication, create a new file descriptor for UDS socket communication by calling the socket function, and then use the dup3 function to copy this file descriptor to overwrite the previous TCP socket local network communication file descriptor, so that only the file descriptor of the UDS socket server will exist, and then use the bind, listen, and original_accept functions to continue to initialize the communication of the UDS socket server, and wait for the client's UDS socket communication request connection.
[0076] 5. The subsequent use of the file descriptor generated by the TCP socket local communication covered by the new UDS socket for network data communication will become UDS socket communication, whether using write / sendto or read / recvfrom.
[0077] 6. Through LD_PRELOAD=unsocket_hook.so, the socket_app communication is executed to perform the application program for network data transmission. In this way, the specific function can be hijacked and rewritten through the dlsym() function at the application layer to optimize the local network communication of TCP socket and solve the problems of low network communication rate, insufficient throughput, insufficient security and reliability, and large resource occupation.
[0078] 7. Then use the tcpdump tool to capture TCP network packets. If no TCP network communication packets can be captured, it means that the TCP socket local communication has been successfully converted into UDS socket communication. How to use the tcpdump tool is well known to those skilled in the art and will not be described in detail.
[0079] 2. For the socket client, in the application that starts the socket, the connect function is called on the server side through the hook technology for rewriting. The following functions need to be implemented in the rewritten function:
[0080] 1. Use the original_connect original request link function to request a connection to the TCP socket server, so that you can get the socket-related attribute information of the server.
[0081] 2. Determine whether this socket is TCP communication. If so, proceed to the next step. Otherwise, directly return the return value of the original_connect function.
[0082] 3. Use the getsockname and getpeername functions to determine whether the IP addresses of the server and client are the same. If they are the same, it is TCP socket local communication, and then proceed to the next step. If they are not the same, directly return the return value of the original_connect function in step 2.
[0083] 4. Create a UDS socket client communication, create a new file descriptor for UDS socket client communication by calling the socket function, and then use the dup3 function to copy this file descriptor to overwrite the previous TCP socket local network communication file descriptor, so that only the UDS socket client file descriptor will exist, and then use the original_connect function to request a network connection, thereby completing the client's request to connect to the server.
[0084] 5. The subsequent use of the file descriptor generated by the TCP socket local communication covered by the new UDS socket for network data communication will become UDS socket communication, whether using write / sendto or read / recvfrom.
[0085] 6. Through LD_PRELOAD=unsocket_hook.sosocket_app, the application program that executes socket_app communication for network data transmission can be executed. In this way, the specific function can be hijacked and rewritten through the dlsym() function at the application layer to optimize the local network communication of TCP socket and solve the problems of low network communication rate, insufficient throughput, insufficient security and reliability, and large resource occupation.
[0086] 7. Then use the tcpdump tool to capture TCP network packets. If no TCP network communication packets can be captured, it means that the TCP socket local communication has been successfully converted into UDS socket communication. How to use the tcpdump tool is well known to those skilled in the art and will not be described in detail.
[0087] Embodiment 2
[0088] This embodiment proposes a TCP socket local network communication optimization system, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the TCP socket local network communication optimization method described in Example 1.
[0089] This embodiment also proposes a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the TCP socket local network communication optimization method described in Embodiment 1 through a processor.
[0090] This embodiment also proposes a computer program product, including a computer program or instructions. The computer program or instructions are programmed or configured to execute the TCP socket local network communication optimization method described in Embodiment 1 through a processor.
[0091] In summary, the present invention proposes a method for optimizing TCP socket local network communication, and proposes a system, a computer-readable storage medium and a computer program product for executing the method. The method converts TCP socket local communication into UDS socket communication by hijacking and rewriting the socket network interface function for the application using socket communication on the server and the client, respectively. This effectively solves the problems of low network communication rate, insufficient throughput, low latency, insufficient security and reliability, and large resource occupation when the application uses TCP socket communication.
[0092] Those skilled in the art should understand that the technical solutions provided by the embodiments of the present application may be in the form of methods, systems, or computer program products. Therefore, the present application may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may take the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes. The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the process Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific way, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the functions specified in the process. Figure 1A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide for implementing the process in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0093] The above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
Claims
1. A TCP socket local network communication optimization method, characterized in that: The method comprises the following steps: hijacking and rewriting the socket network interface function by using the hook function technology for the application programs using the socket communication in the server and the client, converting the TCP socket local network communication between the server and the client into the UDS socket local network communication between the server and the client through the rewritten socket network interface function, wherein the rewritten socket network interface function comprises the rewritten accept link function of the server and the rewritten request link function of the client, and the execution steps of the rewritten accept link function of the server comprise: S101, after receiving the client's request for establishing a link communication, using the original accepting link function original_accept to accept the client's request for establishing a link communication, and obtaining attribute information of the socket connection with the client; S102, judging whether the socket connection with the client is a TCP connection according to the attribute information of the socket connection with the client, if it is a TCP connection, jumping to step S103; otherwise, directly returning the return value ret of the original accept link function original_accept, ending and exiting; S103, obtain the client's IP address client_ip and the server's IP address server_ip. If the client's IP address client_ip and the server's IP address server_ip are the same, it is determined that the socket connection with the client is a TCP socket local network communication, and the execution jumps to step S104; otherwise, directly return the return value ret of the original accept link function original_accept, end and exit; S104, create a UDS socket server communication, create a new file descriptor for the UDS socket server communication by calling the socket function, and then copy the file descriptor by duplicating the file descriptor dup3 and overwrite the file descriptor of the TCP socket local network communication, so that only the file descriptor for the UDS socket server communication exists on the server, and then use the bind port function bind, the listen port function listen, and the original accept link function original_accept to continue to initialize the UDS socket server communication, and wait for the client's UDS socket connection request link; S105, using the file descriptor of the UDS socket server communication to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom between the client and the client are all converted into the communication operations of the UDS socket; The execution steps of the rewritten client request link function connect include: S201, using the original request connection function original_connect to initiate a connection communication request to the server, and obtain attribute information of the socket connection with the server; S202, judging whether the connection with the server socket is a TCP connection according to the attribute information of the connection with the server socket, if it is a TCP connection, jumping to step S203; otherwise, directly returning the return value ret of the original request link function original_connect, ending and exiting; S203, obtain the client's IP address client_ip and the server's IP address server_ip. If the client's IP address client_ip and the server's IP address server_ip are the same, it is determined that the socket connection with the server is a TCP socket local network communication, and the execution jumps to step S204; otherwise, directly return the return value ret of the original request link function original_connect, end and exit; S204, create a UDS socket client communication, create a new file descriptor for the UDS socket client communication by calling the socket function, and then use the copy file descriptor function dup3 to copy the file descriptor and overwrite the file descriptor of the TCP socket local network communication so that the client only has the file descriptor of the UDS socket client communication, and then use the original request connection function original_connect to request a network connection, thereby completing the client's request to connect to the server; S205, using the file descriptor of the UDS socket client communication to perform network data communication, so that the communication operations of the write function write or the send function sendto, the read function read or the receive function recvfrom with the server are all converted into the communication operations of the UDS socket.
2. The TCP socket local network communication optimization method according to claim 1, characterized in that: In step S102, when judging whether the socket connection with the client is a TCP connection based on the attribute information of the socket connection with the client, it includes judging whether the attribute information of the socket connection with the client includes the protocol cluster AF_INET for TCP connection. If the protocol cluster AF_INET for TCP connection is included, the socket connection with the client is judged to be a TCP connection; otherwise, the socket connection with the client is judged not to be a TCP connection. In step S202, when judging whether the socket connection with the server is a TCP connection based on the attribute information of the socket connection with the server, it includes judging whether the attribute information of the socket connection with the server includes the protocol cluster AF_INET for TCP connection. If the protocol cluster AF_INET for TCP connection is included, the socket connection with the server is judged to be a TCP connection; otherwise, the socket connection with the server is judged not to be a TCP connection.
3. The TCP socket local network communication optimization method according to claim 1, characterized in that: When the hook function technology is used to hijack and rewrite the socket network interface function for the application programs using socket communication in the server and the client respectively, it includes using the pre-loaded shared library environment variable LD_PRELOAD to load the dynamic library unsocket_hook.so, dynamically hijacking the socket network interface function through dlsym and then rewriting it, so that the application program using socket communication on the server uses the rewritten server acceptance link function accept, and the application program using socket communication on the client uses the rewritten client request link function connect.
4. The TCP socket local network communication optimization method according to claim 1, characterized in that: Before the socket network interface function is hijacked and rewritten by using hook function technology for applications using socket communication in the server and client, the process also includes: the server starts socket communication, uses the binding port function bind and the listening port function listen to bind the port for socket communication and starts listening.
5. A TCP socket local network communication optimization system, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the TCP socket local network communication optimization method described in any one of claims 1 to 4.
6. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the TCP socket local network communication optimization method described in any one of claims 1 to 4 through a processor.
7. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the TCP socket local network communication optimization method described in any one of claims 1 to 4 through a processor.
Citation Information
Patent Citations
UDP (User Datagram Protocol) link monitoring method and system and electronic equipment
CN112838966A
Socket network communication optimization method and system, and medium
CN118972472A