Re: Sending bytes and string over sockets

Discussion in 'Java' started by Gilles Dubochet, Aug 26, 2003.

  1. Hello,

    Have you tried wrapping the Socket's Output Stream in a
    java.io.DataOutputStream ?

    You have a bunch of lovely functions such as writeByte (and even
    writeShort to write two bytes directly), writeChars, etc.

    I think you can use this class to write first the length using writeByte
    or writeShort, and then go on with your String using writeChars.

    Of course, there are other solutions, such as using String's getBytes
    function to get a byte array, concatenate it with the two bytes for the
    length and then use a basic write(byte[] b) method on the socket
    OutputStream.

    I think the first solution is more pretty. I hope it will do it.

    Best Regards, Gilles Dubochet.

    > 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
     
    Gilles Dubochet, Aug 26, 2003
    #1
    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,158
    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,425
    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,245
    Steve Horsley
    Jun 28, 2003
  4. Steve Horsley
    Replies:
    4
    Views:
    1,480
    Jon A. Cruz
    Aug 29, 2003
  5. Don
    Replies:
    4
    Views:
    1,473
    Roedy Green
    Oct 26, 2003
Loading...

Share This Page