TCP 发送/接收错误报告

发布于 2024-08-15 06:15:57 字数 1391 浏览 4 评论 0原文

我有一个通过 TCP 进行通信的客户端/服务器程序(Windows、winsock2)。 客户端连接并向服务器发送数据(使用send),每次8K。 服务器只是读取数据(使用recv)。

没有对套接字进行特殊设置。

问题: 当通讯过程中出现网络错误(如电缆拔出)时,接收方此时无法收到成功发送的数据。 为了简化, 客户端在 10 次发送调用中发送了 80K 数据。 (所有发送均成功)。由于某些网络问题,第 11 次发送失败。此后不再发送数据。

问题是在接收器处,并未接收到所有 80K 数据。它始终小于 80K。

我希望发送方成功发送 80K 数据后,TCP 将保证将大量数据传送到目标 TCP(数据可能尚未被应用程序接收,但其在目标 TCP 缓冲区中)。

我错过了什么吗?

谢谢

编辑:

示例代码

服务器/接收者

/* create socket */
/* listen */
/* accept connection */
char recvbuf[8192];
do {

    iResult = recv(ClientSocket, recvbuf, sizeof(recvbuf), 0);
    if (iResult > 0) {
        total += iResult;
        printf("Total bytes received: %d\n", total);
    }
    else if (iResult == 0) {
        printf("Connection is closing...\n");
        break;
    }
    else  {
        printf("recv failed: %d\n", WSAGetLastError());
        break;
    }

} while (iResult > 0);

客户端/发送者:

/* create socket */
/* connect to server */
char sendbuf[8192];

do {
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, sizeof(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        break;
    }
    total += iResult;
    printf("Total bytes Sent: %ld\n", total);
} while(iResult > 0);
//wait before cleaning up
getc(stdin);

I have a client/server program (Windows, winsock2) which communicates over TCP.
The client connects and sends data to server (using send) in 8K at a time.
The server just reads data (using recv).

No special settings done on sockets.

Problem:
When some network error occurs during the communication (e.g. cable pulled out), receiver do not get data successfully sent by that time.
To simplify,
Client sent 80K data in 10 calls to send. (all sends are successful). 11th send failed because of some network issue. No more data sent after this.

Problem is that at receiver, not all 80K data is received. It is always less than 80K.

I expect as sender as successfully sent 80K, TCP will guarantee that much data is delivered to destination TCP (data may not be received by application yet, but its in destination TCP buffers).

Am I missing anything?

Thanks

Edit:

Sample code

Server/receiver

/* create socket */
/* listen */
/* accept connection */
char recvbuf[8192];
do {

    iResult = recv(ClientSocket, recvbuf, sizeof(recvbuf), 0);
    if (iResult > 0) {
        total += iResult;
        printf("Total bytes received: %d\n", total);
    }
    else if (iResult == 0) {
        printf("Connection is closing...\n");
        break;
    }
    else  {
        printf("recv failed: %d\n", WSAGetLastError());
        break;
    }

} while (iResult > 0);

Client/sender :

/* create socket */
/* connect to server */
char sendbuf[8192];

do {
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, sizeof(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        break;
    }
    total += iResult;
    printf("Total bytes Sent: %ld\n", total);
} while(iResult > 0);
//wait before cleaning up
getc(stdin);

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

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

发布评论

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

评论(2

哥,最终变帅啦 2024-08-22 06:15:57

你的假设是有缺陷的。

send() 成功返回仅意味着数据已被您的内核接受发送 - 它可能仍然位于您的发送队列中。

如果你想知道数据是否已经到达另一端,你必须在应用程序级别进行确认。

(另请参阅这个问题

Your assumption is flawed.

send() returning successfully just means that the data has been accepted to send by your kernel - it could still be sitting in your send queues.

If you want to know if the data has reached the other end, you must be application-level acknowledgements in.

(See also this question)

温馨耳语 2024-08-22 06:15:57

您是否在循环中调用 sendrecv ?他们不保证发送或接收您提供的所有数据或缓冲区。

TCP 连接似乎接收到不完整的数据

另外,您应该真正发布一些代码,否则我们只能猜测。

Are you calling send and recv in a loop? They do not guarantee that they send or receive all the data or buffer that you provide.

TCP Connection Seems to Receive Incomplete Data

Also you should really post some code, otherwise we can only guess.

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