为什么从 TCP 套接字读取整数数组时会得到奇怪的结果? [英] Why do I get weird results when reading an array of integers from a TCP socket?

查看:17
本文介绍了为什么从 TCP 套接字读取整数数组时会得到奇怪的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在我上一个问题的回答中所建议的(如何在 C 中通过 TCP 发送整数数组?),我尝试发送一个 long int 数组,但是我可能正在做一些事情来破坏解决方案......>

As was suggested in an answer to my last question (How do I send an array of integers over TCP in C?), I tried to send an array of long int, however I may be doing something to break the solution...

#define ARRAY_LEN 4

/* I'm using long because the numbers are very large,
 * but in this example they're small to save space. */
long originalArray[ARRAY_LEN] = { 1, 2, 3, 4 };

myObject.SetMyArray(originalArray);

// NOTE: The following is in a different function.

long *myArrayFromFunction = myObject.GetMyArray();

write(clientSocketFD, myArrayFromFunction, sizeof(myArrayFromFunction) * ARRAY_LEN);

转换为指针然后传递不正确吗?

Is converting to a pointer then passing incorrect?

在客户端读取时,我得到的不是我发送的数字(1、2、3、4),而是很长的数字,例如 140088443806649...

When reading on the client side, instead of getting the numbers I sent (1, 2, 3, 4), I get long numbers such as 140088443806649...

#define ARRAY_LEN 4

long targetArray[ARRAY_LEN];
read(socketFD, targetArray, sizeof(targetArray) * ARRAY_LEN);

所以,假设我需要读入一个指针,我尝试了这个......

So, assuming that I need to read into a pointer, I tried this...

#define ARRAY_LEN 4

long *targetArray;
read(socketFD, targetArray, sizeof(targetArray) * ARRAY_LEN);

但这也不起作用(读取函数返回 -1).

But this didn't work either (the read function returned -1).

推荐答案

多余的指针是不必要的.数组(基本上是一个指针)可以直接传递.您错误地使用了指针的大小而不是数据类型的大小(long);使用 sizeof (long) * ARRAY_LEN 或仅使用 sizeof (originalArray) 代替.

The extra pointer is unnecessary. The array (which basically is a pointer) can be passed directly. You are incorrectly using the size of the pointer instead of the size of the data type (long); use sizeof (long) * ARRAY_LEN or just sizeof (originalArray) instead.

写:

#define ARRAY_LEN 4
long originalArray[ARRAY_LEN] = { 1, 2, 3, 4 };
write(clientSocketFD, originalArray, sizeof (originalArray));

阅读:

#define ARRAY_LEN 4
long targetArray[ARRAY_LEN];
read(socketFD, targetArray, sizeof (targetArray));

如果您将数组作为指针传递,则不能使用 sizeof 来获取数组的总大小.在这种情况下,大小必须与指针一起传递,并且大小用 sizeof (long) * ARRAY_LEN 构造.

If you are passing the array as a pointer, then sizeof cannot be used to get the total size of the array. In that case, the size must be passed along with the pointer and the size constructed with sizeof (long) * ARRAY_LEN.

使用诸如 long 之类的类型时要小心,因为它在不同平台上的大小并不总是相同(即 32 位与 64 位);喜欢像 int32_t 这样的大小类型.此外,您也可能会遇到 endianess 的问题.考虑在写入之前使用 htonl 对值进行字节交换.

Be careful using a type such as long since it is not always the same size on different platforms (i.e. 32-bit versus 64-bit); favor sized types like int32_t instead. Also, you can run into issues with endianess as well. Consider byte-swapping the values with htonl before writing.

这篇关于为什么从 TCP 套接字读取整数数组时会得到奇怪的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