通过C丢失数据,通过TCP Soket发送多个字符串

发布于 2025-02-06 09:14:30 字数 1283 浏览 3 评论 0原文

我正在研究一个C项目,该项目实现了TCP客户端服务器。我正在使用的插座和send()函数是库中定义的sys/socket.hwinsock2.h

我的问题是,当我尝试一个接一个地发送多个字符串时,有些消息没有正确传输,其中一些数据(有时是所有消息)丢失。例如,当我在同一台计算机上运行服务器和客户端时,以下代码无问题,但是如果我尝试使用远程服务器运行它,则第三条消息将无法正确接收。

客户端

    char message[1024];  
    memset(message, 0, 1024);  
    fill_message(message, msg1); //A function that prints something in the message string. 
                                 //It may fill less than 1024 characters.
    send(clientSocket, message, 1024,0);
    
    fill_message(message, msg2);
    send(clientSocket, message, 1024,0);
    
    fill_message(message, msg3);
    send(clientSocket, message, 1024,0);

服务器端

    char message[1024];
    memset(message, 0, 1024);
    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

注意:字符串消息可能不完全是长度1024。

我的解决方案是通过调用sleep(1)在每个消息都发送。这是解决问题的正确方法吗?或者,我是否缺少有关send()recv()工作的情况?

总体而言:使用插座编程的“正确”方法是什么?我是否应该发送消息字节逐字节并将长度指定为第一件事?如果有人可以将我指向使用插座时最好的做法的好教程/指南,我很乐意阅读它。

I'm working on a C project that implements a TCP client-server. The sockets and the send() functions i'm using are the one defined in the libraries sys/socket.h and winsock2.h.

My problem is that when i try to send multiple strings one after the other, some messages aren't transmitted correctly, with some data (sometimes all the message) that goes missing. The following code, for example, works without a problem when i'm running server and client on the same machine, but if I try to run it with a remote server, then the third message isn't properly received.

Client Side

    char message[1024];  
    memset(message, 0, 1024);  
    fill_message(message, msg1); //A function that prints something in the message string. 
                                 //It may fill less than 1024 characters.
    send(clientSocket, message, 1024,0);
    
    fill_message(message, msg2);
    send(clientSocket, message, 1024,0);
    
    fill_message(message, msg3);
    send(clientSocket, message, 1024,0);

Server Side

    char message[1024];
    memset(message, 0, 1024);
    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

    recv(clientSocket, message, 1024,0);
    print_and_do_stuff(message);

Note: the string message may not be exactly of length 1024.

My solution has been to make the client wait for 1 second by calling sleep(1) after each message is sent. Is this the proper way to address the issue? Or am i missing something about how send() and recv() work?

More in general: what is the "proper" way to program with sockets? Should I maybe be sending the message byte-by-byte and specifying the length as the first thing? If someone could point me toward a good tutorial/guide on what the best practices are when working with sockets, I'd be happy to read it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凹づ凸ル 2025-02-13 09:14:30

套接字功能可能会或可能不会在一个呼叫中读取/发送整个数据,这意味着您必须验证正确的接收服务器端,并且可以在TCP之上创建自定义协议,以跟踪您发送和接收的大小。
与UDP相反的TCP保证了数据的完整性,这意味着您在发送时不会丢失任何东西,但是您可能需要使用多个功能调用来确保已发送所有数据并红色。
至于好的教程和指南,正如某人在评论中所说的那样,您可以找到大量的例子和指南。

Socket functions may or may not read/send the entire data in one call, which means that you have to verify the correct reception server side, and maybe create a custom protocol on top of TCP to keep track of the size you sent and received.
TCP, contrary to UDP, guarantees the integrity of data, meaning that you won't lose anything when sending, but you may need to use multiple function calls to ensure all of the data has been sent and red.
As for good tutorial and guides, as someone already said in comments, you can find loads of examples and guides about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文