Winsock数据传输 [英] winsock data transfer

查看:54
本文介绍了Winsock数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c ++中的winsock2从客户端和服务器传递和检索结构类型的数据?

How to pass and retrieve data in structure type from client and server using winsock2 in c++?

推荐答案

最简单的解决方案是

如果您的结构像

The simplest solution would be

if your struct is like

struct MyStruct
{
int a;
int b;
};



在发送方.



At the sender side.

MyStruct myObject;
myObject.a = 10;
myObject.b = 20;


send( SOCKET, (char*)&myObject, sizeof(MyStruct), 0);




并且在接收方;




and at recieving side;

MyStruct myObjectRecv;

recv( SOCKET, (char*)&myObjectRecv, sizeof(MyStruct), 0 );



现在您可以说myObjectRecv是myObject的副本.


这种方法的局限性是,如果您的结构包含指针,它将产生错误的结果.



Now you can say that myObjectRecv is a clone of myObject.


Limitation of this approach is , it will produce errorneous results if your structure contains pointers.


我了解您是专门询问WinSock和C ++的,但是仍然需要考虑一些问题:

仅了解"结构是不够的.有必要考虑以下因素:

1)数据对齐.例如,如果您具有:

I understand you have asked about WinSock and C++ specifically, but still, there are some issues to consider:

Merely "understanding" the structure is insufficient. It is necessary to consider the following:

1) Data alignment. For example, if you have:

typedef struct
{
 long int a;
 short b;
 long int c;
} SOME_STRUCT;



根据编译器选项的不同,目标CPU的总线宽度(例如64/32/16/8位等)的大小可能会有所不同-在Windows上,根据编译器选项的不同,它可能为10或12字节.这是因为如果结构成员在4字节边界上对齐,则编译器可能会在``b''之后留2个字节的间隔.

所有编译器都有数据对齐实用程序和选项.例如,Visual Studio称它们为结构成员对齐".因此,即使您在两端使用相同的平台,也可能会有所不同,然后可能会遇到问题.

2)除非您考虑Windows CE支持的多种平台,否则可能不是问题.然后,您应该考虑数据的字节顺序(因为客户端和服务器可能使用不同的模型).例如,客户端可以发送具有最高有效字节在前的4字节整数,而服务器可以将它们解释为具有最低有效字节在前的4字节数量.参见:

http://en.wikipedia.org/wiki/Endianness [



depending on compiler options, the bus width of the target CPU (e.g. 64/32/16/8 bits etc) SOME_STRUCT may end up different in size - on Windows it could be 10 or 12 bytes depending on the compiler option. This is because the compiler may leave a gap of 2 bytes after ''b'' if struct members are aligned on 4-byte boundaries.

All compilers have data alignment pragmas and options. Visual studio for example, calls them "struct member alignment". So it is possible to have this different even if you use the same platform on both ends and then you could run into problems.

2) Probably not an issue unless you consider a mulitude of platforms supported by Windows CE. Then you should consider the endianness of data (as client and server may use different model). For example, client may send 4-byte integers with most significant byte first, and server may interpret them as 4-byte quantities with least significant byte first. See:

http://en.wikipedia.org/wiki/Endianness[^]

3) If you are using exactly the same platform on both ends, and you have compiled both server and client code with the same struct alignment, then you can indeed send your struct as a stream of bytes and on the other end simply interpret the received data as the same structure.

4) It is a valid and common method to define a communications protocol using C data structs provided you have specified the alignment rules and endianness of data.


只需以字节流的形式发送和接收.只要套接字的两端都了解数据的结构,就应该没有问题.
Just send and receive as a stream of bytes. As long as both ends of the socket understand the structure of the data you should have no problems.


这篇关于Winsock数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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