Trouble with 4 byte integer value


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Trouble with 4 byte integer value

  1. #1
    Join Date
    Apr 2004
    Posts
    3

    Trouble with 4 byte integer value

    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. 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

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Apr 2004
    Posts
    3
    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.

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    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:

    InputStream is = insocket.getInputStream()
    OutputStream os = outsocket.getOutputStream();

    byte[] buffer = new byte[4096];
    for(int bRead = is.read(buf); bRead>-1; bRead=is.read(buffer)){
    os.write(buf; 0; bread);
    }

    ......
    ps; i know it doesnt compile
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    note that OOS/ OIS are intended for writing java objects so they may add headers (i forgot about this till now)

    if you want to use a DataOutputStream, that's no problem.. just replace OOS with DOS in my post above...

    ps; DataInputStream is an input class.. it wont have a writeInt.. writing is an output function...
    so:

    String message = "hello"
    byte[] msg = message.getBytes();
    int len = msg.length;

    DataOutputStream.writeInt(len);
    DataOutputStream.write(msg);

    --

    and at the other end:

    int len = DataInputStream.readInt();
    byte[] msg = new byte[len];
    DataInputStream.read(msg);
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Apr 2004
    Posts
    3
    I didn't check back at the forums til now, and what do you know?... your last post is exactly how I implemented it. Glad to know that I actually did it correctly

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center