-1

我读了一个关于同一主题的帖子: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 上运行客户端。

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

4

2 回答 2

0

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

从您发布的一行中,听起来“ss”为空,而您正试图在空上调用accept()。

于 2012-06-02T20:45:31.713 回答
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 更改 android:onClick="mOnClick"

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

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

于 2017-06-14T20:41:25.997 回答