Solved

unsigned short in network byte order

Posted on 2004-09-15
8
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12066867
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
ID: 12066910
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
ID: 12067947
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:grg99
ID: 12068244
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
 

Author Comment

by:cameron_schuler
ID: 12069575
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
ID: 12069726
OutBuf[0]    = Length >> 8;
OutBuf[1]    = Length & 0xxF;

0
 

Author Comment

by:cameron_schuler
ID: 12069903
Did you mean 0xFF instead of  0xxF?
0
 
LVL 22

Expert Comment

by:grg99
ID: 12071159
Right!    
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 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