Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

htons, ntohs in Java?

2,393 views
Skip to first unread message

belof...@my-dejanews.com

unread,
Nov 27, 1998, 3:00:00 AM11/27/98
to
Hello!

I want to send via my socket integer 2 and 4 byte parameters. Before sending I
want to
convert them from "host" to "network" representation (for Intel architecture
it means changing bytes order) -- in fact I am looking for Java anlogs for C
ntohs and ntohs functions.

Any point in this direction would be very appreciated!

Thanks in advance, Nickolay.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Jim Menard

unread,
Nov 27, 1998, 3:00:00 AM11/27/98
to
belof...@my-dejanews.com writes:

If you are sending data between two Java applications, then this
translation is not necessary. Java guarantees that the byte orderings for
all intrinsics (integers, chars, longs, etc.) are the same.

If you are sending data from Java to a non-Java program, do the translation
on the other side. Only the other side knows what byte ordering it has. Let
it call ntohl() and htonl().

> Hello!
>
> I want to send via my socket integer 2 and 4 byte parameters. Before sending I
> want to
> convert them from "host" to "network" representation (for Intel architecture
> it means changing bytes order) -- in fact I am looking for Java anlogs for C
> ntohs and ntohs functions.
>
> Any point in this direction would be very appreciated!

Jim
--
Jim Menard ji...@io.com http://www.io.com/~jimm/ BeOS developer #1283
"Java: the elegant simplicity of C++ and the blazing speed of Smalltalk."
-- Roland Turner

William Kaufman

unread,
Nov 27, 1998, 3:00:00 AM11/27/98
to
In article <73md7o$3f8$1...@nnrp1.dejanews.com>,
<belof...@my-dejanews.com> wrote:
]
] I want to send via my socket integer 2 and 4 byte parameters. Before

] sending I want to convert them from "host" to "network" representation
] (for Intel architecture it means changing bytes order) -- in fact I am
] looking for Java anlogs for C ntohs and ntohs functions.

Use java.io.Data(Input/Output)Stream: those always load and store
numbers in network order. (If the other side of the connection is
C(++), of course, you should still use the those macros there.)

-- Bill K.

Bill Kaufman | " While not a master of intellect, the blatantly
wkau...@us.oracle.com | obvious things we take for granted never
| escape his keen eye! " -- Bob Burden

Craig West

unread,
Nov 28, 1998, 3:00:00 AM11/28/98
to
Jim Menard (ji...@dillinger.io.com) wrote:
+>belof...@my-dejanews.com writes:

+>If you are sending data between two Java applications, then this
+>translation is not necessary. Java guarantees that the byte orderings for
+>all intrinsics (integers, chars, longs, etc.) are the same.

+>If you are sending data from Java to a non-Java program, do the translation
+>on the other side. Only the other side knows what byte ordering it has. Let
+>it call ntohl() and htonl().

To be a little more clear, Java always uses Network byte order (whenever the
byte order is visible, at least).
This doesn't mean that the internal representations are kept in network order,
but there is NO way to expose the internal representation.

--
Craig West Ph: (905) 821-8300 | It's not a bug,
Pulse Microsystems Fx: (905) 821-7331 | It's a feature...
2660 Meadowvale Blvd, Unit #10 | acw...@echo-on.net
Mississauga, Ont., Canada L5N-6M6 | cr...@pulsemicro.com

0 new messages