我想將多個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();
}
}
}
}
}