Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket - Read Bytes and convert to String

843790Dec 2 2007 — edited Dec 5 2007
Hi All,

Need a help.

I wrote a java ServerSocket and client.

When client passes a string (i.e. �Hello world�) to server, server reads it correctly.

is = new BufferedReader(new InputStreamReader(clientSocket
					.getInputStream()));
is.readLine();
But in client side I need to do some simple encryption.

opStr = " Hello world ";

byte[] messageBytes = opStr.getBytes();
			
int s = 12;

byte b = (byte)s;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int i = 0;
			
for (i = 0; i < messageBytes.length; i++) {
				
		
messageBytes[i] = (byte)(messageBytes*b);

}


baos.write(messageBytes);
os.write(baos.toByteArray());
os.flush();
So please let me know, from server side, how can I read these response and construct the original string (i.e. �Hello world�).

How to read the byte stream?
Decrypt the code?
And construct the original string?

I tried following but not working as expected.
byte[] newByte = new byte[11];

while(((ch = is.read()) != -1))
{
newByte[cnt] = (byte)(ch);

}
String newStr = new String(newByte);
Edited by: galajava on Dec 2, 2007 5:45 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2008
Added on Dec 2 2007
8 comments
572 views