• C

unsigned short in network byte order

I need to two byte message length indicator at the beginning of a message I'm sending to a server.  The message length will be an unsigned short in network byte order.

The following is how I plan on doing it:
-------------
snprintf(pBuffer, sizeofbuffer, "%u", htons(sizeof(ReqMsgTemp)));
-------------

RegMsgTemp is the message I'm sending to the server.  pBuffer will be prepended to ReqMsgTemp.

Is this the right way of doing it?
cameron_schulerAsked:
Who is Participating?
 
grg99Connect With a Mentor Commented:
OutBuf[0]    = Length >> 8;
OutBuf[1]    = Length & 0xxF;

0
 
cjjcliffordCommented:
is the message you are sending an ASCII message? If not, you'll probably want a "unsigned char[]" for the message, and cast the first 2 bytes to a "unsigned short", assigning that the return value of the htnos() call.

// Something like....
unsigned char msg[512];
*((unsigned short*)msg) = htons(...);
 
Cheers,
C
0
 
cjjcliffordCommented:
also, sizeof(buffer) returns the compile time size,

e.g.

unsigned char buf[1024];
int a = sizeof(buf); // "a" will contain 1024.

What you probably want is strlen() assuming ascii message, or some other way of calculating the length of the buffer, unless you are sending a fixed size buffer, in which case you mightn't have to send the size, since if its fixed, it can be fixed at both ends...

Cheers,
C.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
cameron_schulerAuthor Commented:
Yes it is an ascii message.  Asumming that, will it work if I prepend pBuffer in front of an ascii message that I am sending?
0
 
grg99Commented:
You've got two problems, getting the message size, and formatting it.

If the message you're sending is a typical "C" zero-terminated string, you want to do a strlen( TheStr ) to get the length.
You *could* use sizeof( TheStr ), but that's always going to be either the array size, which is wastefully large,
or if TheStr is a pointer to the string, it will be wrong (4).

Once you've got the size, you have to decide how to send it.  Sending it as printable characters has its advantages, you can do this with snprintf() as in your example, but DONT do anything like htons(), as the number is already in proper order.

If you want to send it as raw binary, which has a minor speed advantage, you do have to do something like htons() to get the bytes in the right order.  But then DONT do a "%u" conversion, that will convert it to ASCII which isnt what you want.  Just stuff it into the first two bytes of the sending array.



0
 
cameron_schulerAuthor Commented:
I understand the sizeof strlen problem.

This message length indicator has to be two bytes/characters long.  

For example if the length of the message is 6 bytes then <0x00><0x06> will prefix the ascii message.  How do I translate an unsigned short into 2 characters/bytes?
0
 
cameron_schulerAuthor Commented:
Did you mean 0xFF instead of  0xxF?
0
 
grg99Commented:
Right!    
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.