客户端/服务器:接收的整数始终为 1(C 编程)

发布于 2024-08-11 11:26:35 字数 1227 浏览 8 评论 0原文

我正在构建一个通过 TCP 交换数据的客户端和服务器程序,当操作成功时,我无法将 ACK 确认从服务器发送回客户端。我已经成功地将一个包含各种成员的结构从客户端发送到服务器,然后服务器应该通过发送一个确认结构 ID 的整数来响应客户端。

在 server.c 中,我有这个函数:

int sendACK(int socket, int ack_int){

    int com_result;
    int ACK = htonl(ack_int);

    printf("\n\tSending ACK with value: %i", ack_int);

    com_result = send(socket, &ACK, sizeof(ACK), 0);

    if (com_result == -1) {
        perror("\n\t[ERROR] Send ACK");
        return 0;
    }

    return 1;

}

该函数由 server.c 中的另一个函数调用,如下所示:

int ACK = myStruct->ID;
sendACK(socket, ACK);

因此,如果我刚刚收到 ID 为 2 的结构,则输出将为:
发送值为 2 的 ACK

在 client.c 中,我收到这样的 ACK:

int ACK = 0;
data_size  = sizeof(myStruct.ID);
com_result = read(client_socket, &ACK, data_size);
ACK = ntohl(ACK);

if (com_result == -1) {
    perror("\n\t[ERROR] Read");
} else {
    printf("\n\tRecieved %i of %i bytes", com_result, data_size);
    if (ACK != myStruct.ID) {
        printf("\n\t[ERROR] Invalid ACK (%i).", ACK);
    }
}

但是收到的 ACK 的值始终为 1,因此客户端的输出为:
收到 4 个字节(共 4 个字节)
错误] 无效的 ACK (1)。

为什么未收到值为 2 的整数,如何修复此问题?

I'm building a client and a server program that exchanges data over TCP and I'm having trouble sending an ACK-confirmation from the server back to the client when an operation is successfull. I've managed to send a struct with various members from the client to the server, then the server should respond by sending an integer confirming the ID of the struct back to the client.

In server.c I have this function:

int sendACK(int socket, int ack_int){

    int com_result;
    int ACK = htonl(ack_int);

    printf("\n\tSending ACK with value: %i", ack_int);

    com_result = send(socket, &ACK, sizeof(ACK), 0);

    if (com_result == -1) {
        perror("\n\t[ERROR] Send ACK");
        return 0;
    }

    return 1;

}

This function is called by another function in server.c like this:

int ACK = myStruct->ID;
sendACK(socket, ACK);

So, if I have just recieved a struct with ID: 2, the output will be:
Sending ACK with value: 2

In client.c I recieve the ACK like this:

int ACK = 0;
data_size  = sizeof(myStruct.ID);
com_result = read(client_socket, &ACK, data_size);
ACK = ntohl(ACK);

if (com_result == -1) {
    perror("\n\t[ERROR] Read");
} else {
    printf("\n\tRecieved %i of %i bytes", com_result, data_size);
    if (ACK != myStruct.ID) {
        printf("\n\t[ERROR] Invalid ACK (%i).", ACK);
    }
}

But the ACK is always recieved with a value of 1, so the output from client is:
Recieved 4 of 4 bytes
ERROR] Invalid ACK (1).

Why isn't the integer recieved with a value of 2, and how may I fix this?

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

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

发布评论

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

评论(5

酸甜透明夹心 2024-08-18 11:26:35

这很可能不是问题,但我相信下面的 client.c 片段中的第二行应该是 sizeof(ACK)。

int ACK = 0;
data_size  = sizeof(ACK); // was: sizeof(myStruct.ID);
com_result = read(client_socket, &ACK, data_size);
ACK = ntohl(ACK);

另外,根据您的数据流,您是否实际上正在正确读取并且 ACK 是您之前发送的数据包的响应?

This is most likely not the problem, but I believe that the 2nd line in your client.c snippet below should be sizeof(ACK).

int ACK = 0;
data_size  = sizeof(ACK); // was: sizeof(myStruct.ID);
com_result = read(client_socket, &ACK, data_size);
ACK = ntohl(ACK);

Also, depending on your data flow, is it possible that you're actually reading correctly and the ACK is the response from a packet which you sent previously?

飘然心甜 2024-08-18 11:26:35

你不应该像这样发送整个值,你需要更多地关注序列化。例如,如果发送机器上的 sizeof (int) 与接收机器上的 sizeof (int) 不同,则此操作可能会失败,在这种情况下,您将读取不正确的字节数。

最好更加小心,一次一个字节地序列化字段。

You should never send whole values like this, you need to pay more attention to serialization. This might fail if, for instance, the sizeof (int) is different on the sending machine from the receiving one, in which case you will read the incorrect number of bytes.

It's better to take more care and serialize fields one byte at a time.

娇纵 2024-08-18 11:26:35

为了调试它,您可以使用wireshark。执行wireshark并设置其选项以侦听所需的端口。您将看到数据包内实际通过网络发送的内容。这将使您了解问题是否出在服务器、客户端或两者上。

In order to debug it, you can use wireshark. Execute wireshark and set its options to listen to the required port. You will see what was actually sent inside the packet through the network. This will allow you to understand if the problem is with the server, the client or both.

忆沫 2024-08-18 11:26:35

我无法找出您上面粘贴的代码有什么问题。我建议使用 Wireshark 或其他套接字跟踪程序,并查看线路上的位。

I can't figure out anything wrong with the code you pasted above. I'd suggest getting out Wireshark, or another socket tracing program, and looking at the bits on the wire.

那小子欠揍 2024-08-18 11:26:35

我会检查您的主要发送者/接收者代码是否正在处理正确的数据量。

如果发送方发送 20 个字节,后跟一个 4 字节 ACK,但接收方在尝试读取 ACK 之前仅读取 16 个字节,则可能会遇到此类问题。

如果您要发送可变长度数据(即带有字节计数前缀),则常见的困惑是正在传输的字节计数是否实际上包含发送字节计数所需的附加字节......

I'd check that your main sender/receiver code is handling the correct amount of data.

If your sender sends 20 bytes followed by a four byte ACK but the receiver only reads 16 before trying to read the ACK, you could have this sort of problem.

If you're sending variable length data (ie. with a bytecount prefix) a common confusion about whether the bytecount being transmitted actually includes the additional bytes required to send the byte count...

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