Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Sending bytes and string over sockets

 
 
Steve Horsley
Guest
Posts: n/a
 
      08-26-2003
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

 
Reply With Quote
 
 
 
 
phantom
Guest
Posts: n/a
 
      08-27-2003
>> 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
 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      08-27-2003
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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-27-2003
On 27 Aug 2003 07:34:28 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Jon A. Cruz
Guest
Posts: n/a
 
      08-29-2003
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.

 
Reply With Quote
 
 
 
Reply

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 Gilles Dubochet Java 0 08-26-2003 11:16 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



Advertisments