Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to create my own network packets.
How can i create my own network packets in c++,OR edit the packets.
Posted
Updated 17-Jul-13 2:42am
v2
Comments
CPallini 17-Jul-13 8:00am    
As it stands, your question is not clear. What you intend to do, exactly?
Do you want to replace (or implement yourself) the ISO/OSI model? Or do you want simply send your structures over the network?
Alamgirable 17-Jul-13 8:40am    
i want simply send my structure over the network.
[no name] 17-Jul-13 8:45am    
Okay so go ahead and do that. Help you with what exactly? Saying "I want" not a question or description of any kind of a problem. And the answer to "how" is you write some code.
CPallini 17-Jul-13 8:56am    
That should be simple (a call to send would do the job). Just be sure to correctly pass the size of your struct (and make sure the other side understands what are you sending).
Richard MacCutchan 17-Jul-13 11:12am    
Brilliant; now why didn't I think of that?

1 solution

What you want to reearch is Data Marshalling[^] and its close cousin Serialization[^]. These are common tasks in computing.

The main point is that if your struct/class just contains fundamental data types[^] and no pointers, just send it across the wire without conversion and read "as is" on the other side (you will need a cast to read in enum values). If it contains pointers you will have to dereference the pointer and send that info.
 
Share this answer
 
Comments
pasztorpisti 18-Jul-13 4:46am    
5ed, Simple stupid serialization (writing/reading a struct directly) is enough here as a first step and using an enum as a struct member besides the fundamental data types is okay, the same is true for fixed C arrays. The most important thing is making sure that the two machines run executables that were compiled with the same alignment and the endianness must be handled if the two architectures are different in this regard.
We missed an important thing: all packets should be prefixed with an integer or enum that identifies the type of the packet when we are sending them over the wire. This helps in finding out the number of bytes to read. Of course this is needed only if we have more than one structs defined, for a simple protocol one struct might do the job.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900