在C中正确发送和接收缓冲区

发布于 2025-02-08 07:00:00 字数 1368 浏览 1 评论 0原文

我正在C Windows中编写一些服务器/客户端程序。我不知道我是否以正确的方式发送和接收缓冲区,在Google上,我只会看到人们错误检查它,但没有检查send()函数是否较少发送字节,然后预期。这是我项目的一个示例:

客户端:

// send buffer size
uint32_t num = htonl(sizeBuffer);
char* converted_num = (char*)#
res = send(ClientSocket, converted_num, sizeof(uint32_t), 0); 
if (res == SOCKET_ERROR)
{
    printf("error send\n");
}

// send buffer
while (totalSent < sizeBuffer)
{
    sent = send(ClientSocket, totalBuffer, sizeBuffer, 0);
    totalSent += sent;
    printf("sent: %d\n", sent);
    printf("totalSent: %d\n", totalSent);
}

服务器:

// recv buffer size
char b[sizeof(uint32_t)];
r = recv(s, b, sizeof(uint32_t), 0);
if (r == SOCKET_ERROR)
{
    printf("error recv\n");
}
uint32_t sizeBuffer = ntohl_ch(&b[0]);

// recv buffer
while (totalReceived < sizeBuffer)
{
    received = recv(s, buffer, sizeBuffer, 0);
    strcat(totalBuffer, buffer);
    bzero(buffer, 18384);
    totalReceived += received;
    printf("received: %d\n", received);
    printf("totalReceived: %d\n", totalReceived);
}
printf("%s", totalBuffer);

我使用strcat()的原因是因为当我在printf()中使用while while while() /code>循环它被奇怪地打印出来,就像先前的缓冲区被打印,新的缓冲区在顶部打印出来。我不知道为什么它是这样的。

这是发送和接收缓冲区的正确方法吗?我还必须检查缓冲区的大小(num)是否正确发送,例如我发送缓冲区本身的方式?如果是,我该怎么做?

I'm writing some server/client program in C Windows. I don't know if I'm sending and receiving buffers the right way, on google I only see people error checking it but not checking if the send() function sent less bytes then expected. This is an example from my project:

Client:

// send buffer size
uint32_t num = htonl(sizeBuffer);
char* converted_num = (char*)#
res = send(ClientSocket, converted_num, sizeof(uint32_t), 0); 
if (res == SOCKET_ERROR)
{
    printf("error send\n");
}

// send buffer
while (totalSent < sizeBuffer)
{
    sent = send(ClientSocket, totalBuffer, sizeBuffer, 0);
    totalSent += sent;
    printf("sent: %d\n", sent);
    printf("totalSent: %d\n", totalSent);
}

Server:

// recv buffer size
char b[sizeof(uint32_t)];
r = recv(s, b, sizeof(uint32_t), 0);
if (r == SOCKET_ERROR)
{
    printf("error recv\n");
}
uint32_t sizeBuffer = ntohl_ch(&b[0]);

// recv buffer
while (totalReceived < sizeBuffer)
{
    received = recv(s, buffer, sizeBuffer, 0);
    strcat(totalBuffer, buffer);
    bzero(buffer, 18384);
    totalReceived += received;
    printf("received: %d\n", received);
    printf("totalReceived: %d\n", totalReceived);
}
printf("%s", totalBuffer);

The reason I use strcat() is because when I use printf() inside the while() loop it gets printed weirdly, like the previous buffer gets printed and the new buffer gets printed on top. I don't know why it behaves like this.

Is this the right way to send and receive buffers? And do I also have to check whether the size (num) of the buffer is send correctly, like how I send the buffer itself? If yes, how can I do that?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文