在android中连接模拟器实例

时间:2012-06-02 19:57:57

标签: android android-emulator

我读了一个关于同一主题的帖子:connecting 2 Emulator Instances

侦听端口6000的服务器端代码:

 public class NewServerActivity 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.TextView01);
      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.TextView01);
            tv.setText(mClientMsg);
            break;
         default:
            break;
         }
         super.handleMessage(msg);
      }
   };
   class CommsThread implements Runnable {
      public void run() {
         Socket s = null;
         try {
           ss = new ServerSocket(SERVERPORT );
         } catch (IOException e) {
            e.printStackTrace();
         }
         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();
            }
         }
      }
   }
}

客户端代码:

public class NewClientActivity extends Activity {
   private Button bt;
   private TextView tv;
   private Socket socket;
   private String serverIpAddress = "10.0.2.2";
   // 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.myButton);
      tv = (TextView) findViewById(R.id.myTextView);

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

我在一个模拟器上应用了服务器代码,在另一个模拟器上应用了客户端代码。 重定向端口:

  telnet localhost 5554
  redir add tcp:5000:6000

我在模拟器5554上运行服务器,在5556上运行客户端。

我的客户端工作正常但不幸的是,在启动服务器应用程序时,模拟器显示消息不幸的是应用已停止。 eclipse在“ s = ss.accept(); ”行显示 java.lang.NullPointerException 。 它与我的AVD的配置有什么关系。

2 个答案:

答案 0 :(得分:0)

发布您的代码和堆栈跟踪。

从你发布的那一行听起来像'ss'是null并且你试图在null上调用accept()。

答案 1 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

public void mOnClick(View v) {
    switch (v.getId()) {
        case R.id.myButton:
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                        socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);


                        EditText et = (EditText) findViewById(R.id.EditText01);
                        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");

                        out.close();
                        socket.close();
                    } catch (UnknownHostException e) {
                        //tv.setText("Error1");
                        e.printStackTrace();

                    } catch (IOException e) {
                        //tv.setText("Error2");
                        e.printStackTrace();
                    } catch (Exception e) {
                        //tv.setText("Error3");
                        e.printStackTrace();
                    }
                }
            }).start();
            break;
    }

}

需要xml更改 机器人:的onClick =" mOnClick"

<Button
    android:id="@+id/myButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="mOnClick"
    android:text="start" />

它是因为尝试在主线程中运行connect()方法