简体   繁体   English

通过tcp socket发送和接收的数据不一样

[英]data sent and received through tcp socket not the same

My data sent and received through a tcp connection doesn't match up.我通过 tcp 连接发送和接收的数据不匹配。

I have a raspberry hosting a tcp server written in c++.我有一个用 C++ 编写的托管 tcp 服务器的树莓派。 This is used only by my python module using python's socket module.这仅由我的 python 模块使用 python 的socket模块使用。 I send a string to which the server responds with with an unsigned short integer.我发送一个字符串,服务器用一个无符号短整数响应该字符串。 What I send and receive seem to be completely different.我发送和接收的内容似乎完全不同。 Some examples:一些例子:

| sent in hex | received as string |
| ----------- | ------------------ |
| 0xb6fc      |  b'\x0b\xaa'       | 
| 0x80f5      |  b'\xb6h'          |
| 0xf93b      |  b'X\xec           |
| 0x3167      |  b'\x95\x1b'       |

The only consistent factor is the size.唯一一致的因素是大小。 There are always 2 bytes send and 2 bytes received.总是有 2 个字节发送和 2 个字节接收。 I tried using struct.unpack on the received data and got a completely different value.我尝试struct.unpack收到的数据使用struct.unpack并得到一个完全不同的值。

source code源代码

this is running on the server这是在服务器上运行

unsigned short value = 0;

std::thread count_thr(counter, &toggle, &value);

while (1){
            memset(buf, 0, sizeof(buf));
            received = recv(new_fd, &buf, sizeof buf, 0);
            printf("received string: %s\n", buf);

            if (strcmp(buf, "exit") == 0){
                    printf("closing server...\n");
                    close(new_fd);
                    exit(0);
            }
            else if (strcmp(buf, "get") == 0){
                    int bytes_sent = send(new_fd, &value, sizeof value, 0);
                    printf("sending value: %x\nbytes sent: %i\n", value, bytes_sent);
            }
    }

and this on the client这在客户端

try:
sock.sendall('get'.encode())
    data = sock.recv(32)
    length = len(data)
    print('data received: %s\nlength: %i' % (data, len(data)))
except OSError as error:
    msg = str(error)

As you just discovered, this is a self-inflicted race condition.正如您刚刚发现的,这是一种自我造成的竞争条件。 The fix is easy: just take a local copy of value that will not be changed by the external thread before sending:修复很简单:只需在发送之前获取不会被外部线程更改的value的本地副本:

unsigned short copy = value;
int bytes_sent = send(new_fd, &copy, sizeof copy, 0);
printf("sending value: %x\nbytes sent: %i\n", copy, bytes_sent);

as Botje said, I had a race condition since the data of value was constantly being changed while sending it.正如 Botje 所说,我有一个竞争条件,因为value数据在发送时不断变化。 Another problem was the unclear data I received, but it turns out it were ASCII characters instead of the hexadecimal representation of the byte.另一个问题是我收到的数据不清楚,但结果是 ASCII 字符而不是字节的十六进制表示。

with that sorted out,解决了这个问题,

decoded_val = struct.unpack('<H', data)[0]

this line solved my problem.这条线解决了我的问题。 I added the [0] since unpack returned a tuple, and the first item contained the correct value.我添加了[0]因为解包返回了一个元组,并且第一项包含正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM