Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Sending bytes and string over sockets

Thread Tools

Re: Sending bytes and string over sockets

Gilles Dubochet
Posts: n/a

Have you tried wrapping the Socket's Output Stream in a ?

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

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

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Re: Sending bytes and string over sockets Steve Horsley Java 4 08-29-2003 05:09 AM
Re: receiving Bytes and sending Bytes Ieuan Adams Computer Support 0 07-24-2003 07:46 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM
Re: sending objects over sockets Steve Horsley Java 0 06-27-2003 01:51 PM