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. The rest of the message will be ASCII". So in trying to keep with reading 4 bytes, i'm getting the length of the message and then trying to convert the length into a 4 byte integer 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

04-08-2004, 08:24 AM

cjard

if youre writing your chat client and server in java, why would you care about the network transmission?

just read a full string in from the user's keyboard with a bufferedreader.readline

then use a printwriter connected to a socket, to println it up to the server

the server readln's it using a buffered reader (connected to the socket input), then he prinlns it to all interested clients (via socket output), who then read it using a buffered reader (via socket input)

if at any point youre desperate to know the lenght of the string you jsut read, then String has a length() method....

be aware that if youre not using the java.nio package then you MUST use multiple threads for your programs both client and server
each client will have at least 2 threads - one for transmitting what the user typed and one for reading back what the server sends. DO NOT attempt to do this with one thread. You WILL end up with that thread locked up waiting for a response from something, when something else is trying to do its work

the server will have 2 threads per connected client, plus a thread to create threadpairs

04-08-2004, 09:29 AM

cc166

Yeah.. that's how I was doing the assignment... using a BufferedReader and readln.

But that's not how the prof wanted it. I asked him and basically he wants to send the length of the message as an integer using say a DataInputStream's writeInt method. Then on the server side use readInt. After getting the length use read(byte [] b) to read in the subsequent message.

Kind of a pain, but oh well.

Thanks for your input.

04-08-2004, 09:52 AM

cjard

okay, so use an ObjectOutputStream:

ObjectOutputStream oos = new OOS(socket.getOutputStream);

String message="hello!";
byte[] msgBYtes = message.getBytes();

oos.writeInt(msgBytes.length);
oos.write(msgBytes)

you could also use writeBYtes(message) as that converts a string and writes it

04-08-2004, 09:56 AM

cjard

and on the server end read it in with ObjectInputStream

if the server is merely relaying to other users, dont even both with that.. just read the socket as raw bytes and write to the destination socket: