Re: Sending bytes and string over sockets

Discussion in 'Java' started by Steve Horsley, Aug 26, 2003.

  1. On Mon, 25 Aug 2003 13:42:37 -0700, phantom wrote:

    > Hi all,
    >
    > I am trying to accept client connections (ILE RPG ) and the format
    > of the protocol is such that in my Java Server I should be sending the
    > data packet which is preceded by the hex value of its length. The
    > lenght wud be in two bytes. and the data wud follow then, the data has
    > to be ascii text.
    > So to send a string "hello" I would have the packet look like
    >
    > 1st byte = 0
    > 2nd byte = 5
    > 3-7 = hello
    >
    > where the first 2 bytes are the lengths of "hello" in hexadecimal
    > converted to bytes.
    >
    > Now my problem is the first 2 are sent over as bytes whereas the
    > remaining are simple strings, how can i concatenate the bytes and the
    > string and have the entire thing sent over as a string.
    > I think i am missing something
    > Do i need to use a special dataStructure ??? I am all confused please
    > help
    >
    > Sid


    The easy way is to wrap the OutputStream in a DataOutputStream. If the
    text is truly ASCII then this will work:

    myDataOutputStream.writeUTF("hello");

    but beware that this will fail if you send non-ASCII (codes above 127)
    because of the UTF8 conversion, in which case you need to do something
    like this:

    byte[] bytes = "hello".getBytes("8859-1");
    short len = (short) bytes.length();
    myDataOutputStream.writeShort(len);
    myDataOutputStream.write(bytes);

    Beware that this may fail if the other end is badly written and
    expects all the data in one packet - you may need to use a
    BufferedOutputStream round the OutputStream to collect the data, then call
    flush() when you've done all the write() calls.

    You may prefer to explicitly build the byte[] by hand using shift-and-mask
    rather than using DataOutputStream.

    Steve
    Steve Horsley, Aug 26, 2003
    #1
    1. Advertising

  2. Steve Horsley

    phantom Guest

    >> The easy way is to wrap the OutputStream in a DataOutputStream. If
    the
    > text is truly ASCII then this will work:
    >
    > myDataOutputStream.writeUTF("hello");
    >
    > but beware that this will fail if you send non-ASCII (codes above 127)
    > because of the UTF8 conversion, in which case you need to do something
    > like this:
    >
    > byte[] bytes = "hello".getBytes("8859-1");
    > short len = (short) bytes.length();
    > myDataOutputStream.writeShort(len);
    > myDataOutputStream.write(bytes);
    >
    > Beware that this may fail if the other end is badly written and
    > expects all the data in one packet - you may need to use a
    > BufferedOutputStream round the OutputStream to collect the data, then call
    > flush() when you've done all the write() calls.
    >
    > You may prefer to explicitly build the byte[] by hand using shift-and-mask
    > rather than using DataOutputStream.
    >
    > Steve


    Taking your advice I wrapped the output sockets with DataOutputStream
    and send the length of the string in hex as bytes using the
    write(byte[],offset, len) method, now when I try to send the string
    using the writeChars(str) method as is the method it sends a 2Byte per
    character that i send.

    heres the code :

    DataOutputStream dos = new
    DataOutputStream(connection.getOutputStream());
    dos.write(byteArray , 0 , 2); //byteArray has the hex lengths of
    strSend
    dos.writeChars(strSend);

    the writeChars is sending 2B per character i send so a packet like
    <?xml
    appears as <Nullchar?NullCharx.... and so on I read the docs for the
    writeChars class and it sends the 2Bytes irrespective. The client at
    the other end cud decipher the byteArray above that i send. Only prob
    is the string is getting messed up now. Any suggestions should i use
    another Output Stream...

    All help is appreciated
    phantom, Aug 27, 2003
    #2
    1. Advertising

  3. On Wed, 27 Aug 2003 07:34:28 -0700, phantom wrote:

    >
    > Taking your advice I wrapped the output sockets with DataOutputStream
    > and send the length of the string in hex as bytes using the
    > write(byte[],offset, len) method, now when I try to send the string
    > using the writeChars(str) method as is the method it sends a 2Byte per
    > character that i send.
    >
    > heres the code :
    >
    > DataOutputStream dos = new
    > DataOutputStream(connection.getOutputStream());
    > dos.write(byteArray , 0 , 2); //byteArray has the hex lengths of
    > strSend
    > dos.writeChars(strSend);


    I never suggested you use that!
    You could try this instead:
    dos.write(strSend.getBytes("ASCII"));

    > the writeChars is sending 2B per character


    Yup. That's what the javadocs say it does. Seems they're right.

    I'm beginning to wonder what the first two bytes should be. If the string
    is "hello", should they be binary data (0x00, 0x05), or should the be
    ASCII character codes "05" (0x30, 0x35)?

    Steve
    Steve Horsley, Aug 27, 2003
    #3
  4. Steve Horsley

    Roedy Green Guest

    On 27 Aug 2003 07:34:28 -0700, (phantom) wrote or
    quoted :

    >Taking your advice I wrapped the output sockets with DataOutputStream
    >and send the length of the string in hex as bytes using the
    >write(byte[],offset, len) method, now when I try to send the string
    >using the writeChars(str) method as is the method it sends a 2Byte per
    >character that i send.


    in an email you told me the target of this is RPG.

    It is better to tackle this from the RPG end, and figure out what sort
    of file it wants.

    RPG has surely evolved since I last used it.

    It may want 8-bit chars in some encoding.
    It may want little-endian binary.
    It may want big-endian binary.
    It may want 16-bit chars.

    Decide that first,then worry about how to make Java produce.

    See http://mindprod.com/fileio.html
    for sample code.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 28, 2003
    #4
  5. Steve Horsley

    Jon A. Cruz Guest

    phantom wrote:
    > Thanks all for your patience in answering me, the RPG was using 8-bit
    > chars and sending them encoded as 8 bits worked out for me.


    Be *sure* that you are using the correct 8-bit encoding. Java knows
    quite a few different ones.
    Jon A. Cruz, Aug 29, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Horsley

    Re: sending objects over sockets

    Steve Horsley, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,144
    Steve Horsley
    Jun 27, 2003
  2. Steve Horsley

    Re: sending objects over sockets

    Steve Horsley, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,413
    Steve Horsley
    Jun 27, 2003
  3. Knute Johnson

    Re: sending objects over sockets

    Knute Johnson, Jun 27, 2003, in forum: Java
    Replies:
    5
    Views:
    3,210
    Steve Horsley
    Jun 28, 2003
  4. Gilles Dubochet

    Re: Sending bytes and string over sockets

    Gilles Dubochet, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    455
    Gilles Dubochet
    Aug 26, 2003
  5. Don
    Replies:
    4
    Views:
    1,450
    Roedy Green
    Oct 26, 2003
Loading...

Share This Page