Solved

unsigned short in network byte order

Posted on 2004-09-15
8
348 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

803 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