0

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. 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.
tshepang
  • 12,111
  • 21
  • 91
  • 136
Tricky D
  • 11
  • 1
  • 3
  • For game programming, consider a library like [enet](http://enet.bespin.org/) -- much more intuitive than doing socket code yourself. – paddy Jan 09 '13 at 21:35

2 Answers2

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

  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++? I'm sorry if so, change the example to 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.

Community
  • 1
  • 1
joell
  • 396
  • 6
  • 17
  • I'd appreciate some examples if you have any, examples of sending and receiving structs would be amazing as I am currently really stuck. Thanks. – Tricky D Jan 09 '13 at 21:08
  • Well instead of byte[] you use char* (or void*) and fixed is not needed. I thought it was C# sorry, anyway tomorrow I'll make a better example! – joell Jan 09 '13 at 21:19
  • thanks a lot man. do you have any examples of sending a struct? – Tricky D Jan 09 '13 at 21:21
  • Beware of endianness and byte orders if the sender and receiver have different processor architectures. Check out [passing a socket in c](http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c) for more details on this. – user1952500 Jan 10 '13 at 00:52
  • http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c – joell Jan 11 '13 at 07:18
0

In c and c++ it is possible to use this code:

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