My question might be very basic but I really do not know how to do this...

I need to create a structure, something that looks like a protocol (a data packet actually)...this packet consists of 3 parts header, data and trailer...

The header has 3 offset bytes, 64 bytes of forwarding label, and so on...then starts the data portion followed by the trailer portion each consisting of their own set of information and hence variable bytes...how do I assign memory in terms of bytes instead of a data-type...I do not want to fix a byte with a data-type (eg: I do not want to use char to assign 1 byte, int for 2 n so on)....the answer I guess to this problem is using templates...but I do not know how to assign byte-wise memory...

Can someone please let me know...

The best way to do this is perhaps this: declare a structure representing the information. (Perhaps a header structure, a data one, a tail, and a packet structure that contains all three.)

If you want to skip a given number of bits, use an unnamed bitfield.

struct {
    unsigned : 3;
};

If you want a variable of a given number of bits, use a named bitfield.

struct {
    unsigned three : 3;
};

If you want byte-aligned data, use chars or shorts etc. If it's data that is two bytes but isn't really a number, you could use an array of two chars.

Don't use bitfields unless you have to . . . .

Note that for this to work, you'll have to disable structure alignment.

Well, you can make everything an unsigned char and store a big array of them. Typecast as needed, but an unsigned char is one byte. Check out this thread. It may be of use. I don't see that a template is necessary, but make sure to realize, as I did not at the time and as Narue pointed out in post number 10 in the thread below, that a byte isn't necessarily eight bits.

http://www.daniweb.com/forums/thread130222.html

So maybe you have structs called "header", "data", and "trailer". These structs are made up of variables of type unsigned char*. Perhaps have a struct called "packet", which has variables of type header, data, and trailer.

for dwks -Why is using bitfields not advisable? You mentioned that I can use an array of 2 chars to assign to bytes, but what if I want to assign 3 bytes to an integer? How do I do this?

for VernonDozier - This again might be very basic, can I used unsigned char to represent any data-type? could you give an example...

Thank you so much.

I'm not 100% sure I understand the question. I had something like this in mind. How to store the packet and how to send it down the socket are potentially two different questions. You mention the header and trailer, as well as the data itself. I'm not sure what you are doing with the header and the trailer, what goes down the wire, and how. It occurs to me that you didn't even mention the word "socket", so I'm not positive that's what you have in mind, or what you intend to do with this packet after you have it set up in the program. Anyway, this what I had in mind with my last post:

struct header
{
       unsigned char offset[3];
       unsigned char forward[64];
};


struct message
{
       unsigned char* payload;
       int payloadSize;
};


struct trailer
{
       unsigned char* trailer1;
       unsigned char* trailer2;
       int trailer1size;
       int trailer2size;
       int trailersize;
};


struct packet
{
       header theHeader;
       message theMessage;
       trailer theTrailer;
};

See this link. It's a linux socket example. Windows will be different, but similar.

http://www.linuxhowtos.org/C_C++/socket.htm

http://www.linuxhowtos.org/manpages/2/write.htm
http://www.linuxhowtos.org/manpages/2/read.htm

If you look at the above two links, you'll note that the the "read" and "write" functions both use void* pointers (i.e. they couldn't care less what type of data is being transmitted). They also deal with the size of the array (write specifies it, read returns it). So anything you send needs to be a CONTIGUOUS array of bytes (unsigned char being a byte).

I'm not sure whether your question deals with how to turn a custom data type into an unsigned char array:

struct mydatatype
{
       int a[5];
       char b[16];
       bool c[6];
};

A data type like above would already stored in contiguous memory, so there would be nothing to convert. Some data types would not be contiguous though. If you are sending information down a socket though, you need to also consider things like "little endian" versus "big endian", so a conversion using the htonl function could be in order. The sizeof function could also be needed. You would want to take into consideration how different systems could store different types of data and create your void/unsigned char array accordingly.

So please elaborate on your question and specify a little more on what you're looking for, as well as how familiar you are with how data is stored in memory, how sockets work, etc., or even whether we're talking about sockets. Are these concepts brand new to you? Are you trying to write a function like this?

