多个仿真器客户端连接到仿真器服务器

时间:2011-11-25 06:52:39

标签: java android sockets emulation

我想将多个Android模拟器客户端连接到Android模拟器服务器。我正在使用this post中的代码,我已经更改了服务器。当只有一个客户端尝试连接时,代码工作得很好。当我尝试将两个客户端连接到服务器时,第二个客户端的消息似乎没有通过。

我想知道我需要做些什么来实现这个目标?我是否需要重定向其他端口,或者代码中是否缺少某些内容?

logcat一遍又一遍地报告以下内容,它被卡在循环中。一旦启动第一个客户端,它就会立即显示此错误:

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)

这是客户端(与其他帖子相同):

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();
            }
         }
      });
   }
}

这是服务器(从其他帖子稍微改了一下):

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();
                }
             }
       }

   }
}

2 个答案:

答案 0 :(得分:1)

我发现了我的问题。

ss = new ServerSocket(SERVERPORT );需要在while循环之外。

如果有人发现任何其他问题,请告诉我。感谢

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();
              }
         }


      }
   }

答案 1 :(得分:0)

我使用ThreadPool为多个客户端(即不同的模拟器)连接到单个服务器。它运作得很好。