[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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?
0
cameron_schuler
Asked:
cameron_schuler
  • 3
  • 3
  • 2
1 Solution
 
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
 
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
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
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
 
grg99Commented:
OutBuf[0]    = Length >> 8;
OutBuf[1]    = Length & 0xxF;

0
 
cameron_schulerAuthor Commented:
Did you mean 0xFF instead of  0xxF?
0
 
grg99Commented:
Right!    
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now