Solved

unsigned short in network byte order

Posted on 2004-09-15
8
326 Views
Last Modified: 2010-04-15
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
Comment
Question by:cameron_schuler
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
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
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
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
 

Author Comment

by:cameron_schuler
Comment Utility
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
 
LVL 22

Expert Comment

by:grg99
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:cameron_schuler
Comment Utility
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
 
LVL 22

Accepted Solution

by:
grg99 earned 125 total points
Comment Utility
OutBuf[0]    = Length >> 8;
OutBuf[1]    = Length & 0xxF;

0
 

Author Comment

by:cameron_schuler
Comment Utility
Did you mean 0xFF instead of  0xxF?
0
 
LVL 22

Expert Comment

by:grg99
Comment Utility
Right!    
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now