Question

I've been reading up on winsock and I want to create a small game to test it out. Now I have a client/server system in mind so I need the client to send data to the server.

I can send data that is a char* (like the send function wants) but I can't figure out how I would send structs with data to the other computer. I read that I would need to use serialization but that seems to write to files and I don't need that. So, what I need is that that struct can be send through winsock and can be reconstructed on the server and vice versa.

Can someone help me on understanding this problem? Any help is appreciated.

EDIT: Would the following be sufficient?

tester test;
test.pos = 10;

send(sock, (char*)&test, (int)strlen(test), 0);

MORE EDIT:

This is what works now as a test. Is this a good method?

tester test;
test.pos = 350;

char* buffer = (char*)&test;
tester* testFollowup = (tester*)buffer;
cout << (int)testFollowup->pos;

getchar();
return 0;
Was it helpful?

Solution

I would highly recommend using a library to handle this for you while you get started - a good, lightweight example is Protocol Buffers, a serialization protocol designed by Google.

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.

If Google Protocol Buffers don't suite you, feel free to keep looking around, and I'm sure you'll find something you can use :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top