Data transmission method and device, electronic equipment and storage medium
By using the exchange mechanism of the first flag and the second flag in Socket communication, the problem of data inconsistency in multi-machine communication is solved, reliable transmission and consistency of data are achieved, and the integrity of communication is ensured.
Patent Information
- Application Number
- CN202410694809.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-30
- Publication Date
- 2024-10-22
AI Technical Summary
In a multi-machine communication scenario, data inconsistency may occur during data transmission, affecting the integrity and accuracy of communication.
By introducing the exchange mechanism of the first flag and the second flag in Socket communication, the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, ensuring that the relationship between the sender and the receiver is Communication confirmation to achieve reliable transmission and consistency of data.
This method ensures the end-to-end integrity and reliability of data, reduces errors during data transmission, ensures data consistency, and provides a communication confirmation mechanism to avoid errors caused by inconsistent status.
Smart Images

Figure CN118827751A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a data transmission method, device, electronic equipment and storage medium. Background Art
[0002] In multi-machine communication scenarios, data transmission plays a vital role. With the rapid development of informatization, the demand for data exchange and sharing between different computers through the network is growing. In order to achieve this cross-platform and cross-regional data interaction, Socket communication, as a basic network communication method, is widely used in various application scenarios.
[0003] Socket communication allows two or more computers to establish a connection in the network and send and receive data through this connection. Through Socket communication, computers can efficiently transmit various types of data such as text, pictures, audio, video, etc., thereby supporting various complex network applications. However, in actual multi-computer communication scenarios, due to various factors, data inconsistency may occur during data transmission, thus affecting the integrity and accuracy of communication. Summary of the invention
[0004] The present invention provides a data transmission method, device, electronic equipment and storage medium, which are used to solve the problem of data inconsistency in the data transmission process.
[0005] The present invention provides a data transmission method, which is applied to a sender and includes: When it is determined that the data transmission is completed, a first flag is sent to the receiving party, and the sending party and the receiving party are connected via Socket communication; receiving a second flag, where the second flag is sent by the receiving party when the receiving party receives the first flag and determines that data reception is completed; Based on the first flag and the second flag, it is determined that the receiving party has completed data reception.
[0006] According to a data transmission method provided by the present invention, determining that the receiving party has completed data reception based on the first flag and the second flag includes: Compare the first flag with the second flag to obtain a comparison result; When the comparison result is inconsistent, it is determined that the receiving party has completed data reception.
[0007] According to a data transmission method provided by the present invention, the receiving a second flag includes: A receiving function is called, and the second flag is received based on the receiving function, where the second flag is sent by the receiving party calling a sending function.
[0008] According to a data transmission method provided by the present invention, when it is determined that the data transmission is completed, the first flag is sent to the receiving party, and the method also includes: Calling a sending function, and sending the data to be sent to the receiving party based on the sending function; The return value of the sending function is received, and when the return value is the same as the number of bytes of the data to be sent, it is determined that the data sending is completed.
[0009] According to a data transmission method provided by the present invention, the first flag is used to notify the receiving party that the data has been sent, and the second flag is used to notify the sending party that the data has been received. The first flag and the second flag both include specified fields, and the value of the specified field in the first flag is different from the value of the specified field in the second flag.
[0010] According to a data transmission method provided by the present invention, the sender is a client and the receiver is a server; or, The sender is the server, and the receiver is the client.
[0011] The present invention also provides a data transmission method, which is applied to a receiving party and comprises: When receiving a first flag sent by a sender and determining that data reception is completed, sending a second flag to the sender, so that the sender determines that the receiver has completed data reception based on the first flag and the second flag; The receiver and the sender are connected via Socket communication, and the first flag is sent by the sender when it is determined that data sending is completed.
[0012] According to a data transmission method provided by the present invention, the sending of the second flag to the sender includes: updating the value of the first flag, and using the updated first flag as the second flag; A sending function is called, and the second flag is sent to the sender based on the sending function. The sender receives the second flag based on calling a receiving function.
[0013] The present invention also provides a data transmission device, which is applied to a sender and includes: A sending unit, configured to send a first flag to a receiving party when determining that data sending is completed, wherein the sending party and the receiving party are connected via Socket communication; A receiving unit, configured to receive a second flag, where the second flag is sent by the receiving party when the receiving party receives the first flag and determines that data reception is completed; A determining unit is used to determine, based on the first flag and the second flag, that the receiving party has completed data reception.
[0014] The present invention also provides a data transmission device, which is applied to a receiving party and includes: a flag sending unit, configured to send a second flag to the sender when receiving a first flag sent by the sender and determining that data reception is completed, so that the sender determines that the receiver has completed data reception based on the first flag and the second flag; The receiver and the sender are connected via Socket communication, and the first flag is sent by the sender when it is determined that data sending is completed.
[0015] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, any of the above-mentioned data transmission methods is implemented.
[0016] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, any of the data transmission methods described above is implemented.
[0017] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned data transmission methods.
[0018] The data transmission method, device, electronic device and storage medium provided by the present invention can make the receiver know that the sender has completed data transmission by sending a first flag to the receiver when the sender determines that the data transmission is completed, and can make the sender judge whether the data has been successfully transmitted based on the first flag and the second flag by sending a second flag to the sender when the receiver receives the first flag and determines that the data reception is completed. The present invention realizes a communication confirmation mechanism between the sender and the receiver through the exchange of the first flag and the second flag. This mechanism can not only ensure the reliable transmission of data, but also greatly reduce errors in the data transmission process, thereby ensuring data consistency. In addition, since the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, the end-to-end data integrity from the sender to the receiver can be ensured. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the present invention or related technologies, the following briefly introduces the drawings required for use in the embodiments or related technical descriptions. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0020] Figure 1 It is a schematic diagram of the process of establishing a Socket communication connection provided by the present invention; Figure 2 It is one of the flowcharts of the data transmission method provided by the present invention; Figure 3 This is the second flow chart of the data transmission method provided by the present invention; Figure 4 It is one of the structural schematic diagrams of the data transmission device provided by the present invention; Figure 5 This is the second structural diagram of the data transmission device provided by the present invention; Figure 6 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0021] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0022] In the multi-machine communication scenario, Socket communication is an important way to transmit data in computer networks, allowing two or more computers to establish a connection and send and receive data. However, in practical applications, due to various factors, data transmission also faces some challenges and defects.
[0023] First, in multi-machine communication, the sender and receiver may be in different system environments. After sending data once, the sender may change the data in the memory due to other operations and continue to send new data. If the receiver requests to resend the data that was not successfully received last time, it may receive the data changed by the sender instead of the original data. This data inconsistency will cause the receiver to be unable to correctly understand the meaning of the data, thus causing errors.
[0024] Secondly, in multi-machine communication, due to network delays, packet loss or disorder, the receiver may not be able to receive the complete data packet at one time. If the receiver fails to successfully receive the previous data before the next data arrives, the new data may overwrite the old data, resulting in data errors. In this case, the loss or damage of data may affect the integrity and accuracy of the communication.
[0025] In this regard, the present invention provides a data transmission method, which adds a reverse notification function without changing the original communication process and principle of the Socket to ensure that the sender and the receiver know that the other party has completed the corresponding operation, so as to perform subsequent operations safely and ensure data consistency, thereby overcoming the above defects.
[0026] It should be noted that Socket communication is a network communication mechanism that allows different computers to exchange data through the network. The core of Socket communication is the socket, which is one end of the process communication in the network and provides a mechanism for the application layer process to exchange data using the network protocol. Socket communication can be regarded as the endpoints of the communication connection between two network applications. During communication, one of the network applications writes a piece of information to be transmitted into the socket of the host where it is located. The socket sends this piece of information to the socket of the host where the other network application is located through the transmission medium connected to the network interface card (i.e., network card), so that the other party can receive this piece of information. Here, the host refers to a device or system with independent processing and communication capabilities, which can execute programs, store data, and be used for communication with other devices or systems. For example, in a computer network, the host can be any device connected to the network, such as a personal computer, a server, a central processing unit, etc.
[0027] Figure 1 It is a schematic diagram of the process of establishing a Socket communication connection provided by the present invention. Figure 1As shown, the server will call the Socket() method on the computer where it is located (i.e., computer 1 in the figure) to create a Socket object, which allows computer 1 to receive and send data on the network. The server then calls the Bind() method to bind the Socket object to a specific IP address and port number so that the client can connect to this address and port. Next, the server calls the Listen() method to start listening for connection requests on the port. At this time, the Socket object on the server will enter a waiting state until a client initiates a connection request. At the same time, the client will also create a Socket object on its computer (i.e., computer 0 in the figure) and call the Connect() method to send a connection request to the server, trying to connect to the IP address and port number of the server. When the Socket object on the server listens to the client's connection request, it accepts the client's connection request by calling the Accept() method, and the connection is established.
[0028] After the connection is established, the server and client can exchange data. After that, both the server and the client can use the Send() method to send data and the Receive() method to receive data. During the communication process, the client can send data to the server, and the server processes the data after receiving it and sends the response data back to the client through the Socket connection. Similarly, the server can also actively send data to the client. Through Socket communication, two computers can achieve real-time, two-way data interaction on the network. It should be understood that the client refers to a device, program or system that requests a service or resource, and the server refers to a device, program or system that provides resources or services. In the client-server architecture, the server is the host that provides services and resources, and the client is the host that requests these services and resources. The client sends a request to the server and waits to receive a response from the server; the server receives the request from the client and returns an appropriate response.
[0029] Figure 2 It is one of the flowcharts of the data transmission method provided by the present invention, such as Figure 2 As shown, the method is applied to the sender, and the method includes: Step 210, when it is determined that the data transmission is completed, a first flag is sent to the receiving party, and the sending party and the receiving party are connected via Socket communication; It should be noted that the data transmission method provided in the embodiment of the present invention can be applied to the sender of data, where the sender refers to the device, program or system responsible for sending data. The receiver refers to the device, program or system responsible for receiving data. In the client-server architecture, the client is usually the sender because it sends requests to the server, and the server is usually the receiver because it receives requests from the client; but when the server responds to the client request, the server becomes the sender, and similarly, when the client receives the server's response, the client is the receiver.
[0030] Specifically, after the sender has sent all data to the receiver through Send(), it can send a specific message to the receiver through the Socket connection as the first flag. Here, the first flag can be a simple byte, data packet, or a message in a specific format, which is used to notify the receiver that the data has been sent. For example, the first flag can be a message that contains a specified field (such as a flag field) whose value is true. It should be understood that the sender can use the Send() method of Socket to send the first flag.
[0031] Before executing step 210, the sender can determine whether the data sending is completed by the following steps: the sender usually sends data through the Send() method of Socket. After sending the data, the method returns a value indicating the number of bytes actually sent. By checking the return value of the Send() method, the sender can determine the actual amount of data sent, thereby determining whether the data has been sent.
[0032] In the embodiment of the present invention, by sending the first flag, the sender can ensure that the receiver knows that the data has been sent, thereby synchronizing the communication status of the two parties, which helps to avoid communication errors or data loss caused by inconsistent status of the two parties. After receiving the first flag, the receiver can trigger corresponding processing logic, such as starting to parse or process the received data, etc., thereby helping to ensure the timeliness and accuracy of the data.
[0033] Step 220, receiving a second flag, where the second flag is sent by the receiving party when the receiving party receives the first flag and determines that data reception is completed; Specifically, when the receiver receives the first flag sent by the sender through Receive(), the receiver knows that the sender has completed data transmission. At the same time, after receiving all data through Receive(), the receiver can send a specific message to the sender through the Socket connection as the second flag. Here, the second flag refers to a message sent by the receiver to the sender after successfully receiving all data sent by the sender. For example, the second flag may also include a specified field (such as a flag field), but unlike the first flag, the value of the flag field is false. The main function of the second flag is to inform the sender that the receiver has successfully received all data and can perform subsequent operations. It should be understood that the receiver can also use the Send() method of Socket to send the second flag.
[0034] In addition, the sender can receive data by continuously monitoring the Socket connection established with the receiver. When the receiver sends the second flag, the sender can call the Receive() method of the Socket to read the second flag from the Socket connection. Once the second flag is read, the sender can confirm that the receiver has successfully received all the data sent this time.
[0035] It is understandable that the sender will send the first flag after sending all the data, which clearly demarcates the end point of data transmission. After receiving the first flag, the receiver can start to check whether it has completely received all the expected data. This check may include calculating the checksum, checking whether the sequence number of the data packet is continuous and not missing, etc. Only when the receiver confirms that the data is received completely and correctly will it send the second flag to the sender. This mechanism ensures that the receiver has verified the integrity and accuracy of the data before the sender receives the second flag.
[0036] In the embodiment of the present invention, by sending the second flag to the sender by the receiving side, the sender can be notified that the data has been completely received, which helps to ensure the reliability of communication and the consistency of data. In the Socket communication based on TCP / IP, although the TCP protocol itself provides reliable transmission of data packets, the use of the second flag can further confirm the integrity and reception status of the data at the application layer.
[0037] Step 230: Determine, based on the first flag and the second flag, whether the receiving party has completed data reception.
[0038] Specifically, the sender can determine that the receiver has completed data reception based on the interaction of the first flag and the second flag. Specifically, when the sender determines that all data has been sent to the receiver through the Socket connection, it will send the first flag to the receiver, which is used to notify the receiver that data transmission has been completed; after receiving the data, the receiver will continue to perform the Receive() operation to ensure that all data has been completely received. Once all data has been received, the receiver will send the second flag to the sender; after sending the first flag, the sender will continue to monitor the Socket connection to receive the second flag. When the sender receives the second flag, it can determine that the receiver has completed data reception.
[0039] It is understandable that the sender sends the first flag after the data is sent, which is a clear confirmation message indicating that the sender has completed its sending task. Similarly, the receiver sends the second flag after confirming that the data is received completely, which is also a clear confirmation message indicating that the receiver has successfully received all the data. This two-way synchronous confirmation mechanism can ensure that the sender and the receiver have a common understanding of the data transmission status, thereby avoiding data errors or losses caused by inconsistent status between the two parties.
[0040] Furthermore, after receiving the second flag, the sender can confirm that the receiver has successfully received and verified the data. In this case, the sender can perform subsequent operations, such as sending the next data packet, closing the connection, or performing other tasks. In addition, if errors occur during data transmission (such as loss, damage, or disorder), the receiver will find these problems when verifying the data and will not send the second flag. After the sender times out and does not receive the second flag, it will realize that there is a problem with the data transmission and can trigger a retransmission mechanism or other error handling measures, such as resending data, disconnecting, or recording an error log. Similarly, if the receiver does not receive the first flag, it can also take corresponding error handling measures.
[0041] The method provided by the embodiment of the present invention can make the receiver know that the sender has completed data transmission by sending a first flag to the receiver when the sender determines that data transmission is completed, and can make the sender judge whether the data has been successfully transmitted based on the first flag and the second flag by sending a second flag to the sender when the receiver receives the first flag and determines that data reception is completed. The present invention implements a communication confirmation mechanism between the sender and the receiver through the exchange of the first flag and the second flag, which can not only ensure the reliable transmission of data, but also greatly reduce errors in the data transmission process, thereby ensuring data consistency. In addition, since the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, the end-to-end data integrity from the sender to the receiver can be ensured.
[0042] Based on the above embodiment, step 230 specifically includes: Step 231, comparing the first mark with the second mark to obtain a comparison result; Step 232: if the comparison result is inconsistent, determine that the receiving party has completed data reception.
[0043] It should be noted that when the sender determines whether the receiver has completed data reception based on the first flag and the second flag, it can compare the first flag with the second flag to confirm the status of data transmission and reception and ensure that both parties of the communication (sender and receiver) have reached the expected synchronization point.
[0044] Specifically, the sender sends data to the receiver through Send(), and after all data is sent, it sends a message with a flag value of true (i.e., the first flag) to the receiver through Send() to inform the receiver that the data sending is completed. The receiver continues to receive data through Receive(), and after receiving the first flag and all data is received, it can set the flag to false (or other status indicating completion), obtain the second flag, and send it to the sender through Send(). After sending the first flag (flag with a value of true), the sender waits for the receiver's response through Receive(). When the sender receives the second flag (flag with a value of false), it can compare it with the first flag it sent before. If the comparison results are inconsistent, it can be determined that the receiver has completed the reception of all previous data, so that subsequent operations can continue.
[0045] It is understandable that, although the values of the first flag and the second flag (i.e., the values of the two flags) are not logically the same and do not represent the same state (one represents "sending completed" and the other represents "receiving completed"), by comparing the first flag with the second flag, the state transition during the communication process can be confirmed. The "inconsistency" between the first flag and the second flag is actually an expected and normal state transition during the communication process, which ensures that both communicating parties can accurately know the state of the other party and perform subsequent operations accordingly.
[0046] Based on any of the above embodiments, step 220 specifically includes: A receiving function is called, and the second flag is received based on the receiving function, where the second flag is sent by the receiving party calling a sending function.
[0047] It should be noted that a receive function refers to a function or method in a program that is used to receive data from a communication channel. In Socket programming, a receive function refers to a specific function or method, such as Receive(). A send function refers to a function or method in a program that is used to send data to a communication channel. In Socket programming, a send function is also a specific function or method, such as Send().
[0048] Specifically, since the receiver receives data through Receive(), when the receiver receives the first flag and determines that the data has been received, in order to send the second flag to the sender, the sending function Send() can be called and the second flag can be passed to the function as a parameter. The sending function will then send the second flag to the communication channel established previously so that the sender can receive it.
[0049] The sender sends data to the receiver through Send(). After sending the first flag to the receiver through Send(), the receiving function Receive() can be called to wait for and receive the second flag from the receiver.
[0050] In the embodiment of the present invention, by adding a receiving function Receive() and a sending function Send() to the original Send() and Receive() operations of the Socket, the reverse notification function can be implemented to ensure that the sender and the receiver can know that the other party has completed the corresponding operation, so as to safely perform the next operation and ensure the reliability of the data transmission process.
[0051] Based on any of the above embodiments, before step 210, the method further includes: Calling a sending function, and sending the data to be sent to the receiving party based on the sending function; The return value of the sending function is received, and when the return value is the same as the number of bytes of the data to be sent, it is determined that the data sending is completed.
[0052] It should be noted that the above-mentioned sending function refers to Send() in the Socket connection, which can put data into the sending buffer and call the underlying network or communication library to actually send the data. The function can return a value to indicate the number of bytes actually sent. The above-mentioned data to be sent refers to the information or data that the sender needs to send to the receiver through the sending function this time, which can be text, images, videos, binary data, etc. The number of bytes of the data to be sent refers to the size or length of the data after it is converted into a byte stream. In most cases, data needs to be serialized into bytes before transmission because bytes are the basic unit in network communication.
[0053] Specifically, when the sender sends the data to be sent to the receiver, it can be achieved by calling Send(). Send() will return the number of bytes actually sent after sending the data. By comparing the number of bytes actually sent with the number of bytes of the data to be sent, if the two are the same, it can be determined that the data sending is completed.
[0054] It is understandable that when the sender sends data through a Socket connection, the data will be written into the send buffer. If the amount of data to be sent is less than or equal to the buffer length, Send() will usually return successfully. However, if the amount of data to be sent is large and the network condition is poor, Send() may need to be called multiple times to complete the sending of all data. In this case, the return value of Send() may not be able to accurately determine whether the data has been sent, because the return value only indicates the number of bytes actually sent in this call, and does not mean that all data has been successfully sent.
[0055] When sending a large amount of data, if the number of bytes returned by Send() is less than the number of bytes of the data to be sent, it indicates that the send buffer is full and it is necessary to wait for a period of time before continuing to send the remaining data. In this regard, the sender can determine whether the data has been sent by calling the Send() function in a loop and combining other methods. For example, the sender can maintain a counter to record the number of bytes that have been successfully sent. After each call to the Send() function, the number of bytes actually sent is added to the counter until the counter is equal to the total amount of data to be sent, indicating that all the data has been sent. For another example, after sending the data, the size of the send buffer can be checked. When the data in the send buffer is successfully transmitted to the network layer or the transport layer (depending on the specific implementation and protocol stack), it can be checked that the size of the send buffer is 0, and the data can be determined to be sent. In addition, in some cases, the application layer protocol can define a specific data packet format, including data length information or terminator to identify the data boundary, so that the sender can determine that the data to be sent has been sent based on this information.
[0056] Based on any of the above embodiments, the first flag is used to notify the receiving party that the data has been sent, and the second flag is used to notify the sending party that the data has been received. The first flag and the second flag both include specified fields, and the value of the specified field in the first flag is not the same as the value of the specified field in the second flag.
[0057] Specifically, when the sender completes the task of sending data, it will send a special flag to the receiver. This flag is the first flag. The first flag is used to tell the receiver that "the sender has sent all the data". The flag may include a specified field, and the value of the field is used to further describe or distinguish different states or situations. For example, the first flag may include a flag field, and the value of the field is true.
[0058] When the receiver successfully receives all the data, it will send a different flag to the sender. This flag is the second flag. The second flag tells the sender that "the receiver has successfully received all the data sent." The second flag may also include a specified field, which may be the same as the specified field in the first flag, but the values of the two are different. For example, the second flag may also include a flag field, but the value of the field is false.
[0059] When the sender receives the second flag, by comparing the first flag and the second flag, it can detect the change of the value of the flag field from true to false. Based on this change, the sender can determine that the reverse notification is over. It should be understood that the above-mentioned reverse notification refers to the receiver sending a message to the sender indicating that the data has been received. The usage scenario of the reverse notification is the last time the receiver and the sender communicated (for a data sending task). In the scenario where the reverse notification is not used (that is, the scenario of normal data sending and receiving), the sender will not set the value of the flag field to true. In this case, the value of the flag field defaults to false, and the sender will not detect the change in the value of the flag field. Therefore, by detecting whether the value of the flag field has changed, it can be identified whether the reverse notification is over, thereby ensuring the accuracy and reliability of communication and reducing the possibility of misunderstanding or confusion.
[0060] Based on any of the above embodiments, the sender is a client, and the receiver is a server; or, The sender is the server, and the receiver is the client.
[0061] Specifically, in Socket communication, the client creates a Socket object and uses it to send data to the server and receive responses from the server; the server listens to a specific port, waits for the client's connection request, and processes the data from the client after the connection is established. In the client-server architecture, the roles of the sender and receiver are relative. The sender can be the client or the server, and the receiver can be the server or the client, depending on the direction of data flow.
[0062] For example, if the client sends data to the server (such as HTTP request, file upload, etc.), then in this specific communication process, the client is the sender and the server is the receiver. If the server sends data to the client (such as HTTP response, file download, etc.), then in this specific communication process, the server is the sender and the client is the receiver.
[0063] Based on any of the above embodiments, Figure 3 FIG. 2 is a flow chart of the data transmission method provided by the present invention. Figure 3 As shown, the method is applied to a receiving party, and the method includes: Step 310, when receiving the first flag sent by the sender and determining that data reception is completed, sending a second flag to the sender, so that the sender determines that the receiver has completed data reception based on the first flag and the second flag; The receiver and the sender are connected via Socket communication, and the first flag is sent by the sender when it is determined that data sending is completed.
[0064] It should be noted that the data transmission method provided in the embodiment of the present invention can be applied to the receiver of the data, where the receiver refers to the device, program or system responsible for receiving the data. The sender refers to the device, program or system responsible for sending the data. In the client-server architecture, due to the bidirectional nature of network communication, both the client and the server can be both the sender and the receiver, depending on their current ongoing communication activities.
[0065] Specifically, after the sender has sent all data to the receiver through Send(), it can send a specific message to the receiver through the Socket connection as the first mark. When the receiver receives the first mark sent by the sender through Receive(), the receiver can know that the sender has completed data sending. At the same time, after the receiver has received all data through Receive(), it can send a specific message to the sender through the Socket connection as the second mark to inform the sender that the data has been correctly received. After sending the first mark through Send(), the sender can call Receive() to wait for the second mark sent by the receiver. When it receives the second mark, it can compare the second mark with the first mark, and determine based on the comparison result that the receiver has completed all data reception this time, and can perform subsequent operations.
[0066] It is understandable that before executing step 310, the receiving party can determine that data reception is complete by the following steps: the receiving party reads data from the Socket connection by continuously calling Receive() until there is no more data to read or a special identifier indicating the end of the data is received, such as length information, a specific end character, or the last packet in a data packet sequence, etc., to determine that data reception is complete.
[0067] Furthermore, after receiving the first flag, the receiver can also start to check whether it has completely received all the expected data. This check may include calculating the checksum, checking whether the sequence number of the data packet is continuous and not missing, etc. Only when the receiver confirms that the data is received completely and correctly will it send the second flag to the sender. After receiving the second flag, the sender can confirm that the receiver has successfully received and verified the data. In this case, the sender can perform subsequent operations, such as sending the next data packet, closing the connection, or performing other tasks.
[0068] In addition, if errors occur during data transmission (such as loss, damage, or disorder), the receiver will discover these problems when checking the data and will not send the second flag. After the sender fails to receive the second flag after the timeout, it will realize that there is a problem with the data transmission and can trigger a retransmission mechanism or other error handling measures, such as resending data, disconnecting, or recording an error log. Similarly, if the receiver does not receive the first flag, it can also take corresponding error handling measures.
[0069] The method provided by the embodiment of the present invention can make the receiver know that the sender has completed data transmission by sending a first flag to the receiver when the sender determines that data transmission is completed, and can make the sender judge whether the data has been successfully transmitted based on the first flag and the second flag by sending a second flag to the sender when the receiver receives the first flag and determines that data reception is completed. The present invention implements a communication confirmation mechanism between the sender and the receiver through the exchange of the first flag and the second flag, which can not only ensure the reliable transmission of data, but also greatly reduce errors in the data transmission process, thereby ensuring data consistency. In addition, since the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, the end-to-end data integrity from the sender to the receiver can be ensured.
[0070] Based on any of the foregoing embodiments, in step 310, sending the second flag to the sender includes: Step 311, update the value of the first flag, and use the updated first flag as the second flag; Step 312, call the sending function, and send the second flag to the sender based on the sending function, and the sender receives the second flag based on calling the receiving function.
[0071] Specifically, after receiving the first flag through Receive(), the receiver can update the value of the first flag to obtain the second flag. For example, the first flag can include a flag field whose value is true. After reading the value of the first flag, the receiver can modify it, change the value of the flag field to false, and send the flag field with a value of false as the second flag to the sender.
[0072] It is understandable that the receiving party can send the second flag to the sending party by calling the sending function Send(). After sending the first flag through Send(), the sending party can call the receiving function Receive() to wait for and receive the second flag from the receiving party.
[0073] In the embodiment of the present invention, by updating the value of the first flag and using the updated first flag as the second flag, the sender can confirm the state transition in the communication process by comparing the first flag and the second flag, and perform subsequent operations accordingly.
[0074] Based on any of the above embodiments, an embodiment of the present invention provides a data transmission method based on multi-machine Socket communication, the method comprising: Step S1, the sender and the receiver establish a Socket communication connection; Step S2, when the sender has finished sending the data, it sends a message with flag set to true to the receiver through Send() to inform the receiver that the data sending is completed; Step S3, after the receiver receives the message with flag set to true through Receive(), if it detects that the data reception is completed, it can change the flag value to false and call Send() to send a message with flag set to false to the sender to tell the sender that the data has been received correctly; Step S4, after the sender sends a message with a flag of true through Send(), it can call Receive() to wait for the flag sent by the receiver; Step S5, when the sender receives a flag with a value of false, it can compare it with the flag with a value of true sent previously. If the comparison is inconsistent, it is determined that the receiver has completed all data reception this time and can perform subsequent operations.
[0075] The method provided by the embodiment of the present invention can add a reverse notification function without changing the original communication process and principle of the Socket by adding the call of the receiving function Receive() and the sending function Send() in the original Send() and Receive() operations of the Socket, thereby ensuring that the sender and the receiver know that the other party has completed the corresponding operation, so as to safely perform subsequent operations, thereby ensuring data consistency. In addition, the user can also decide whether to enable the reverse notification function provided by the embodiment of the present invention according to the requirements of the actual application scenario (such as multi-machine synchronous communication).
[0076] Based on any of the above embodiments, Figure 4 is one of the structural diagrams of the data transmission device provided by the present invention, such as Figure 4 As shown, the device is applied to a sender, and the device includes: The sending unit 410 is used to send a first flag to a receiving party when it is determined that the data sending is completed, and the sending party and the receiving party are connected through Socket communication; The receiving unit 420 is configured to receive a second flag, where the second flag is sent by the receiving party after receiving the first flag and determining that data reception is completed; The determining unit 430 is configured to determine, based on the first flag and the second flag, whether the receiving party has completed data reception.
[0077] The device provided by the embodiment of the present invention can make the receiver know that the sender has completed data transmission by sending a first flag to the receiver when the sender determines that data transmission is completed, and can make the sender judge whether the data has been successfully transmitted based on the first flag and the second flag by sending a second flag to the sender when the receiver receives the first flag and determines that data reception is completed. The present invention implements a communication confirmation mechanism between the sender and the receiver through the exchange of the first flag and the second flag, which can not only ensure the reliable transmission of data, but also greatly reduce errors in the data transmission process, thereby ensuring data consistency. In addition, since the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, the end-to-end data integrity from the sender to the receiver can be ensured.
[0078] Based on any of the above embodiments, the determining unit 430 is specifically configured to: Compare the first flag with the second flag to obtain a comparison result; When the comparison result is inconsistent, it is determined that the receiving party has completed data reception.
[0079] Based on any of the foregoing embodiments, the receiving unit 420 is specifically configured to: A receiving function is called, and the second flag is received based on the receiving function, where the second flag is sent by the receiving party calling a sending function.
[0080] Based on any of the above embodiments, the device further includes a data sending unit, and the data sending unit is used to: Calling a sending function, and sending the data to be sent to the receiving party based on the sending function; The return value of the sending function is received, and when the return value is the same as the number of bytes of the data to be sent, it is determined that the data sending is completed.
[0081] Based on any of the above embodiments, the first flag is used to notify the receiving party that the data has been sent, and the second flag is used to notify the sending party that the data has been received. The first flag and the second flag both include specified fields, and the value of the specified field in the first flag is not the same as the value of the specified field in the second flag.
[0082] Based on any of the above embodiments, the sender is a client and the receiver is a server; or, the sender is a server and the receiver is a client.
[0083] Based on any of the above embodiments, Figure 5 This is a second structural diagram of the data transmission device provided by the present invention, such as Figure 5 As shown, the device is applied to a receiving side, and the device includes: The flag sending unit 510 is used to send a second flag to the sender when receiving the first flag sent by the sender and determining that the data reception is completed, so that the sender determines that the receiver has completed the data reception based on the first flag and the second flag; The receiver and the sender are connected via Socket communication, and the first flag is sent by the sender when it is determined that data sending is completed.
[0084] The device provided by the embodiment of the present invention can make the receiver know that the sender has completed data transmission by sending a first flag to the receiver when the sender determines that data transmission is completed, and can make the sender judge whether the data has been successfully transmitted based on the first flag and the second flag by sending a second flag to the sender when the receiver receives the first flag and determines that data reception is completed. The present invention implements a communication confirmation mechanism between the sender and the receiver through the exchange of the first flag and the second flag, which can not only ensure the reliable transmission of data, but also greatly reduce errors in the data transmission process, thereby ensuring data consistency. In addition, since the sender sends the first flag after the data transmission is completed, and the receiver sends the second flag after confirming that the data reception is completed, the end-to-end data integrity from the sender to the receiver can be ensured.
[0085] Based on any of the above embodiments, the flag sending unit 510 is specifically used for: updating the value of the first flag, and using the updated first flag as the second flag; A sending function is called, and the second flag is sent to the sender based on the sending function. The sender receives the second flag based on calling a receiving function.
[0086] Based on any of the above embodiments, the first flag is used to notify the receiving party that the data has been sent, and the second flag is used to notify the sending party that the data has been received. The first flag and the second flag both include specified fields, and the value of the specified field in the first flag is not the same as the value of the specified field in the second flag.
[0087] Based on any of the above embodiments, the sender is a client and the receiver is a server; or, the sender is a server and the receiver is a client.
[0088] Figure 6 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 6 As shown, the electronic device may include: a processor 610, a communication interface 620, a memory 630 and a communication bus 640, wherein the processor 610, the communication interface 620 and the memory 630 complete mutual communication through the communication bus 640. The processor 610 may call the logic instructions in the memory 630 to execute the data transmission method, which is applied to the sender, and the method includes: when it is determined that the data transmission is completed, sending a first flag to the receiver, the sender and the receiver are connected through Socket communication; receiving a second flag, the second flag is sent by the receiver when the first flag is received and the data reception is determined to be completed; based on the first flag and the second flag, determining that the receiver has completed data reception.
[0089] In addition, the processor 610 can also call the logic instructions in the memory 630 to execute the data transmission method, which is applied to the receiving party, and the method includes: when receiving the first flag sent by the sender and determining that the data reception is completed, sending a second flag to the sender, so that the sender determines that the receiving party has completed the data reception based on the first flag and the second flag; the receiving party and the sender are connected via Socket communication, and the first flag is sent by the sender when determining that the data sending is completed.
[0090] In addition, the logic instructions in the above-mentioned memory 630 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the relevant technology or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0091] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the data transmission method provided by the above-mentioned methods. The method is applied to the sender, and the method includes: when it is determined that the data sending is completed, sending a first flag to the receiver, the sender and the receiver are connected via Socket communication; receiving a second flag, the second flag is sent by the receiver when the first flag is received and the data reception is determined to be completed; based on the first flag and the second flag, determining that the receiver has completed data reception.
[0092] In addition, when the computer program is executed by the processor, the computer can also execute the data transmission method provided by the above methods, which is applied to the receiving party, and the method includes: when receiving the first flag sent by the sender and determining that the data reception is completed, sending a second flag to the sender, so that the sender determines that the receiving party has completed data reception based on the first flag and the second flag; the receiving party and the sender are connected via Socket communication, and the first flag is sent by the sender when determining that the data sending is completed.
[0093] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, it is implemented to execute the data transmission method provided by the above-mentioned methods. The method is applied to the sender, and the method includes: when it is determined that the data sending is completed, sending a first flag to the receiver, the sender and the receiver are connected via Socket communication; receiving a second flag, the second flag is sent by the receiver when the first flag is received and the data reception is determined to be completed; based on the first flag and the second flag, determining that the receiver has completed data reception.
[0094] In addition, when the computer program is executed by the processor, it is implemented to execute the data transmission method provided by the above-mentioned methods. The method is applied to the receiving party, and the method includes: when receiving the first flag sent by the sender and determining that the data reception is completed, sending a second flag to the sender, so that the sender determines that the receiving party has completed data reception based on the first flag and the second flag; the receiving party and the sender are connected via Socket communication, and the first flag is sent by the sender when determining that the data sending is completed.
[0095] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.
[0096] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution can be essentially or in other words, the part that contributes to the relevant technology can be embodied in the form of a software product, which can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment.
[0097] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data transmission method, characterized in that: The method is applied to a sender, and comprises: When it is determined that the data transmission is completed, a first flag is sent to the receiving party, and the sending party and the receiving party are connected via Socket communication; receiving a second flag, where the second flag is sent by the receiving party when the receiving party receives the first flag and determines that data reception is completed; Based on the first flag and the second flag, it is determined that the receiving party has completed data reception.
2. The data transmission method according to claim 1, characterized in that: The determining, based on the first flag and the second flag, that the receiving party has completed data reception includes: Compare the first flag with the second flag to obtain a comparison result; When the comparison result is inconsistent, it is determined that the receiving party has completed data reception.
3. The data transmission method according to claim 1, characterized in that: The receiving of the second flag comprises: A receiving function is called, and the second flag is received based on the receiving function, where the second flag is sent by the receiving party calling a sending function.
4. The data transmission method according to claim 1, characterized in that: The step of sending a first flag to a receiving party when determining that the data transmission is completed also includes: Calling a sending function, and sending the data to be sent to the receiving party based on the sending function; The return value of the sending function is received, and when the return value is the same as the number of bytes of the data to be sent, it is determined that the data sending is completed.
5. The data transmission method according to any one of claims 1 to 4, characterized in that: The first flag is used to notify the receiving party that the data has been sent, and the second flag is used to notify the sending party that the data has been received. The first flag and the second flag both include specified fields, and the value of the specified field in the first flag is different from the value of the specified field in the second flag.
6. The data transmission method according to any one of claims 1 to 4, characterized in that: The sender is the client, and the receiver is the server; or, The sender is the server, and the receiver is the client.
7. A data transmission method, characterized in that: The method is applied to a receiving party, and the method comprises: When a first flag sent by a sender is received and it is determined that data reception is completed, a second flag is sent to the sender, so that the sender determines that the receiver has completed data reception based on the first flag and the second flag; the receiver and the sender are connected via Socket communication, and the first flag is sent by the sender when it is determined that data sending is completed.
8. The data transmission method according to claim 7, characterized in that: The sending of the second flag to the sender comprises: updating the value of the first flag, and using the updated first flag as the second flag; A sending function is called, and the second flag is sent to the sender based on the sending function. The sender receives the second flag based on calling a receiving function.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the data transmission method according to any one of claims 1 to 8 is implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the data transmission method according to any one of claims 1 to 8 is implemented.
