gpt4 book ai didi

java - 连接到模拟器服务器的多个模拟器客户端

转载 作者:行者123 更新时间:2023-11-30 04:27:19 24 4
gpt4 key购买 nike

我想将多个 android 模拟器客户端连接到 android 模拟器服务器。我正在使用 this post 中的代码,我已经稍微改变了服务器。当只有 1 个客户端尝试连接时,代码运行良好。当我尝试将两个客户端连接到服务器时,第二个客户端的消息似乎无法通过。

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

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

}
}

最佳答案

我发现了我的问题。

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


}
}

关于java - 连接到模拟器服务器的多个模拟器客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8265651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com