Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Network Byte Order ...

Reply
Thread Tools

Network Byte Order ...

 
 
Luke Skywalker
Guest
Posts: n/a
 
      11-11-2003
hi all

I have to read a network packet, coded by a C program, where
the first two bytes codes the packet type, the following four
the packet length and then the payload. My problem is that the
lengths are integer coded (by the C program) into the network
order (using the htons() and htonl() functions). How can I perform
the decoding form Java? I.e. I read the first two byte:

int c1 = in.read();
int c2 = in.read();

How can I convert c1 and c1 into the packet length?

thanks in advance for ur reply !!!!!
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-11-2003
On 10 Nov 2003 23:56:27 -0800, Luke Skywalker wrote:
> My problem is that the lengths are integer coded (by the C program)
> into the network order (using the htons() and htonl() functions).


Network byte order is big endian. Use a DataInputstream; it has
methods to read various data types stored in big endian order.

Alternatively you can shift and add yourself, but this gets tedious:

int n = (c1 << + c2;

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
A. Craig West
Guest
Posts: n/a
 
      11-11-2003
Gordon Beaton <(E-Mail Removed)> wrote:
> On 10 Nov 2003 23:56:27 -0800, Luke Skywalker wrote:
>> My problem is that the lengths are integer coded (by the C program)
>> into the network order (using the htons() and htonl() functions).


> Network byte order is big endian. Use a DataInputstream; it has
> methods to read various data types stored in big endian order.


It is not a coincidence that 'network byte order' happens to coincide with
what Java uses in it's input and output. DataInputStream is the way to go,
and remember that all Java numeric types except char are signed. Java short's
are always 16 bits, int's are always 32 bits and long's are always 64 bits.

If your packet length is stored as a 16 bit value in network byte order, you
coulde read it like so:
try {
DataInputStream din = new DataInputStream(in);
short packetLength = din.readShort();
} catch (IOException ioe) {
// Do something to handle the exception
}

--
Craig West Ph: (416) 666-1645 | It's not a bug,
http://www.velocityreviews.com/forums/(E-Mail Removed) | It's a feature...
 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      11-12-2003
A. Craig West wrote:
> Gordon Beaton <(E-Mail Removed)> wrote:
>
>>On 10 Nov 2003 23:56:27 -0800, Luke Skywalker wrote:
>>
>>>My problem is that the lengths are integer coded (by the C program)
>>>into the network order (using the htons() and htonl() functions).

>
>
>>Network byte order is big endian. Use a DataInputstream; it has
>>methods to read various data types stored in big endian order.

>
>
> It is not a coincidence that 'network byte order' happens to coincide with
> what Java uses in it's input and output. DataInputStream is the way to go,
> and remember that all Java numeric types except char are signed. Java short's
> are always 16 bits, int's are always 32 bits and long's are always 64 bits.
>
> If your packet length is stored as a 16 bit value in network byte order, you
> coulde read it like so:
> try {
> DataInputStream din = new DataInputStream(in);
> short packetLength = din.readShort();
> } catch (IOException ioe) {
> // Do something to handle the exception
> }
>


Since packet lengths are generally unsigned, it would probably be better
to use :

int packetLength = din.readUnsignedShort();

BK

 
Reply With Quote
 
Luke Skywalker
Guest
Posts: n/a
 
      11-12-2003
Gordon Beaton <(E-Mail Removed)> wrote in message

> Alternatively you can shift and add yourself, but this gets tedious:
>
> int n = (c1 << + c2;
>
> /gordon


thanks for ur help, Gordon ... you have been very kind!

best regards
 
Reply With Quote
 
Luke Skywalker
Guest
Posts: n/a
 
      11-12-2003
Gordon Beaton <(E-Mail Removed)> wrote in message

> Alternatively you can shift and add yourself, but this gets tedious:
>
> int n = (c1 << + c2;
>
> /gordon


thanks for ur help, Gordon ... you have been very kind!

best regards
 
Reply With Quote
 
Luke Skywalker
Guest
Posts: n/a
 
      11-12-2003
Gordon Beaton <(E-Mail Removed)> wrote in message

> Alternatively you can shift and add yourself, but this gets tedious:
>
> int n = (c1 << + c2;
>
> /gordon


thanks for ur help, Gordon ... you have been very kind!

best regards
 
Reply With Quote
 
nos
Guest
Posts: n/a
 
      11-12-2003
is it possible theoretically that c2 is a negative number?


"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:3fb098ba$(E-Mail Removed)...
> On 10 Nov 2003 23:56:27 -0800, Luke Skywalker wrote:
> > My problem is that the lengths are integer coded (by the C program)
> > into the network order (using the htons() and htonl() functions).

>
> Network byte order is big endian. Use a DataInputstream; it has
> methods to read various data types stored in big endian order.
>
> Alternatively you can shift and add yourself, but this gets tedious:
>
> int n = (c1 << + c2;
>
> /gordon
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e



 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      11-12-2003
nos wrote:
> is it possible theoretically that c2 is a negative number?
>

Yes. You really need to mask the bytes as well:

int x = ((c1 & 255) << | (c2 & 255);

Yuk! signed bytes.

Steve

 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      11-13-2003
Steve Horsley wrote:
> nos wrote:
>
>> is it possible theoretically that c2 is a negative number?
>>

> Yes. You really need to mask the bytes as well:
>
> int x = ((c1 & 255) << | (c2 & 255);
>
> Yuk! signed bytes.
>


That's true if c1 and c2 are bytes. But in the specific case that was
being discussed, these were declared as type int and are the results of
the read() method from an InputStream - which always returns an int
between 0 and 255. (Of course an end of stream case can return -1, but
then that is a special check that needs to be done for handling errors)

BK



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert from network to host byte order Evan Python 4 03-05-2009 03:55 PM
unpack signed short in network (big-endian) byte order baumanj@gmail.com Ruby 11 04-10-2006 03:19 PM
Reading a signed byte in network byte order Robert Evans Ruby 7 11-15-2005 11:14 PM
Converting ip addr, port to unsigned Network Byte Order DaBeef Python 0 11-02-2005 06:54 PM
network byte order Jesse Engle C Programming 2 03-01-2004 04:53 PM



Advertisments