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 8580677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:52:44+00:00 2026-06-11T20:52:44+00:00

I am connecting two Android Devices P2P using the following code. The problem is

  • 0

I am connecting two Android Devices P2P using the following code. The problem is that when the server receives data from client its Thread/Runnable doesn’t stop.

Please suggest me a way to stop/forced this to go in an infinite loop.

Following Is the piece of code I am using for my Server Activity :

       public class ServerThread implements Runnable {
            private volatile boolean keepGoing = true;

               public void requestStop() {
                   keepGoing = false;
                }
               public void run() {
                   try {
                       if (SERVERIP != null) {
                           handler.post(new Runnable() {
                               @Override
                               public void run() {
                      serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new            
                             InputStreamReader(client.getInputStream()));
                        line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    serverStatus.setText(line);


                                    Log.d("IP recevied", line);
                                    requestStop();
                                    //finish();

                                    //Intent i = new Intent(ServerActivity.this, 
                                        //ClientActivity.class);
                                    //startActivity(i);
                                    //serverStatus.setText(line);
                                    // do whatever you want to the front end
                                    // this is where you can be creative
                                }
                            });
                        }

                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. 
                                        Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
                       }
                              } catch (Exception e) {
                                  handler.post(new Runnable() {
                                     @Override
                           public void run() {
                    serverStatus.setText("Error");
                           }
                       });
                       e.printStackTrace();
                   }
               }
           }

EDIT:

   while (keepGoing) {
         // listen for incoming clients
         Socket client = serverSocket.accept();
         handler.post(new Runnable() {
             @Override
             public void run() {
                 serverStatus.setText("Connected.");
             }
         });

         try {
             BufferedReader in = new BufferedReader(new            
                  InputStreamReader(client.getInputStream()));
             line = null;
             while (((line = in.readLine()) != null) && keepGoing) {
                 Log.d("ServerActivity", line);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {

                         serverStatus.setText(line);


                         Log.d("IP recevied", line);
                         requestStop();
                         //finish();

                         //Intent i = new Intent(ServerActivity.this, 
                             //ClientActivity.class);
                         //startActivity(i);
                         //serverStatus.setText(line);
                         // do whatever you want to the front end
                         // this is where you can be creative
                     }
                 });
             }
  • 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-11T20:52:45+00:00Added an answer on June 11, 2026 at 8:52 pm

    I think the problem is that even though you signal the server to stop, it’s still stuck in the call to serverSocket.accept();, waiting for other clients to connect. What you should do is also close the socket in the stop method:

    public void requestStop() {
          keepGoing = false;
          serverSocket.close();
    }
    

    to force the code to throw an IOException and then recheck the while condition and exit.

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

Sidebar

Related Questions

I'm attempting to write an app that involves connecting two android devices via bluetooth.
The HTTP 1.1 RFC restricts a Client from using more than Two TCP connections
I'm sending sensor data from an android device to a TCP server in C#.
I am using com.android.internal.telephony API's. In that I called two abstract classes they are
I'm wanting to use DatagramSocket and DatagramPacket to talk between two Android devices. Looking
I've decided to implement my own interface for connecting two devices running my game,
I'm consuming a web service from an Android device using HTTP(s) with SSL. A
I'm having trouble connecting a socket from my android phone to my computer running
I am making a game for Android, in which two devices connect and then
I am currently developing an android application which requires two devices to communicate over

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.