Socket communication method and system for implementing local TCP using shared memory
By replacing the Socket communication key function of TCP before the program starts the main function is implemented based on shared memory, the communication delay problem caused by frequent system calls in the prior art is solved, and the rapid efficiency of local socket communication is achieved.
Patent Information
- Application Number
- CN202411229267.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-03
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2044-09-03
AI Technical Summary
The existing TCP Socket communication method requires switching between user state and kernel state, resulting in frequent system calls, resulting in greater delay in local socket communication, especially on domestic processor platforms.
Before the program starts the main function main, the key Socket communication function of TCP is replaced with a shared memory-based implementation, reducing the switching between user-state and kernel-state, and using shared memory to transmit and receive Socket data, achieving fast communication between local processes.
By reducing the delay caused by system calls, the communication time of local sockets is significantly shortened and communication efficiency is improved.
Smart Images

Figure CN118733304B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to, in particular to a Socket communication method and system for realizing local TCP by sharing memory. Background Art
[0002] TCP socket communication is the most commonly used communication method in operating systems. It can support inter-process communication between remote devices and local processes (between different processes in the same system). In order to be compatible with both scenarios, many application developers do not distinguish between local and remote communication.
[0003] The existing socket communication method needs to switch from user mode to kernel mode when sending and receiving data. For example, when the client sends data to the server, the function call stack is as follows: the client calls the write function; falls into the Linux kernel; the vfs_write function is called; the sock_sendmsg function is called; the tcp_sendmsg function is called. It can be clearly seen that the call stack of the entire system in socket communication is very deep, which leads to a large delay in local socket communication.
[0004] Since TCP socket communication needs to go through layers of system calls, requires switching between user mode and kernel mode, and needs to go through the operating system's protocol stack, the entire process is very cumbersome and complicated, and the function call stack is very deep, so the communication delay is relatively large, especially on the current domestic processor platform. The delay is even greater due to low hardware performance. Summary of the invention
[0005] The technical problem to be solved by the present invention is as follows: In view of the above problems in the prior art, a method and system for realizing local TCP Socket communication by shared memory are provided, which realizes the sending and receiving of socket data in a shared memory manner, reduces the delay caused by system calls, and thus achieves the purpose of fast communication.
[0006] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0007] A method for realizing local TCP Socket communication by shared memory includes: for TCP Socket communication between local processes, a section of initialization code is first executed before the main function main of the program is started to replace the key function of TCP Socket communication with the key function of Socket communication obtained by realizing the Socket data receiving and sending of the sender and the receiver of the local process based on the shared memory, thereby realizing the acceleration of TCP Socket communication between local processes.
[0008] Furthermore, the key functions of the TCP Socket communication include accepting the client connection request function, connect initializing the connection server request function, send sending data function, write data to the file descriptor function, read function, receive data function recv, close the connection function, and shut down the sending or receiving function.
[0009] Furthermore, the execution of a section of initialization code to replace the TCP Socket communication key function with the Socket communication key function obtained by implementing the Socket data transmission and reception of the sender and receiver of the local process based on shared memory includes:
[0010] S101, obtaining the environment variables of the Socket communication;
[0011] S102, obtaining addresses of a function for accepting a client connection request, a function for initializing a connection to a server, a function for sending data, a function for writing data to a file descriptor, a function for reading data, a function for receiving data, a function for closing a connection, and a function for shutting down sending or receiving data from an environment variable of the Socket communication;
[0012] S103, assign the address of the accept function of the client connection request to the pointer function ky_accept which accepts the client connection request based on the shared memory; assign the address of the connect function which initializes the connection server request to the pointer function ky_connect which initializes the connection server request based on the shared memory; assign the address of the send function to the pointer function ky_send which sends the data based on the shared memory; assign the address of the write function which writes the data to the file descriptor to the pointer function ky_write which writes the data to the file descriptor based on the shared memory; assign the address of the read function to the pointer function ky_read which reads based on the shared memory; assign the address of the receive data function recv to the pointer function ky_recv which receives the data based on the shared memory; assign the address of the close connection function to the pointer function ky_close which closes the connection based on the shared memory; assign the address of the shutdown function which closes the sending or receiving to the pointer function ky_shutdown which closes the sending or receiving based on the shared memory.
[0013] Further, the key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory include a client connection request accept function and a server connection initialization request connect function. The client connection request accept function accept accepts the client connection request based on shared memory, and the server connection initialization request connect initializes the server connection request based on shared memory. Both include:
[0014] S200, executing the corresponding pointer function;
[0015] S201, determine whether the environment variable of the Socket communication is a specified value, if not, end and exit, if yes, execute step S202;
[0016] S202, determine whether the destination address of the Socket data is the local address, if not, end and exit, if yes, execute step S203;
[0017] S203, obtaining the local port and remote port number of the Socket communication;
[0018] S204, creating a shared memory according to the local port and / or remote port number.
[0019] Further, the Socket communication key functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory include a data sending function send and a data writing function write to the file descriptor, wherein the data sending function send implements sending data based on the shared memory, and the data writing function write to the file descriptor implements writing data to the file descriptor based on the shared memory, both of which include:
[0020] S301, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S302;
[0021] S302, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S303;
[0022] S303, judging whether the sending condition of the shared memory is satisfied according to the read control field of the control field in the shared memory, if not, waiting for the condition to be satisfied, if satisfied, executing step S304;
[0023] S304, setting the write control field to monopolize the shared memory operation, copying the data to the shared memory, and clearing the write control field after the copying is completed.
[0024] Furthermore, the key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory include a read function read and a receive data function recv, wherein the read function read implements reading based on the shared memory, and the receive data function recv implements receiving data based on the shared memory, both of which include:
[0025] S401, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S402;
[0026] S402, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S403;
[0027] S403, judging whether the receiving condition of the shared memory is satisfied according to the write control field of the control field in the shared memory, if not, waiting for the condition to be satisfied, if satisfied, executing step S404;
[0028] S404, setting the read control field to monopolize the operation of the shared memory, taking the data out of the shared memory, and clearing the read control field after taking out the data.
[0029] Furthermore, the key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory include a connection closing function close and a sending or receiving closing function shutdown, wherein the connection closing function close closes the connection based on the shared memory, and the sending or receiving closing function shutdown closes the sending or receiving based on the shared memory and both include:
[0030] S501, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S502;
[0031] S502, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S503;
[0032] S503, releasing shared memory resources;
[0033] S504, execute the corresponding pointer function and end the function.
[0034] The present invention also provides a Socket communication system for implementing local TCP with shared memory, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute any one of the Socket communication methods for implementing local TCP with shared memory.
[0035] The present invention also provides a computer-readable storage medium, in which a computer program / instruction is stored. The computer program / instruction is programmed or configured to execute any one of the shared memory to implement the local TCP Socket communication method through a processor.
[0036] The present invention also provides a computer program product, comprising a computer program / instruction, wherein the computer program / instruction is programmed or configured to execute any one of the shared memory to implement local TCP Socket communication methods through a processor.
[0037] Compared with the prior art, the present invention has the following advantages:
[0038] The present invention replaces the Socket communication key function before the program startup main function main, and the new Socket communication key function realizes the Socket data reception and transmission of the sender and receiver of the local process based on the shared memory, reduces the switching of the user state and the kernel state, reduces the system call process, and thus greatly shortens the communication time of the local socket. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 is a flow chart of an embodiment of the present invention.
[0040] Figure 2 Schematic diagram of a shared memory layout in an embodiment of the present invention.
[0041] Figure 3 It is a flowchart of the function accept for accepting client connection request and the function connect for initializing connection to the server in the key function of Socket communication after replacement in the embodiment of the present invention.
[0042] Figure 4 The present invention is a flowchart of a function send for sending data and a function write for writing data to a file descriptor in a key function of Socket communication after replacement in an embodiment of the present invention.
[0043] Figure 5 It is a flowchart of the read function and the data receiving function recv in the key functions of Socket communication after replacement in the embodiment of the present invention.
[0044] Figure 6 The present invention is a flowchart of a connection closing function close and a sending or receiving closing function shutdown in a key function of Socket communication after replacement in an embodiment of the present invention. DETAILED DESCRIPTION
[0045] Before introducing the specific embodiments of the present invention, related terms are explained first.
[0046] TCP: Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. Socket: A socket is an endpoint for communication between two processes on a network.
[0047] accept function: used to accept a connection request from the client and return a new socket for communicating with the client.
[0048] connect function: used to connect the client socket to the server socket to achieve communication between the client and the server.
[0049] send function: used to send data from one end to another.
[0050] write function: used to send data from one end to another, similar to the send function.
[0051] Read function: used to receive data sent by the other end.
[0052] recv function: used to receive data sent by the other end, similar to the read function.
[0053] close function: used to close the client socket to release system resources.
[0054] Shutdown function: used to close the client socket to release system resources, similar to the close function.
[0055] Embodiment 1
[0056] This embodiment proposes a method for implementing local TCP Socket communication by shared memory. For TCP Socket communication between local processes, a section of initialization code is executed before the main function main of the program is started to replace the key functions of TCP Socket communication with the key functions of Socket communication obtained by implementing the sending and receiving of Socket data of the sender and receiver of the local process based on shared memory, thereby accelerating TCP Socket communication between local processes. By replacing the key functions of TCP Socket communication in this step, the sending and receiving of socket data is realized in a shared memory manner, reducing the delay caused by system calls, thereby achieving the purpose of fast communication.
[0057] In this embodiment, the key functions of the TCP Socket communication include accepting the client connection request function, connect initializing the connection server request function, send sending data function, write data to the file descriptor function, read function, receive data function recv, close the connection function, and shut down the sending or receiving function.
[0058] The overall idea of the method of this embodiment is to replace the key functions such as accept, connect, send, write, read, recv, close, shutdown, etc. before the main function is executed with the Socket data transmission and reception of the sender and receiver of the local process based on shared memory to obtain new Socket communication key functions such as accept, connect, send, write, read, recv, close, shutdown, etc.; the new Socket communication key functions re-implement the process of the accept function; re-implement the process of the connect function; re-implement the process of the send function; re-implement the process of the write function; re-implement the process of the read function; re-implement the process of the recv function; re-implement the process of the close function; re-implement the process of the shutdown function. In these replaced Socket communication key functions, the operations of creating, managing, reading, writing, and releasing shared memory are implemented.
[0059] like Figure 1 As shown, in this embodiment, the execution of a section of initialization code replaces the TCP Socket communication key function with the Socket communication key function obtained by implementing the Socket data transmission and reception of the sender and receiver of the local process based on shared memory, including:
[0060] S101, obtaining the environment variables of the Socket communication;
[0061] S102, obtaining addresses of a function for accepting a client connection request, a function for initializing a connection to a server, a function for sending data, a function for writing data to a file descriptor, a function for reading data, a function for receiving data, a function for closing a connection, and a function for shutting down sending or receiving data from an environment variable of the Socket communication;
[0062] S103, assign the address of the accept function of the client connection request to the pointer function ky_accept which accepts the client connection request based on the shared memory; assign the address of the connect function which initializes the connection server request to the pointer function ky_connect which initializes the connection server request based on the shared memory; assign the address of the send function to the pointer function ky_send which sends the data based on the shared memory; assign the address of the write function which writes the data to the file descriptor to the pointer function ky_write which writes the data to the file descriptor based on the shared memory; assign the address of the read function to the pointer function ky_read which reads based on the shared memory; assign the address of the receive data function recv to the pointer function ky_recv which receives the data based on the shared memory; assign the address of the close connection function to the pointer function ky_close which closes the connection based on the shared memory; assign the address of the shutdown function which closes the sending or receiving to the pointer function ky_shutdown which closes the sending or receiving based on the shared memory.
[0063] like Figure 2 As shown, in this embodiment, the shared memory is divided into three intervals, wherein the first interval stores the buffer written by the client and read by the server during Socket communication, the second interval stores the buffer written by the client and read by the server during Socket communication, and the third interval stores the control field of the buffer. The control field of the buffer in this embodiment includes:
[0064] 1. The length of the buffer written by the client and read by the server;
[0065] 2. The address number that the current client can write to;
[0066] 3. The address number that the current server can read;
[0067] 4. The client reads the buffer length written by the server;
[0068] 5. The address number that the current server can write to;
[0069] 6. The address number that the current client can read;
[0070] 7. The client writes the status judgment field that the server reads;
[0071] 8. The client reads the status judgment field written by the server.
[0072] like Figure 3As shown, in this embodiment, in the Socket communication key functions obtained by implementing the Socket data receiving and sending of the sender and the receiver of the local process based on shared memory, the accept client connection request function accept implements the acceptance of the client connection request based on shared memory, and the initialization connection server request function connect implements the initialization connection server request based on shared memory, both of which include:
[0073] S200, executing the corresponding pointer function to execute the corresponding socket communication key function before replacement;
[0074] S201, determine whether the environment variable of the Socket communication is a specified value, if not, end and exit, if yes, execute step S202;
[0075] S202, determine whether the destination address of the Socket data is the local address, if not, end and exit, if yes, execute step S203;
[0076] S203, obtaining the local port and remote port number of the Socket communication;
[0077] S204, creating a shared memory based on the local port and / or remote port number. For example, when creating a shared memory segment, the port number is associated with the identifier of the shared memory segment. When data related to a specific port needs to be accessed, data sharing and communication is achieved by reading and writing data in the shared memory segment.
[0078] Specifically, the replaced accept function accepts client connection requests based on shared memory to implement the following process for accepting client connection requests:
[0079] 1. Execute the pointer function ky_accept, thereby executing the accept function that accepts the client connection request before replacement;
[0080] 2. Determine whether the environment variable of Socket communication is 1. If not, end the function. If yes, proceed to the next step.
[0081] 3. Determine whether the destination address of the Socket data is the local address. If not, end the function. If yes, proceed to the next step.
[0082] 4. Get the local port and remote port number;
[0083] 5. Create shared memory based on the port number;
[0084] 6. End the function.
[0085] Specifically, after replacement, the process of implementing the initialization connection server request function connect based on shared memory to initialize the connection server request is as follows:
[0086] 1. Execute the pointer function ky_connect to execute the initialization connection server request function connect before replacement;
[0087] 2. Determine whether the environment variable of Socket communication is 1. If not, end the function. If yes, proceed to the next step.
[0088] 3. Determine whether the destination address of the Socket data is the local address. If not, end the function. If yes, proceed to the next step.
[0089] 4. Get the local port and remote port number;
[0090] 5. Create shared memory based on the port number;
[0091] 6. End the function.
[0092] like Figure 4 As shown, in this embodiment, in the Socket communication key functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory, the data sending function send implements the sending of data based on the shared memory, and the data writing function write to the file descriptor implements the writing of data to the file descriptor based on the shared memory, both of which include:
[0093] S301, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function to execute the corresponding Socket communication key function before replacement, and end the function, if yes, execute step S302;
[0094] S302, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S303;
[0095] S303, judging whether the sending condition of the shared memory is met according to the read control field of the control field in the shared memory, for example, judging whether the value in the read control field is a specified value, if yes, the condition is met, otherwise the condition is not met, if not, waiting for the condition to be met, if yes, executing step S304;
[0096] S304, setting the write control field to monopolize the shared memory operation, copying the data to the shared memory, and clearing the write control field after the copying is completed.
[0097] Specifically, the replaced data sending function send implements the process of sending data based on shared memory as follows:
[0098] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_send pointer function to execute the send function before replacement and end the function. If yes, proceed to the next step.
[0099] 2. Determine whether the shared memory is created. If not, execute the ky_send pointer function and end the function. If yes, proceed to the next step.
[0100] 3. Determine whether the sending conditions of the shared memory are met. If not, wait until the conditions are met. If they are met, proceed to the next step. The conditions are met by determining the read control field of the control field in the shared memory.
[0101] 4. Copy the data to the shared memory. Set the write control field before copying to monopolize the shared memory operation. Clear the write control field after the copy is completed.
[0102] 5. The function ends.
[0103] Specifically, after replacement, the process of writing data to the file descriptor based on shared memory is as follows:
[0104] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_write pointer function to execute the write function that writes data to the file descriptor before replacement and end the function. If yes, proceed to the next step.
[0105] 2. Determine whether the shared memory is created. If not, execute the ky_write pointer function and end the function. If yes, proceed to the next step.
[0106] 3. Determine whether the sending conditions of the shared memory are met. If not, wait until the conditions are met. If they are met, proceed to the next step. The conditions are met by determining the read control field of the control field in the shared memory.
[0107] 4. Copy the data to the shared memory. Set the write control field before copying to monopolize the shared memory operation. Clear the write control field after the copy is completed.
[0108] 5. The function ends.
[0109] like Figure 5 As shown, in this embodiment, in the Socket communication key functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory, the read function read implements reading based on the shared memory, and the receive data function recv implements receiving data based on the shared memory, both of which include:
[0110] S401, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function to execute the corresponding Socket communication key function before replacement, and end the function, if yes, execute step S402;
[0111] S402, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S403;
[0112] S403, judging whether the receiving condition of the shared memory is met according to the write control field of the control field in the shared memory, for example, judging whether the value in the write control field is a specified value, if yes, the condition is met, otherwise the condition is not met, if not, waiting for the condition to be met, if yes, executing step S404;
[0113] S404, setting the read control field to monopolize the operation of the shared memory, taking the data out of the shared memory, and clearing the read control field after taking out the data.
[0114] Specifically, the replaced read function reads based on shared memory to achieve the following reading process:
[0115] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_read pointer function to execute the read function before replacement and end the function. If yes, proceed to the next step.
[0116] 2. Determine whether the shared memory is created. If not, execute the ky_read pointer function and end the function. If yes, proceed to the next step.
[0117] 3. Determine whether the receiving conditions of the shared memory are met. If not, wait until the conditions are met. If they are met, proceed to the next step. The conditions are met by determining the write control field of the control field in the shared memory.
[0118] 4. Take the data out of the shared memory, set the read control field before taking out the data, monopolize the operation of the shared memory, and clear the read control field after taking out the data;
[0119] 5. The function ends.
[0120] Specifically, the replaced data receiving function recv implements the process of receiving data based on shared memory as follows:
[0121] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_recv pointer function to execute the data receiving function recv before replacement and end the function. If yes, proceed to the next step.
[0122] 2. Determine whether the shared memory is created. If not, execute the ky_recv pointer function and end the function. If yes, proceed to the next step.
[0123] 3. Determine whether the receiving conditions of the shared memory are met. If not, wait until the conditions are met. If they are met, proceed to the next step. The conditions are met by determining the write control field of the control field in the shared memory.
[0124] 4. Take the data out of the shared memory, set the read control field before taking out the data, monopolize the operation of the shared memory, and clear the read control field after taking out the data;
[0125] 5. The function ends.
[0126] like Figure 6 As shown, in this embodiment, in the Socket communication key functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on the shared memory, the connection closing function close implements the connection closing based on the shared memory, and the shutdown sending or receiving function shutdown implements the shutdown sending or receiving based on the shared memory, both of which include:
[0127] S501, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function to execute the corresponding Socket communication key function before replacement, and end the function, if yes, execute step S502;
[0128] S502, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S503;
[0129] S503: Release shared memory resources.
[0130] S504, execute the corresponding pointer function and end the function.
[0131] Specifically, the replaced closing connection function close implements the following process of closing the connection based on shared memory:
[0132] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_close pointer function to execute the close connection function before replacement and end the function. If yes, proceed to the next step.
[0133] 2. Determine whether the shared memory is created. If not, execute the ky_close pointer function and end the function. If yes, proceed to the next step.
[0134] 3. Release shared memory resources;
[0135] 4. Execute the ky_close pointer function;
[0136] 5. The function ends.
[0137] Specifically, the replaced shutdown function shutdown implements the process of shutting down sending or receiving based on shared memory as follows:
[0138] 1. Determine whether the environment variable of Socket communication is 1. If not, execute the ky_shutdown pointer function to execute the shutdown function of the send or receive function before replacement and end the function. If yes, proceed to the next step.
[0139] 2. Determine whether the shared memory is created. If not, execute the ky_shutdown pointer function and end the function. If yes, proceed to the next step.
[0140] 3. Release shared memory resources;
[0141] 4. Execute the ky_shutdown pointer function;
[0142] 5. The function ends.
[0143] Embodiment 2
[0144] This embodiment proposes a Socket communication system for implementing local TCP using shared memory, including a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute the Socket communication method for implementing local TCP using shared memory described in the first embodiment.
[0145] This embodiment also proposes a computer-readable storage medium, in which a computer program / instruction is stored. The computer program / instruction is programmed or configured to execute the shared memory to implement the local TCP Socket communication method described in the first embodiment through a processor.
[0146] This embodiment also proposes a computer program product, including a computer program / instruction, which is programmed or configured to execute the shared memory to implement the local TCP Socket communication method described in the first embodiment through a processor.
[0147] In summary, the present invention proposes a method and system for implementing local TCP Socket communication by shared memory, which replaces key functions such as accept, connect, send, write, read, recv, close, and shutdown before the main function is executed; the processes of accept function, connect function, send function, write function, read function, recv function, close function, and shutdown function are re-implemented for the replaced key functions, and local TCP socket communication is implemented in a shared memory communication mode. By reducing the switching between user state and kernel state and the process of system call, the communication time of the local socket is greatly shortened.
[0148] Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can 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 code. 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 1 A 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 1The steps for the functions specified in one or more boxes.
[0149] 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 method for implementing local TCP Socket communication using shared memory, characterized in that: Including TCP Socket communication between local processes, before starting the main function main of the program, a section of initialization code is executed to replace the key function of TCP Socket communication with the key function of Socket communication obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory, thereby realizing the acceleration of TCP Socket communication between local processes, and the execution of a section of initialization code to replace the key function of TCP Socket communication with the key function of Socket communication obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory includes: S101, obtaining the environment variables of the Socket communication; S102, obtaining addresses of a function for accepting a client connection request, a function for initializing a connection to a server, a function for sending data, a function for writing data to a file descriptor, a function for reading data, a function for receiving data, a function for closing a connection, and a function for shutting down sending or receiving data from an environment variable of the Socket communication; S103, assign the address of the accept function of the client connection request to the pointer function ky_accept which accepts the client connection request based on the shared memory; assign the address of the connect function which initializes the connection server request to the pointer function ky_connect which initializes the connection server request based on the shared memory; assign the address of the send function to the pointer function ky_send which sends the data based on the shared memory; assign the address of the write function which writes the data to the file descriptor to the pointer function ky_write which writes the data to the file descriptor based on the shared memory; assign the address of the read function to the pointer function ky_read which reads based on the shared memory; assign the address of the receive data function recv to the pointer function ky_recv which receives the data based on the shared memory; assign the address of the close connection function to the pointer function ky_close which closes the connection based on the shared memory; assign the address of the shutdown function which closes the sending or receiving to the pointer function ky_shutdown which closes the sending or receiving based on the shared memory.
2. The method for implementing local TCP Socket communication using shared memory according to claim 1, characterized in that: The key functions of Socket communication include accepting client connection request function, connect initializing connection server request function, send sending function, write data to file descriptor function, read function, receive data function recv, close connection function, close sending or receiving function shutdown.
3. The method for implementing local TCP Socket communication using shared memory according to claim 1, characterized in that: The key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and receiver of the local process based on shared memory include the accept function for accepting the client connection request and the connect function for initializing the connection server request. The accept function for accepting the client connection request is based on shared memory to accept the client connection request, and the connect function for initializing the connection server request is based on shared memory to initialize the connection server request. Both of them include: S200, executing the corresponding pointer function; S201, determine whether the environment variable of the Socket communication is a specified value, if not, end and exit, if yes, execute step S202; S202, determine whether the destination address of the Socket data is the local address, if not, end and exit, if yes, execute step S203; S203, obtaining the local port and remote port number of the Socket communication; S204, creating a shared memory according to the local port and / or remote port number.
4. The method for implementing local TCP Socket communication using shared memory according to claim 1, characterized in that: The key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory include a data transmission function send and a data writing function write to a file descriptor, wherein the data transmission function send implements data transmission based on shared memory, and the data writing function write to a file descriptor implements data writing to a file descriptor based on shared memory, both of which include: S301, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S302; S302, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S303; S303, judging whether the sending condition of the shared memory is satisfied according to the read control field of the control field in the shared memory, if not, waiting for the condition to be satisfied, if satisfied, executing step S304; S304, setting the write control field to monopolize the shared memory operation, copying the data to the shared memory, and clearing the write control field after the copying is completed.
5. The method for implementing local TCP Socket communication using shared memory according to claim 1, characterized in that: The key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory include a read function read and a receive data function recv. The read function read implements reading based on shared memory, and the receive data function recv implements receiving data based on shared memory. Both of them include: S401, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S402; S402, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S403; S403, judging whether the receiving condition of the shared memory is satisfied according to the write control field of the control field in the shared memory, if not, waiting for the condition to be satisfied, if satisfied, executing step S404; S404, setting the read control field to monopolize the operation of the shared memory, taking the data out of the shared memory, and clearing the read control field after taking out the data.
6. The method for implementing local TCP Socket communication using shared memory according to claim 1, characterized in that: The key Socket communication functions obtained by implementing the Socket data transmission and reception of the sender and the receiver of the local process based on shared memory include a connection closing function close and a sending or receiving closing function shutdown. The connection closing function close closes the connection based on shared memory, and the sending or receiving closing function shutdown closes the sending or receiving based on shared memory. Both include: S501, determine whether the environment variable of the Socket communication is a specified value, if not, execute the corresponding pointer function and end the function, if yes, execute step S502; S502, determine whether the shared memory is created, if not, execute the corresponding pointer function and end the function, if yes, execute step S503; S503, releasing shared memory resources; S504, execute the corresponding pointer function and end the function.
7. A Socket communication system for implementing local TCP with shared memory, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the Socket communication method for implementing local TCP by sharing memory as described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program / instruction stored therein, characterized in that: The computer program / instruction is programmed or configured to execute the Socket communication method for implementing local TCP by sharing memory as described in any one of claims 1 to 6 through a processor.
9. A computer program product comprising a computer program / instructions, characterized in that The computer program / instruction is programmed or configured to execute the Socket communication method for implementing local TCP by sharing memory as described in any one of claims 1 to 6 through a processor.
