Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sending data questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Jeffreyp   Members   -  Reputation: 178

Like
0Likes
Like

Posted 14 June 2012 - 02:59 AM

Hey guys,

I have a question about sending data in UDP.
For example i have this structure :



structure NetworkPacket

{

	 char *m_data;

	 int m_id;

};





And i fill it like this :


SomeStructure l_data;

...

NetworkPacket l_packet;

l_packet.m_data = (char*)(&l_data);

l_packet.m_id = 1;



And send it / receive it like this



sendto(socket, (char*)(&l_packet), sizeof(NetworkPacket), 0, (sockaddr*)&address,

		   sizeof(sockaddr));



NetworkPacket l_packet;



auto l_bytes_received  = 0;



l_bytes_received = recvfrom(socket, (char*)(&l_packet), sizeof(NetworkPacket), 0,

(sockaddr*)&l_incomingAddress, &l_size)



Is it true that this will fail because in the structure NetworkPacket the char * is dynamic so it's size isn't known in the structure declaration?
Also i had an idea about solving it if the above is true.
What about just sending a packet with the size of the packet that will come next in it?
But this will fail if the packets got reordered somewhere. Does that happen often in UDP?
If so then this design isn't really good, and if it doesn't should i just ignore that and send the packets again that where lost?

Thanks in advance.

Sponsor:

#2 smasherprog   Members   -  Reputation: 432

Like
1Likes
Like

Posted 14 June 2012 - 09:37 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids
unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &sizetosend, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));

Edited by smasherprog, 14 June 2012 - 09:40 AM.

Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#3 Jeffreyp   Members   -  Reputation: 178

Like
0Likes
Like

Posted 14 June 2012 - 10:45 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids

unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &sizetosend, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));


Oke thanks, but how will i handle this on the receiving end?
Because the information about the size is packed in the packet.
So i dont't know how big the packet is gonna be on the receiving end.
And i need that information in the recvfrom function.
Or am i missing something?

#4 hplus0603   Moderators   -  Reputation: 5302

Like
1Likes
Like

Posted 14 June 2012 - 11:26 AM

Or am i missing something?


It sounds like you're missing a basic understanding of how data is represented and referenced in memory, and in external storage.
If you were to save this same data to a file, such that you could later read it back into memory, how would you do it?
In this sense, the payload of a network packet is like a file on disk. The question of how to format the data for basic transmission and retrieval is the same.
enum Bool { True, False, FileNotFound };

#5 Jeffreyp   Members   -  Reputation: 178

Like
0Likes
Like

Posted 14 June 2012 - 02:24 PM

If you were to save this same data to a file, such that you could later read it back into memory, how would you do it?



I would probably read the data by each line or each char.
But i don't really get how i would apply that to a packet.
Can you explain a little more ?
Or do you know a better way of packing data(that allow structures)?

Thanks : )

Edited by Jeffreyp, 14 June 2012 - 02:32 PM.


#6 hplus0603   Moderators   -  Reputation: 5302

Like
2Likes
Like

Posted 14 June 2012 - 04:09 PM

Treat the data packet as a file. (It's a memory stream)
"Write" your packet to this memory stream.
Send the bytes of that memory stream as your packet.
On the other end, read the data back from a similar memory stream, initialized with the data of the packet.

Also, I highly recommend you practice the difference between pointers, arrays, and variables, and study how the stack and the heap work in C/C++. You should be able to read and follow the implementation of malloc()/free() and new/delete in your standard library (MSVC or GLIBC.) If you see things there that you don't understand, I recommend you study up on what those are!
Once you can do this, you'll have enough understanding of the bits-and-bytes of systems programming to start doing binary-level networking effectively.

enum Bool { True, False, FileNotFound };

#7 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 14 June 2012 - 04:16 PM

I would probably read the data by each line or each char.
But i don't really get how i would apply that to a packet.
Can you explain a little more ?
Or do you know a better way of packing data(that allow structures)?

Thanks : )


I'm not sure if you're using C or C++. If you're using C, I can't help you, but if you're using C++, the C++ Middleware Writer could help you with the marshalling.

#8 dpadam450   Members   -  Reputation: 922

Like
-1Likes
Like

Posted 14 June 2012 - 10:12 PM

Your packet holds a pointer (which is an address) and an integer. It will send:
Address of l_data;
Integer id;

What you want to send is
l_data (including all its members like say position, name, rotation whatever online data)
Integer id;

To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data, ...sizeof(SomeStructure));
offset += sizeof(SomeStructure);
memcpy(&(Buffer[offset]), &id, 4);//integer is 4 bytes

SendTo(.....Buffer, offset);//sending "offset bytes" which is 4 + sizeof(SomeStructure)

#9 frob   Moderators   -  Reputation: 21212

Like
1Likes
Like

Posted 15 June 2012 - 10:34 AM

What you want to send is
l_data (including all its members like say position, name, rotation whatever online data)
Integer id;

To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data, ...sizeof(SomeStructure));
offset += sizeof(SomeStructure);

Please don't do that.

If you did that in our studio you would be facing a VERY tough code review.

Proper serialization means allocating exactly the correct amount of space (rather than blindly allocating 1000 bytes), ensuring that only necessary fields are stored (instead of blindly copying the entire structure), and almost always it means implementing a simple interface (e.g. Serialize() and Deserialize()) so the object itself is responsible for reading itself in and writing itself out in optimal fashion.

As h+ pointed out, there is fundamentally no difference between serialization across a network and serialization to disk. In several games we have used disk-based networking where both sides would write to shared files; disk-based virtual networks are replayable and help with more complex networking issues like packet loss. All that maters from a game perspective is that you are hooked up to data streams.


Please to read many of the articles on serialization to learn how to do it properly.
Check out my personal indie blog at bryanwagstaff.com.

#10 hplus0603   Moderators   -  Reputation: 5302

Like
0Likes
Like

Posted 15 June 2012 - 11:16 AM

I disagree that you need to allocate the exactly right number of bytes. Any stream-based output system is likely to use malloc() or a container like std::vector<> for implementation, and those will certainly pad most size requests up a bit.
If you have an upper limit to the size of a serialized packet, then that's a fine, fixed, size to use for your serialization buffer, assuming that your code does the right thing (i e, fails loudly) if you try to exceed that size.
The bonus of keeping some number of pre-allocated, fixed-size blocks around is that you don't have to worry about fragmentation.

The rest of the advice is fine!

Edited by hplus0603, 15 June 2012 - 11:17 AM.

enum Bool { True, False, FileNotFound };

#11 dpadam450   Members   -  Reputation: 922

Like
1Likes
Like

Posted 15 June 2012 - 12:10 PM

To the OP: what I meant was:
To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data.m_data, ...sizeof(YOUR_M_DATA));
offset += sizeof(YOUR_M_DATA);
memcpy(&(Buffer[offset]), &l_data.m_id, 4);//integer is 4 bytes

Basically you have to copy what is actually in the pointer itself was what I was getting at.

Edited by dpadam450, 15 June 2012 - 12:32 PM.


#12 Jeffreyp   Members   -  Reputation: 178

Like
0Likes
Like

Posted 15 June 2012 - 01:20 PM

Thanks for all the advice : ). I already got something up and running with memcpy.
I also found something else(what hplus mentioned) here : http://stackoverflow...nd-over-network (scroll down a bit for the code). Would this be a better solution?
Also i couldn't find to much information about

serialization(found 1 tutorial).


If anybody got some useful links post them : )



Thanks


Edited by Jeffreyp, 15 June 2012 - 01:21 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS