简体   繁体   English

使用UDP(WinSock和C ++)发送和接收结构?

[英]Sending and receiving structs using UDP (WinSock & C++)?

I have two programs. 我有两个程序。 I need one of them to send data and the other to receive that data. 我需要其中一个发送数据,而另一个接收数据。

I have some code in place that is hopefully sending a struct across the network. 我在地方,那就是希望发送整个网络结构的一些代码。

However, I don't even know if it is working properly because I don't know how to code the receiving program to receive structs and pass the data it receives into a local struct to be manipulated. 但是,我什至不知道它是否正常工作,因为我不知道如何编码接收程序以接收结构并将接收到的数据传递到本地结构中进行操作。

Here is the code I'm using to send if it helps any 这是我用来发送的代码,如果有帮助的话

gamePacket.PplayerX = userSprite.x;
gamePacket.PplayerY = userSprite.y;
gamePacket.Plives = lives;
gamePacket.Pstate = state;
for(int z=0;z<8;z++)
{
    gamePacket.PenemyX[z] = enemySprite[z].x;
    gamePacket.PenemyY[z] = enemySprite[z].y;
}   

char Buffer[sizeof(gamePacket)];

UDPSocket.Send(Buffer);

The struct is called Packet and gamePacket is an instance of it. 该结构称为Packet,而gamePacket是其实例。 What I am stuck with is: 我坚持的是:

  1. Is the code I posted even sending the struct 我发布的代码甚至发送了结构
  2. How do I receive the struct in the receiving program so that I can use the data inside it. 如何在接收程序中接收该结构,以便可以使用其中的数据。
  1. Its not send, you only declare a buffer. 它不发送,您只声明一个缓冲区。 To send it you need to fill it. 要发送它,您需要填写它。 Also the way you use sizeof is wrong, it probably doesn't return the right size of all fields, you should count them up. 同样,您使用sizeof的方法是错误的,它可能不会返回所有字段的正确大小,您应该将它们加起来。

  2. When you received everything you do the opposite, you allocate a struct and fill it using ofsets 收到所有内容后,您会做相反的事情,即分配一个结构,并使用偏移量填充它

If you need examples just ask. 如果您需要示例,请询问。 But learning is doing research so an push in the right direction is I think enough. 但是学习是在做研究,所以我认为朝正确的方向推进就足够了。 (There are thousand examples on this.) (对此有数千个示例。)

Ps: you can use pointers + offset because the memory of the struct is layed out next to each other. 附言:您可以使用指针+偏移量,因为该结构的内存彼此相邻放置。 It are blocks of memory, just like an array. 它是内存块,就像数组一样。

EDIT; 编辑; this link is what you need: Passing a structure through Sockets in C EDIT: Example using pointers: EDIT: Is this C# or C/C++? 该链接是您所需要的: 通过C中的套接字传递结构编辑:使用指针的示例:编辑:这是C#还是C / C ++? I'm sorry if so, change the example to C/C++ ;) ' 抱歉,请将示例更改为C / C ++;)'

struct StructExample
{
    int x;
    int y;
};

int GetBytes(struct* Struct, void* buf)
{

        //Access the memory location and store
        *(int*)(buf + 0) = Struct->x;
        *(int*)(buf + sizeof(int)) = Struct->y;
    return sizeof(Struct->x) + sizeof(Struct->y)
}

Ps: I typed it with my mobile, I'm not 100% sure it compiles/works. 附言:我用手机打过它,我不是100%确信它可以编译/工作。

In c and c++ it is possible to use this code: 在c和c ++中,可以使用以下代码:

struct StructExample
{
    int x;
    int y;
};
struct StructExample a;
a->x = 1;
a->y = 2;
send(FSocket, &a, sizeof(a), 0);

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

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