hey,
as in title is it possible to send a dynamic array through sockets with WinSock2 ?
dynamic array through sockets
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...?
Are you interested in a particular language's capabilities in this regard, or a networking library, or...?
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)
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?
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);
}
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.
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
Popular Topics
Advertisement