Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Transmitting unsigned bytes over the network...

843790Dec 23 2009 — edited Dec 26 2009
Ok, so I'm aware that Java doesn't support unsigned data types. I'm aware the work around is to use a larger data type (in my case I'm using integers to store unsigned bytes). I'm in the process of writing a client that connects to a server and receives unsigned byte traffic as well as sends unsigned byte traffic to the server. I can send the information properly for the most part but the issue comes in every now and again when I'm trying to send the data back to the server.

When using a network monitoring tool, I can see that what is sent back is correct for the most part except the 3rd unsigned byte...

From the packet sniffer:
ff:cf:3f:05:00:30:38:30:31:63:63:35:36:61:35:38:39:38:32:64:36:61:33:39:66:38:64:64:66:38:31:65:38:37:63:66:36:00:02:00:00:00
The 3f should be 93. I know what I've done is right because when I print out my data before I send it, the 3f is still 93 (like it should be)
but as soon as it gets passed out to the OutputStreamWriter it gets mangled.

Heres how I'm sending the data:
	
        public void send(int[] bytes)
	{
		try {
			OutputStreamWriter outstream = new OutputStreamWriter(sock.getOutputStream());
			for(int x=0; x < bytes.length; x++)	{
				System.out.println("MSGOUT: 0x" + Integer.toHexString(bytes[x]));
				outstream.write(bytes[x]);
			}
			
			outstream.flush();
		} catch (IOException e) {
			System.err.println("An error occurred: " + e.getMessage());
			e.printStackTrace();
		}
	}
Here is the output from the System.out.println("MSGOUT: 0x" + Integer.toHexString(bytes[x]);
As you can see...before it goes out over the wire it's still 0x93 like its supposed to be.
MSGOUT: 0xff
MSGOUT: 0xcf
MSGOUT: 0x93
MSGOUT: 0x5
MSGOUT: 0x0
MSGOUT: 0x30
MSGOUT: 0x38
MSGOUT: 0x30
MSGOUT: 0x31
MSGOUT: 0x63
MSGOUT: 0x63
MSGOUT: 0x35
MSGOUT: 0x36
MSGOUT: 0x61
MSGOUT: 0x35
MSGOUT: 0x38
MSGOUT: 0x39
MSGOUT: 0x38
MSGOUT: 0x32
MSGOUT: 0x64
MSGOUT: 0x36
MSGOUT: 0x61
MSGOUT: 0x33
MSGOUT: 0x39
MSGOUT: 0x66
MSGOUT: 0x38
MSGOUT: 0x64
MSGOUT: 0x64
MSGOUT: 0x66
MSGOUT: 0x38
MSGOUT: 0x31
MSGOUT: 0x65
MSGOUT: 0x38
MSGOUT: 0x37
MSGOUT: 0x63
MSGOUT: 0x66
MSGOUT: 0x36
MSGOUT: 0x0
MSGOUT: 0x2
MSGOUT: 0x0
MSGOUT: 0x0
MSGOUT: 0x0
Why just this particular byte? I send out 0xFF at the start of the packet which is way out of bounds for signed bytes, and 0xCF, HELP, PLEASE!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 23 2010
Added on Dec 23 2009
19 comments
240 views