The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with toHexString

3 replies on 1 page. Most recent reply: Apr 6, 2004 3:12 PM by Alex Blewitt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
cathy

Posts: 2
Nickname: cc166
Registered: Apr, 2004

Help with toHexString Posted: Apr 5, 2004 7:39 PM
Reply to this message Reply
Advertisement
Hi all,

I'm trying to write a client server program in which each request/response message from the client to the server and back will contain the length of the message. I've heard that the best way to do this is to convert the int containing the length of the message to ASCII (making it platform independent), get the length on the server side, and then convert the ASCII back to an int.

The thing about this assignment that confuses me is that it says "all request and response messages will be preceeded by a 4 byte integer value written in network byte order to determine the length of the subsequent message". So in trying to keep with reading 4 bytes, i'm trying to convert the length into 4 bytes so that when i read it on the server side i can use an InputStream and do something like in.read(b) where b = byte[4].

Is there a way to do this? Did that make sense at all? Sorry if it didn't... I'm a bit confused myself.

Thanks for any input.


cathy

Posts: 2
Nickname: cc166
Registered: Apr, 2004

Re: Help with toHexString Posted: Apr 5, 2004 7:54 PM
Reply to this message Reply
Forgot to put that the rest of the message will be ASCII text

Kyle Cacciatore

Posts: 1
Nickname: kyle
Registered: Jan, 2003

Re: Help with toHexString Posted: Apr 6, 2004 12:45 PM
Reply to this message Reply
The two are different solutions to the same problem, so I think you've confused yourself a bit. Just calculate the size of the message, convert it to NBO and ship it out, followed by the message. If I may make a suggestion, add a header or footer to the message, so you can ensure that the message is delivered in its entirety by reading the length as sent in, and making sure that the start and end pattern exist.

Alex Blewitt

Posts: 44
Nickname: alblue
Registered: Apr, 2003

Re: Help with toHexString Posted: Apr 6, 2004 3:12 PM
Reply to this message Reply
You could also use a DataInputStream, which allows the construction/deconstruction of such data on the fly (and it uses Network Byte Order too).


DataInputStream dis = new DataInputStream(in);
length = dis.readInt(); // length of data; int=32bits=4bytes
byte[] buffer = new byte[length];
dis.readFully(buffer); // contents of data


Then do whatever you want with the buffer. Of course, if you want to stream it in, you can always read into a partial array.

If the data is always ASCII, then you can use

dis.readUTF()

which will read in the data as a string; though I don't know what terminator it will use. I think it will default to UTF-8 (of which ASCII is a subset), but it might use UTF-16, which won't work.

Lastly, if you know it's always going to be text, you could read it into a byte buffer first, then copy each letter across into a char array instead, and then use something like the new String(chararray) constructor.

Flat View: This topic has 3 replies on 1 page
Topic: Exception in rmi based chat Previous Topic   Next Topic Topic: java directory tree

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use