2

Problem

I read first byte in my socket client to check connection:

ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN);
...
int dataInt = clientSocket.getInputStream().read();

Documentation:

Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the stream has been reached.

after that I want to splice this byte with next input string. I convert this integer byte to characters

b.putInt(dataInt);
byte[] dataByte = b.array();
final String data;

and check it. If dataInt != -1, I have to return this croped byte into new string:

if(dataInt != -1)
{
    String c = new String(dataByte);
    Log.v("MSG", c);
    data = c + inToServer.readLine();
}
else
{
    data = inToServer.readLine();
}

Why I see in log "MSG, ������MY MESSAGE"? How to get a string correctly?


Upd, how I send my messages:

byte[] buf = str.getBytes("UTF-8");
outToServer.write(buf, 0, buf.length);
outToServer.writeBytes("\n");
outToServer.flush();
3
  • In Java, characters are represented as two bytes. Although your file may or may not be storing them as 1 or 2. Something to consider. Maybe your b allocation should be 1 or 2, not 4. Commented Nov 11, 2012 at 20:25
  • So, how to splice this byte and bytes in next string? Commented Nov 11, 2012 at 20:29
  • How are your characters stored? as 4 byte integer values of a char? If this is the case, you shouldnt be creating a string from the byte array directly, but casting the int value to char. Commented Nov 11, 2012 at 20:32

4 Answers 4

2
if(dataInt != -1)
{
    String c = new String(dataByte, "UTF-8");
    Log.v("MSG", c);
    data = c + inToServer.readLine();
}
else
{
    data = inToServer.readLine();
}
Sign up to request clarification or add additional context in comments.

4 Comments

No, it does not work. V/data(30887): ������msg#Leo#kokoko E/AndroidRuntime(30887): FATAL EXCEPTION: main
@Leo His answer looks pretty solid to me. +1 for more in-depth. Are you sure the byte array you recieved is complete. And not anything additional. Send a small string, and see what numbrs are in the array, before and after.
Perhaps try new String(dataByte, "ISO-8859-1"); so you can at least see what bytes are coming back instead of the invalid unicode character ?'s
@Doomsknight Yes, I'm sure cause if I dont use clientSocket.getInputStream().read(), all my messages is complete. But so I cant't see my socket state.
1

Okay, so on your server side just do this with your recieved byte[] There is no need for byte buffers, or to manipulate it in any way.

String str = new String(recievedBytes); 

1 Comment

@Leo remove your byte buffer stuff, and read it directly into the new String(recievedBytes); method.
0

I would recommend you to use IOUtils.toString for this. It facilitates a lot of the boring and error prone input/output stream manipulation.

Here is an answer explaining how to setup your project to use apache IO commons.

Comments

0

Omg, I did it. Solution:

byte[] b1 = new byte[1];
int dataInt = clientSocket.getInputStream().read();
b1[0] = (byte)dataInt;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.