Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:25:32+00:00 2026-05-28T03:25:32+00:00

I have troubles with my program when i need to send Strings from my

  • 0

I have troubles with my program when i need to send Strings from my server bluetooth-socket to my client bluetooth-socket.
Everything works fine as long as I am only sending one String at a time (for example chatting) but if I need to write more Strings at a short period of time (to interchange informations), the Strings will not get seperated from the client code. For example if I’m sending “FirstUser” and right after that “SecondUser” the client does not read “FirstUser” and then “SecondUser”. It will read “FirstUserSecondUser”. How can I avoid this behaviour?

Edit: If I let the Thread sleep before it is able to send a new message, it reads the right strings but this solution is not working 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 a 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: reading 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: writing 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 🙂 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:25:33+00:00Added an answer on May 28, 2026 at 3:25 am

    You need to encapsulate your data item to avoid concatenation.
    It means that you have to write and read a whole data item before continuing.

    You should have some utility methods to do that instead of directly using methods of OutputStream and InputStream :

    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 both for server & client to read and write your String items.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client
I'm writing an MPI C program. I have troubles debugging it, because whenever I
I have a CIImage I need to convert from color to Black and White
I have been thinking about building a client to client program. But the way
I have this code from Learn Python The Hard Way and I need to
I'm writing a quiz program. I have a 1-Dimensional array of correct answers from
I have trouble deallocating memory that I allocated using malloc. The program runs fine
I need some guidance. I have to create a simple program, that captures still
Interview question- Often its pretty easier to debug a program once you have trouble
I have a lot of trouble with the internet connectivity in the program I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.