Convert user defined structure to char array for network transmittal (code included)

DB74
DB74 used Ask the Experts™
on
/*
Convert user defined structure to char array for network transmittal (code included)
*/

struct sPDUHeader{
   char ProtocolVersion;               //1 byte
   char ExerciseID;                    //1 byte
   char PDUTypeVar;                    //1 byte
   char Padding1;                      //1 byte
   unsigned long int TimeStamp;        //4 byte
   unsigned short int Length;          //2 byte
   unsigned short int Padding2;        //2 byte
};

struct sPDUHeader spduheader;          //12 byte

time_t ltime;
spduheader->ProtocolVersion=9;          //1byte,   9
spduheader->ExerciseID=1;               //1 byte,  1
spduheader->PDUTypeVar=16;              //1 byte,  16
spduheader->Padding1=0;                 //1byte,   1
spduheader->TimeStamp=time(&ltime);     //4 byte,  TTTTTT
spduheader->Length=1;                   //2 byte,  1818
spduheader->Padding2=1;                 //2 byte,  11

/**** How do I go about copying the structure into a char
array or sending the array for example UDP connection.
****/

//send(conn_socket,&spduheader,sizeof(spduheader),0);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
//The following code seems to work. Need a reality check.

char aPDUHeader[100];

memcpy(aPDUHeader,&spduheader,sizeof(spduheader));

printf("aPDUHeader: %d",aPDUHeader[0]);
printf("aPDUHeader: %d",aPDUHeader[1]);
printf("aPDUHeader: %d",aPDUHeader[2]);
printf("aPDUHeader: %d",aPDUHeader[3]);
         
send(conn_socket,aPDUHeader,sizeof(aPDUHeader),0);
Top Expert 2012
Commented:
What about simply using

send(conn_socket, (char*) &spduheader,sizeof(spduheader),0);

?

Commented:
If you are transmitting between machines that use different integer formats you will also need to use:

ntohl()
htonl()
ntohs()
htons()

These functions convert integers to/from network byte ordering and host byte ordering.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
jkr,
That explains the casting error I was getting. I was not sure I could cast to the entire structure. Lesson learned.
Thanks

zebada,
Thank you for the reminder. I have read that the byte order will need to be swapped, but concerned that nto* functions do not always work. The systems involved are a couple of windows systems and a couple of unix systems. Have you ever had any problems with the nto* functions not performing as expected? Just wanting to understand if there really is an issue with using the nto* functions.

Author

Commented:
jkr,
That explains the casting error I was getting. I was not sure I could cast to the entire structure. Lesson learned.
Thanks

zebada,
Thank you for the reminder. I have read that the byte order will need to be swapped, but concerned that nto* functions do not always work. The systems involved are a couple of windows systems and a couple of unix systems. Have you ever had any problems with the nto* functions not performing as expected? Just wanting to understand if there really is an issue with using the nto* functions.
jkr
Top Expert 2012

Commented:
>>Just wanting to understand if there really is an issue with using the nto* functions

There isn't - the rule of thumb is:

- convert all outgoing data using 'hton*()'
- convert all incoming data using 'ntoh*()'

Author

Commented:
Grade: A+
Took the time to address additional concerns.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial