Network Byte Order ...

Discussion in 'Java' started by Luke Skywalker, Nov 11, 2003.

  1. 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 !!!!! :)
    Luke Skywalker, Nov 11, 2003
    #1
    1. Advertising

  2. 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 << 8) + 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
    Gordon Beaton, Nov 11, 2003
    #2
    1. Advertising

  3. Gordon Beaton <> 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,
    | It's a feature...
    A. Craig West, Nov 11, 2003
    #3
  4. A. Craig West wrote:
    > Gordon Beaton <> 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
    Babu Kalakrishnan, Nov 12, 2003
    #4
  5. Gordon Beaton <> wrote in message

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


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

    best regards
    Luke Skywalker, Nov 12, 2003
    #5
  6. Gordon Beaton <> wrote in message

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


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

    best regards
    Luke Skywalker, Nov 12, 2003
    #6
  7. Gordon Beaton <> wrote in message

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


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

    best regards
    Luke Skywalker, Nov 12, 2003
    #7
  8. Luke Skywalker

    nos Guest

    is it possible theoretically that c2 is a negative number?


    "Gordon Beaton" <> wrote in message
    news:3fb098ba$...
    > 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 << 8) + 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
    nos, Nov 12, 2003
    #8
  9. 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) << 8) | (c2 & 255);

    Yuk! signed bytes.

    Steve
    Steve Horsley, Nov 12, 2003
    #9
  10. 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) << 8) | (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
    Babu Kalakrishnan, Nov 13, 2003
    #10
  11. Babu Kalakrishnan wrote:
    > 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) << 8) | (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


    Oops! You are right of course. Thank you.

    Steve
    Steve Horsley, Nov 13, 2003
    #11
  12. Luke Skywalker

    nos Guest

    so the answer is yes, c2 can be negative

    "Steve Horsley" <> wrote in message
    news:bp0lp0$g6h$2surf.net...
    > Babu Kalakrishnan wrote:
    > > 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) << 8) | (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

    >
    > Oops! You are right of course. Thank you.
    >
    > Steve
    >
    nos, Nov 14, 2003
    #12
  13. nos wrote:
    > so the answer is yes, c2 can be negative
    >

    As Babu pointed out, it depends where c1 and c2 come from.
    InputStream.read() returns an integer between 0 and 255, so they can not
    be negative (except for the -1 that indicates EOF).

    However, if you use InputStream.read(byte[]) then you end up with an
    array of bytes with values in the range -128 to +127.

    If you intend to use read(), it's probably best to wrap the InputStream
    in a BufferedInputStream to reduce the number of O/S level I/O calls you
    make.

    Steve
    Steve Horsley, Nov 14, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse Engle

    network byte order

    Jesse Engle, Feb 27, 2004, in forum: C Programming
    Replies:
    2
    Views:
    520
    Dan Pop
    Mar 1, 2004
  2. DaBeef
    Replies:
    0
    Views:
    339
    DaBeef
    Nov 2, 2005
  3. Evan
    Replies:
    4
    Views:
    760
    Mark Tolonen
    Mar 5, 2009
  4. Robert Evans
    Replies:
    7
    Views:
    281
    Joel VanderWerf
    Nov 15, 2005
  5. Replies:
    11
    Views:
    358
Loading...

Share This Page