Bluetooth connection cannot send strings correctly

I have problems with my program when I need to send strings from my bluetooth socket server to my client bluetooth socket. Everything works fine, while I only send one line at a time (for example, chatting), but if I need to write more lines in a short period of time (to exchange information), the lines will not be separated from the client code, For example, if I send " FirstUser, "and immediately after that," SecondUser, "the client does not read" FirstUser, "and then" SecondUser. " He will read "FirstUserSecondUser". How can I avoid this behavior?

Edit: if I allow Thread sleep before it can send a new message, it reads the correct lines, but this solution does not work fine for my need.

Server code: sending to all clients (edited)

public synchronized void sendToAll(String message) { try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } publishProgress(message); for(OutputStream writer:outputList) { try { writer.write(message.getBytes()); writer.flush(); } catch (IOException e) { System.out.println("Some-Error-Code"); } } } 

Server code: reading from client:

  public void run() { String nachricht; int numRead; byte[] buffer = new byte[1024]; while (runningFlag) { try { if((numRead = inputStream.read(buffer)) >= 0) { nachricht = new String(buffer, 0, numRead); serverThread.handleMessage(nachricht); } } catch (IOException e) { this.cancel(); e.printStackTrace(); } } } 

Client code: read from server (edited)

 @Override protected Void doInBackground(Integer... ints) { String nachricht = new String(); byte[] buffer = new byte[1024]; int numRead; while (runningFlag) { try { if(((numRead = inputStream.read(buffer)) >= 0)) { nachricht = new String(buffer, 0, numRead); publishProgress(nachricht); } } catch (IOException e) { clientGame.finish(); e.printStackTrace(); } } return null; } 

Client code: write to server

 public synchronized void write(String nachricht) { try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } try { outputStream.write(nachricht.getBytes()); outputStream.flush(); } catch (IOException e) { this.cancel(); e.printStackTrace(); } } 

I appreciate every little help :).

+6
source share
1 answer

You need to encapsulate the data element to avoid concatenation. This means that you must write and read the entire data element before continuing.

You should have some utility methods for this, and not directly using the OutputStream and InputStream methods:

 public static void writeItem(OutputStream out, String s) throws IOException { // Get the array of bytes for the string item: byte[] bs = s.getBytes(); // as bytes // Encapsulate by sending first the total length on 4 bytes : // - bits 7..0 of length out.write(bs.length); // modulo 256 done by write method // - bits 15..8 of length out.write(bs.length>>>8); // modulo 256 done by write method // - bits 23..16 of length out.write(bs.length>>>16); // modulo 256 done by write method // - bits 31..24 of length out.write(bs.length>>>24); // modulo 256 done by write method // Write the array content now: out.write(bs); // Send the bytes out.flush(); } public static String readItem(InputStream in) throws IOException { // first, read the total length on 4 bytes // - if first byte is missing, end of stream reached int len = in.read(); // 1 byte if (len<0) throw new IOException("end of stream"); // - the other 3 bytes of length are mandatory for(int i=1;i<4;i++) // need 3 more bytes: { int n = in.read(); if (n<0) throw new IOException("partial data"); len |= n << (i<<3); // shift by 8,16,24 } // Create the array to receive len bytes: byte[] bs = new byte[len]; // Read the len bytes into the created array int ofs = 0; while (len>0) // while there is some byte to read { int n = in.read(bs, ofs, len); // number of bytes actually read if (n<0) throw new IOException("partial data"); ofs += n; // update offset len -= n; // update remaining number of bytes to read } // Transform bytes into String item: return new String(bs); } 

Then you use these methods for both the server and the client to read and write your String elements.

+8
source

Source: https://habr.com/ru/post/905264/


All Articles