Hi,
I am using WinSock library for sending data over network, and I have a problem.
I know how to send data in one struct and receive it on the other side, because there is constant size of structure. But what if I have more structures, each one with different size?
Now I have one struct that contains locations of players and other with in-game messages. When I am about to receive struct, I don't know which one is being sent.
Is there a "trick" how to do that?

Thanks for help.

Edited 5 Years Ago by Cainer: n/a

I've dabbled in winsock a bit. So i'll throw in me' two cents:

I believe you should have a couple of struct attributes that include the 'type' of struct, and perhaps also the 'size' of the struct. These should probably be the first two things ye' test for as a client. Then ye' can identify and handle the type of data ye' are receiving.

there seems to be a common theme among win32 and winsock ADT's in that they usually contain a 'header' of some sort, which tells you everything you need to know about the struct, before you even begin to dig into of the contents of the struct. Try employing a similar strategy in the design your ADT's.

extra bonus
(also, i believe the size of many Winsock structs are 'padded' with useless attributes to the nearest power of 2 (128, 256, 512, 1024, etc) for sending and receiving efficiency. Win32 ADT's employ the same strategy for memory management efficiency.)

Edited 5 Years Ago by Clinton Portis: n/a

But when I call recv function, I need to know the size of struct before it is actually received. That's the problem.

call recv() twice, the first time to get just the first 4 bytes (assuming the size of the structure is at the beginning, and then again to get the rest of the structure.

call recv() twice, the first time to get just the first 4 bytes (assuming the size of the structure is at the beginning, and then again to get the rest of the structure.

I tried this. However, it worked correctly only on localhost, when I tried it on network I sometimes get wrong data. Or is mistake somewhere else?
Btw, I am using asynchronous sockets.

Edited 5 Years Ago by Cainer: n/a

> I know how to send data in one struct and receive it on the other side, because there is constant size of structure.

Assuming that you are not converting to/from a common network data representation of the struct, this will work only if the POD struct involved has the same binary layout at both ends. This would be the case if both ends run on the same processor architecture, the same OS, are compiled with the same version of the same compiler and with the same compiler flags.

If for some absolutely compelling reason you decide to transmit POD structs in native binary form, and there is more than one such struct involved, one idea would be to wrap them in a discriminated union for send; and unwrap based on the type field after recv.

struct one { /*...*/ } ;
struct two { /*...*/ } ;
// ...

struct any
{
    enum struct_type { POD_ONE, POD_TWO /*, ... */ } ;

    any( const one& data ) : type(POD_ONE), pod_struct_one(data) {}
    any( const two& data ) : type(POD_TWO), pod_struct_two(data) {}
    // ...

    struct_type type ;
    union
    {
        one pod_struct_one ;
        two pod_struct_two ;
        // ...
    };
};

Ok, I don't know where was the problem, but it works now ...
Thanks for help.

This question has already been answered. Start a new discussion instead.