unsigned char* ConvertToBytes (mydatatype mdt, int& size);
// converts mdt to unsigned char array.  size = size of array

Like I said, my links are Linux-specific, but the functions in Windows is going to be similar.

That was really really helpful. Let me give a bigger picture of what I am doing.

I mainly have to design a FIFO buffer module. I need to create dummy packets which consist of header, data & trailer, to put into this buffer. On the recieving end there are threads or, as I name them, module chains. These chains process specific data, for example, there might be a security chain which takes in security data or a control chain which takes in control data and so on. The buffer queries the Management system for the connection id of the module chain to by providing it with the forwarding label contained in the header of the packet. After recieving the connection id the packet is sent to the correct module chain.

I am attaching a diagram to make it more clear. Of these I need to design the buffer module, the dummy packets, queries to send to the management system (I do not have to design the management system) and send the packets to the right module chains (again, I do not have to design te module chains, jjust send the packet to the appropriate one).

P.S.- Can I continue asking questions about my project on the same thread or do I start a new one?

That was really really helpful. Let me give a bigger picture of what I am doing.

I mainly have to design a FIFO buffer module. I need to create dummy packets which consist of header, data & trailer, to put into this buffer. On the recieving end there are threads or, as I name them, module chains. These chains process specific data, for example, there might be a security chain which takes in security data or a control chain which takes in control data and so on. The buffer queries the Management system for the connection id of the module chain to by providing it with the forwarding label contained in the header of the packet. After recieving the connection id the packet is sent to the correct module chain.

I am attaching a diagram to make it more clear. Of these I need to design the buffer module, the dummy packets, queries to send to the management system (I do not have to design the management system) and send the packets to the right module chains (again, I do not have to design te module chains, jjust send the packet to the appropriate one).

P.S.- Can I continue asking questions about my project on the same thread or do I start a new one?

Continue with this same thread until you feel that the original problem was solved or that the discussion has veered in such a way that the thread title no longer accurately describes your current problem (i.e. if the original question has been answered and you think you understand, but now have questions on how to spawn new threads, that's sort of a new topic). If that happens, the best bet is to mark the current thread "solved", then start a new thread with a paragraph that summarizes anything from the old thread that you feel is pertinent to the new topic, then ask the new question. And it never hurts to stick a link to the old thread in the new thread.

So it sounds like you're not using sockets , so the "little endian" vs. "big endian" issues, etc., don't apply.

Did my diagram help? Do you have anything to say about it as in in terms of the packet structure? Do you suggest I should use a class instead...I will write and post a dummy packet...you can make changes r corrections to it I guess...Thank you so much...

Did my diagram help? Do you have anything to say about it as in in terms of the packet structure? Do you suggest I should use a class instead...I will write and post a dummy packet...you can make changes r corrections to it I guess...Thank you so much...

Well, it helps somewhat, but it's one page of many in this assignment (I'm assuming this is a school assignment?) and I don't really have a feel for the whole assignment and what the functions are, what's a thread, what's a process, etc. I assume your packet that you need to create is something that you need to pass to a thread that takes a void* argument, but that's only a guess. I don't know what operating system you are using. Here's a link to a Linux thread tutorial example. If you're not using Linux, I'll stop posting this stuff.

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

I would actually figure out what's a thread, what's a process, how they are created, what functions are called and what those function's jobs are, which will dictate what those functions need. Presumably you have a bunch of information that you need to turn into a void* argument before you can pass it, and the function you pass it to needs to be able to extract that information from the void* parameter. So start by creating the function declarations and figuring out what each function needs, then build your packet around that, then find a way to turn that packet into an array of void that can be passed, and a way to get the packet from the void array. You'll have to really think about what should be a process and what should be a thread and issues like that. And again, what OS are you using? Figure out what you need and then work backwards into creating the packet around that. I imagine that one way or another it's going to be somewhere along the lines of the structs I posted. I can't offer much more advice than that because right now the problem is too general.

I am using Windows...I'll post back whatever I have figured out in a few days...I am not going to end this thread now, hope thats fine...I am going to use your idea of creating the structure though...Thanks a lot for everything...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.