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

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

问题描述

至于有人建议在回答我的最后一个问题(<一个href=\"http://stackoverflow.com/questions/672241/how-do-i-send-an-array-of-integers-over-tcp-in-c\">How我用C发送整数数组通过TCP?),我试图发送长整型数组,但是我可能会做一些突破的解决方案...

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).

推荐答案

额外的指针是不必要的。数组(这基本上是一个指针)可以直接通过。你错误地使用指针而不是数据类型(长)的尺寸的大小;使用的sizeof(长)* 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(长)* 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.

使用类型小心如长,因为它不是总是在不同平台上相同的大小(即,32位与64位);看好大小类型,如 int32_t 代替。此外,您还可以运行与字节序的问题,以及。考虑写之前字节交换与 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天全站免登陆