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();
Leo
  • 3,003
  • 5
  • 38
  • 61
  • 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. – IAmGroot Nov 11 '12 at 20:25
  • So, how to splice this byte and bytes in next string? – Leo Nov 11 '12 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. – IAmGroot Nov 11 '12 at 20:32

4 Answers4

2
if(dataInt != -1)
{
    String c = new String(dataByte, "UTF-8");
    Log.v("MSG", c);
    data = c + inToServer.readLine();
}
else
{
    data = inToServer.readLine();
}
gordy
  • 9,360
  • 1
  • 31
  • 43
  • No, it does not work. V/data(30887): ������msg#Leo#kokoko E/AndroidRuntime(30887): FATAL EXCEPTION: main – Leo Nov 11 '12 at 20:42
  • @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. – IAmGroot Nov 11 '12 at 20:44
  • 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 – gordy Nov 11 '12 at 20:49
  • @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. – Leo Nov 11 '12 at 20:52
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); 
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • @Leo remove your byte buffer stuff, and read it directly into the `new String(recievedBytes); ` method. – IAmGroot Nov 11 '12 at 20:48
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.

Community
  • 1
  • 1
renam.antunes
  • 761
  • 1
  • 5
  • 11
0

Omg, I did it. Solution:

byte[] b1 = new byte[1];
int dataInt = clientSocket.getInputStream().read();
b1[0] = (byte)dataInt;
Leo
  • 3,003
  • 5
  • 38
  • 61