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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:42:57+00:00 2026-05-27T00:42:57+00:00

I am wanting to connect multiple android emulator clients to an android emulator server.

  • 0

I am wanting to connect multiple android emulator clients to an android emulator server. I am using the code from this post, i have changed the server a little bit. The code works great when only 1 client is attempting to connect. When i try to connect two clients to the server the second client’s messages do not seem to come through.

I was wondering what i need to do to achieve this goal? Do i need to redirect other ports or is there something i am missing in the code?

The logcat reports the following over and over again, it is stuck in the loop. It starts showing this error as soon as the first client is launched:

11-25 00:47:46.543: W/System.err(198):  at java.lang.Thread.run(Thread.java:1096)
11-25 00:47:46.543: W/System.err(198): java.net.BindException: The address is already in use
11-25 00:47:46.552: W/System.err(198):  at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
11-25 00:47:46.552: W/System.err(198):  at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
11-25 00:47:46.552: W/System.err(198):  at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
11-25 00:47:46.552: W/System.err(198):  at java.net.ServerSocket.<init>(ServerSocket.java:136)
11-25 00:47:46.552: W/System.err(198):  at java.net.ServerSocket.<init>(ServerSocket.java:87)
11-25 00:47:46.552: W/System.err(198):  at school.cpe434.ClassAidServer.ClassAidServer4Activity$CommsThread.run(ClassAidServer4Activity.java:64)
11-25 00:47:46.552: W/System.err(198):  at java.lang.Thread.run(Thread.java:1096)
11-25 00:47:46.552: W/System.err(198): java.net.BindException: The address is already in use
11-25 00:47:46.552: W/System.err(198):  at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
11-25 00:47:46.552: W/System.err(198):  at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
11-25 00:47:46.563: W/System.err(198):  at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
11-25 00:47:46.563: W/System.err(198):  at java.net.ServerSocket.<init>(ServerSocket.java:136)
11-25 00:47:46.563: W/System.err(198):  at java.net.ServerSocket.<init>(ServerSocket.java:87)
11-25 00:47:46.563: W/System.err(198):  at school.cpe434.ClassAidServer.ClassAidServer4Activity$CommsThread.run(ClassAidServer4Activity.java:64)

Here is the client (same as the other post):

public class ClassAidClient4Activity extends Activity {
   private Button bt;
   private TextView tv;
   private Socket socket;
   private String serverIpAddress = "10.0.2.2";
//   private String serverIpAddress = "192.168.1.102";
   // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
   // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
   // PORT 6000
   private static final int REDIRECTED_SERVERPORT = 5000;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      bt = (Button) findViewById(R.id.button1);
      tv = (TextView) findViewById(R.id.textView1);

      try {
         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
      } catch (UnknownHostException e1) {
         e1.printStackTrace();
      } catch (IOException e1) {
         e1.printStackTrace();
      }

      bt.setOnClickListener(new OnClickListener() {

         public void onClick(View v) {
            try {
               EditText et = (EditText) findViewById(R.id.editText1);
               String str = et.getText().toString();
               PrintWriter out = new PrintWriter(new BufferedWriter(
                     new OutputStreamWriter(socket.getOutputStream())),
                     true);
               out.println(str);
               Log.d("Client", "Client sent message");

            } catch (UnknownHostException e) {
               tv.setText("Error1");
               e.printStackTrace();
            } catch (IOException e) {
               tv.setText("Error2");
               e.printStackTrace();
            } catch (Exception e) {
               tv.setText("Error3");
               e.printStackTrace();
            }
         }
      });
   }
}

Here is the server (changed from other post a little):

public class ClassAidServer4Activity extends Activity {
   ServerSocket ss = null;
   String mClientMsg = "";
   Thread myCommsThread = null;
   protected static final int MSG_ID = 0x1337;
   public static final int SERVERPORT = 6000;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      TextView tv = (TextView) findViewById(R.id.textView1);
      tv.setText("Nothing from client yet");
      this.myCommsThread = new Thread(new CommsThread());
      this.myCommsThread.start();
   }

   @Override
   protected void onStop() {
      super.onStop();
      try {
         // make sure you close the socket upon exiting
         ss.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   Handler myUpdateHandler = new Handler() {
      public void handleMessage(Message msg) {
         switch (msg.what) {
         case MSG_ID:
            TextView tv = (TextView) findViewById(R.id.textView1);
            tv.setText(mClientMsg);
            break;
         default:
            break;
         }
         super.handleMessage(msg);
      }
   };
   class CommsThread implements Runnable {
      public void run() {
//         Socket s = null;

         while(true) {
             try {
                 ss = new ServerSocket(SERVERPORT );

                 Socket socket = ss.accept();
                 Thread lThread = new Thread(new ListeningThread(socket));
                 lThread.start();

              } catch (IOException e) {
                 e.printStackTrace();
              }
         }


      }
   }

   class ListeningThread implements Runnable {

       private Socket s = null;

       public ListeningThread(Socket socket) {
        // TODO Auto-generated constructor stub
           this.s = socket;
       }

       @Override
       public void run() {
           // TODO Auto-generated method stub
           while (!Thread.currentThread().isInterrupted()) {
                Message m = new Message();
                m.what = MSG_ID;
                try {
                   if (s == null)
                      s = ss.accept();
                   BufferedReader input = new BufferedReader(
                         new InputStreamReader(s.getInputStream()));
                   String st = null;
                   st = input.readLine();
                   mClientMsg = st;
                   myUpdateHandler.sendMessage(m);
                } catch (IOException e) {
                   e.printStackTrace();
                }
             }
       }

   }
}
  • 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-27T00:42:57+00:00Added an answer on May 27, 2026 at 12:42 am

    I found my problem.

    ss = new ServerSocket(SERVERPORT ); needed to be outside the while loop.

    If anyone sees any other problem, just let me know. Thanks

    class CommsThread implements Runnable {
          public void run() {
    //         Socket s = null;
    
            ss = new ServerSocket(SERVERPORT );
    
             while(true) {
                 try {
    
    
                     Socket socket = ss.accept();
                     Thread lThread = new Thread(new ListeningThread(socket));
                     lThread.start();
    
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
             }
    
    
          }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server to which multiple clients can connect to. The client is
I have the below code that i am wanting to into certain files so
Using Oracle 11 I'm wanting to pull data from a table, and add a
I am wanting to access a website from a different port than 80 or
I'm trying to figure out the best way to approach this. I have a
Not wanting this question to be too long, I will skip to an example:
Apple in iTunes Connect Developer Guide says: App Name Expiry Once you have created
Here is my problem: I cannot connect to serial port COM1 using ClipGem (Model:
I have downloaded the JTwain api and created/tested a java class to connect to
I have a new structure getting a date, then connect to a php file...

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.