Tuesday, May 22, 2012

Java network byte order issue

Java uses a DataOutputStream which has a method writeShort() (and writeIntwriteLong, etc.) which automatically write in network byte order ( the high-order byte first, followed by the next highest-order byte and so on, with the low order byte sent last).


// Input and output streams for TCP socket
protected DataInputStream in;protected DataOutputStream out;

protected Socket connect (int port) throws IOException
{
// Connect method
output.appendText ("\nConnecting to " + server + " Port " + port + "\n");
Socket socket = 
new Socket (server, port);
OutputStream rawOut = socket.getOutputStream ();
InputStream rawIn = socket.getInputStream ();
BufferedOutputStream buffOut = new BufferedOutputStream (rawOut);
out = 
new DataOutputStream (buffOut);
in = 
new DataInputStream (rawIn);return socket;
// END connect

No comments:

Post a Comment