• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Demx

dynamic array through sockets

18 posts in this topic

hey,
as in title is it possible to send a dynamic array through sockets with WinSock2 ?
0

Share this post


Link to post
Share on other sites
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...?
0

Share this post


Link to post
Share on other sites
[quote name='Demx' timestamp='1343852283' post='4965291']
im interested in c++
[/quote]

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) Edited by SimonForsman
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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);

}
0

Share this post


Link to post
Share on other sites
Depending on how Player is defined that may not work. It would probably be better to use a marshalling/serialization library. Edited by wood_brian
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
ok so is there any way to send a dynamic array in the struct i'm sending ?
0

Share this post


Link to post
Share on other sites
[quote name='Demx' timestamp='1343934514' post='4965623']
ok so is there any way to send a dynamic array in the struct i'm sending ?
[/quote]

Yes, use a [url="http://en.wikipedia.org/wiki/Serialization#C_and_C.2B.2B"]serialization library[/url] .
0

Share this post


Link to post
Share on other sites
[quote]
Yes, use a [url="http://en.wikipedia.org/wiki/Serialization#C_and_C.2B.2B"]serialization library[/url] .
[/quote]

is boost a good library for this ?



btw currently i'm using a static array to send the players data from server to clients:

[code]
struct data
{
Player array[50];
}
[/code]

But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ? Edited by Demx
0

Share this post


Link to post
Share on other sites
[quote name='Demx' timestamp='1343948665' post='4965668']
is boost a good library for this ?
[/quote]

There are a lot of good libraries in Boost. The serialization library in Boost has [url="http://webEbenezer.net/comparison.html"]some weaknesses[/url] though.


[quote]
btw currently i'm using a static array to send the players data from server to clients:

[code]
struct data
{
Player array[50];
}
[/code]

But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ?
[/quote]

Where did you read that?
0

Share this post


Link to post
Share on other sites
[quote]
btw currently i'm using a static array to send the players data from server to clients:

[code]
struct data
{
Player array[50];
}
[/code]

But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ?
[/quote]

[quote]
Where did you read that?
[/quote]

on some random forums.. so serialization doesn't increase the size of the struct ?
0

Share this post


Link to post
Share on other sites
If dealing with a "low level" networking library instead of a high-level serialization library, then it will usually send as many bytes as you tell it to send. With your example of "[font=courier new,courier,monospace]struct Data { Player array[50]; }[/font]", if you give your networking library a pointer to the start of one of these objects, and tell it to send [font=courier new,courier,monospace]sizeof(data)[/font] bytes, then yes, that will send the whole structure ([i]all 50 players[/i]).
e.g.[code]Data myData;
send( &myData, sizeof(Data) );//send the whole struct
///.... on the other end ....
Data myData;
receive( &myData, sizeof(Data) );//read the whole struct[/code]
The usual solution is to prepend the your variable-length data with the amount of data that is going to be sent/received. e.g.[code]struct Data { int numPlayers; Player array[50]; }
Data myData;
myData.numPlayers = 42;
send( &myData.numPlayers, sizeof(int) );//send the length so the other end knows how much of the array to expect
send( &myData.array[0], sizeof(Player)*myData.numPlayers );//only send the part of the array that matters
///.... on the other end ....
Data myData;
receive( &myData.numPlayers, sizeof(int) );//read the length in first
receive( &myData.array[0], sizeof(Player)*myData.numPlayers );//now we know how much data to read[/code]
A serialisation library usually just tries to make all of this less complicated, by implementing these details for you. Edited by Hodgman
0

Share this post


Link to post
Share on other sites
If you send something between two of your aplications, you will have to deconstrut and reconstrat the structure, becouse you transmit only bytes, array of bytes, as you said, but dynamic array of bytes recalls std::vector<> type to me. You would have to deconstruct and reconstruct such a type from raw bytes, but that would be rather simple bevaouse std::vector is simulatenous array of bytes, just self growing and reallcoable, so it would be just asking bytes from array, sending them, and filling std::vector type on other end. But you should handle , client and server, in a way that they even do so.
0

Share this post


Link to post
Share on other sites
I will answer your question backwards:

[quote]
... so serialization doesn't increase the size of the struct ?
[/quote]
Forget about structs. What you need to worry about is the amount of data on the wire. The data on the wire is a serialised form of the data in memory. Directly sending a struct is an extremely basic form of serialisation - one where the memory and serialised representations are identical. Though basic, It is not the most efficient.

A structure will often contain "padding bytes". These bytes are placed so that data can be efficiently accessed by the processor (or in some architectures, so that it can be accessed without triggering an error!). It is not necessary to send such padding on the wire. Another example is data ranges. Say you have a Player structure with a health member variable of type "int". On common toolchains, you will have a four byte variable. However, your game might only ever have health values in the range 0 to 100. Thus, you could efficiently encode the health in a single byte.

Serialisation can go beyond just bytes. You can serialise boolean values to individual bits. The aforementioned health value needs only 7 bits to encode the full range.

Finally, one you get out of the mentality of "sending" a "struct" to "serialise to a byte stream and send that", you can easily add compression as part of the serialisation process, which might save more space depending on the nature of the data.

Combining these, a hand rolled serialisation scheme will almost certainly be as space efficient, if not more so, than directly sending the source structure. A serialisation library, one with space efficiency as a design goal, should also be able to compete with the source structures on size.
[quote]
[quote]
[quote]
... i read that serialization greatly increases the size of the struct so at the end is it worth it ?
[/quote]
Where did you read that?
[/quote]
on some random forums...
[/quote]
What you read on "some random forums" was probably referring to generalised and/or automatic serialisation. Such tools may not designed for space optimisation. For example, they may not allow you to express domain specific knowledge such as the range of a "health" variable, etc.

They have different design goals. For example they may not require you to list all possible messages/serialised forms up front. They may support deserialising more than one "version" of the data.
1

Share this post


Link to post
Share on other sites
One of the biggest sources of bloat in serialization libraries is the ability to serialize object instances, and whole object network graphs. Typically, this is implemented by sending large bits of information about each data type, such that the receiver can use reflection to re-construct each object with the right type on the other end.
0

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1344296002' post='4966852']

What you read on "some random forums" was probably referring to generalised and/or automatic serialisation. Such tools may not designed for space optimisation. For example, they may not allow you to express domain specific knowledge such as the range of a "health" variable, etc.
[/quote]

All the serialization libraries I know of eliminate padding that compilers have added. They also allow marshalling of single bytes. They may not support bit level access though. I'm not aware of a library that supports that. Edited by wood_brian
-1

Share this post


Link to post
Share on other sites
[quote]
They may not support bit level access though. I'm not aware of a library that supports that.
[/quote]
I was thinking of Raknet, which, while not a standalone serialisation library, provides related functionality through its [url="http://www.jenkinssoftware.com/raknet/manual/Doxygen/classRakNet_1_1BitStream.html"]BitStream[/url] class.

Actually, if we are recommending libraries to the OP, Raknet is a reasonably popular networking library that solves these problems in a high level way.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0