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

  • Home
  • SEARCH
  • 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 8300237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:28:57+00:00 2026-06-08T16:28:57+00:00

I’m working on a test project, something like a basic chat program using wi-fi

  • 0

I’m working on a test project, something like a basic chat program using wi-fi connection.
I’m creating sockets to connect two different devices, but my problem is that when I send the first message, it’s showing in the other device. But if I try to send again, I can see in the logs from the first one, that the message is sent, but it never shows up in the second device.

I’ve tried to implement the reading of the received data in another thread..or in Async Task, but the problem is still there. Here are both ways of my implementation :

Single Thread :

public void listenForSocket(){
    thread =  new Thread(new Runnable() {
        public void run() {
            Log.e("READDATAFROMSOCKET","READDATAFROMSOCKET");
            try {
                // sets the service running state to true so we can get it's state from other classes and functions.                     
                serverSocket = new ServerSocket(DNSUtils.port);
                client = serverSocket.accept();
                client.setKeepAlive(true);
                InputStream is = client.getInputStream();
                Log.d("","is Size : "+is.available());
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                int readed = in.read();
                Log.d("","readed bytes : "+readed);
                String line = "";
                while ((line = in.readLine()) != null) {
                    Log.i("","line : "+line);
                    changeText(line);
                }

                //client.close();
                //serverSocket.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

And here is AsyncTask :

class ServerTask extends AsyncTask<Void, Void, Void>{

    private String line = "";

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Log.e("ASYNCTASK","ASYNCTASK");
            // sets the service running state to true so we can get it's state from other classes and functions.                     
            serverSocket = new ServerSocket(DNSUtils.port);
            client = serverSocket.accept();
            client.setKeepAlive(true);
            InputStream is = client.getInputStream();

            Log.d("","is Size : "+is.available());
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            int readed = in.read();
            Log.d("","readed bytes : "+readed);

            while ((line = in.readLine()) != null) {
                Log.i("","line : "+line);                   
            }

            //client.close();
            //serverSocket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        changeText(line);
    }
}

changeText(String); –

private void changeText(final String line) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            LinearLayout.LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.RIGHT;
            TextView sendMsg = new TextView(MainActivity.this);
            sendMsg.setText(DNSUtils.clientName+" : "+line);
            sendMsg.setTextColor(Color.DKGRAY);
            sendMsg.setTextSize(18);
            layout.addView(sendMsg, params);
        }
    });
}

Any ideas how to fix this issue?

And another problem is that when I am reading the received data, the first letter of the sent string never shows. It always starts from the second letter.

Thanks in advance.

  • 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-06-08T16:29:03+00:00Added an answer on June 8, 2026 at 4:29 pm

    If i were you i will try to implement serverSocket = new ServerSocket(DNSUtils.port); only once with new; not in every thread.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have two tables with like below codes: Table: Accounts id | username |
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.