dynamic array through sockets

Started by
17 comments, last by rip-off 11 years, 8 months ago
hey,
as in title is it possible to send a dynamic array through sockets with WinSock2 ?
Advertisement
Of course. You can send anything over a socket that can be encoded as a sequence of bytes.

Are you interested in a particular language's capabilities in this regard, or a networking library, or...?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

im interested in c++

im interested in c++


C++ doesn't have any network features and winsock is a low level library so its all up to you how to encode and send it.

one rather simple format to encode an array in would be
a header that tells the reciever that the packet contains an array with x elements of datatype y. (in a verbose plaintext format you could encode it as Array:5454:Enemy (in a binary representation you'd replace the words Array and Enemy with an integer number between 0 and the max number of different packet and datatypes supported)
and then just send 5454 Enemy objects (if they have a fixed size you can just write the member variables in a fixed order to the socket, if they have dynamic fields you need a header for each object in the array aswell)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
One cannot answer this in isolation. It is intimately connected with how you construct valid outgoing "messages" and how you try interpret potentially valid incoming messages. Have you such a schema already? If so, what is it?

Are you using TCP or UDP?
i'm using TCP

currintly im sending messages with structs

struct ClientPacket
{
Player p;
};


void Send()
{

ClientPacket packet;

packet.p = a;

send(conn,(char const *)&packet,sizeof(packet),0);

}
Depending on how Player is defined that may not work. It would probably be better to use a marshalling/serialization library.
How would you save this array to a file? Sending data over TCP is very similar to saving (and re-loading) data to (and from) files.
enum Bool { True, False, FileNotFound };
ok so is there any way to send a dynamic array in the struct i'm sending ?

ok so is there any way to send a dynamic array in the struct i'm sending ?


Yes, use a serialization library .

This topic is closed to new replies.

Advertisement