gpt4 book ai didi

java - 使用不同计算机上的模拟器将客户端连接到服务器

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

我正在编写一个使用套接字进行通信的应用程序。我有一台在计算机上的一个 android 模拟器上运行的服务器,然后我有 2 个其他客户端在另外 2 台计算机上的 android 模拟器上运行。我正在尝试让 2 个客户端连接到服务器。

当我在同一台计算机上运行服务器和客户端时,这会起作用,但是当我尝试在同一 wifi 网络和不同的计算机上执行此操作时,会出现以下错误。客户端和服务器代码贴在下面。为了展示重要的东西,很多东西都被剥离了。此外,在服务器启动后,我远程登录到服务器并运行这些命令 redir add tcp:5000:6000 (我也尝试过不执行 redir 但它仍然说一样)。然后我启动客户端并得到错误。感谢您的帮助!

我的路由器5000端口和6000端口都打开了。我在托管服务器的计算机上禁用了 Windows 防火墙。

11-27 18:54:02.274: W/ActivityManager(60): Activity idle timeout for HistoryRecord{44cf0a30 school.cpe434.ClassAidClient/school.cpe434.ClassAid.ClassAidClient4Activity}
11-27 18:57:02.424: W/System.err(205): java.net.SocketException: The operation timed out
11-27 18:57:02.454: W/System.err(205): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
11-27 18:57:02.454: W/System.err(205): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:114)
11-27 18:57:02.465: W/System.err(205): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:245)
11-27 18:57:02.465: W/System.err(205): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
11-27 18:57:02.465: W/System.err(205): at java.net.Socket.startupSocket(Socket.java:780)
11-27 18:57:02.465: W/System.err(205): at java.net.Socket.<init>(Socket.java:314)
11-27 18:57:02.465: W/System.err(205): at school.cpe434.ClassAid.ClassAidClient4Activity.onCreate(ClassAidClient4Activity.java:102)
11-27 18:57:02.474: W/System.err(205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-27 18:57:02.474: W/System.err(205): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 18:57:02.474: W/System.err(205): at android.os.Looper.loop(Looper.java:123)
11-27 18:57:02.486: W/System.err(205): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-27 18:57:02.486: W/System.err(205): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 18:57:02.486: W/System.err(205): at java.lang.reflect.Method.invoke(Method.java:521)
11-27 18:57:02.486: W/System.err(205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-27 18:57:02.486: W/System.err(205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-27 18:57:02.486: W/System.err(205): at dalvik.system.NativeStart.main(Native Method)

服务器代码

public class ClassAidServer4Activity extends Activity {

ServerSocket ss = null;
String mClientMsg = "";
String mClientExtraMsg = "";
Thread myCommsThread = null;
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();
}

class CommsThread implements Runnable {
public void run() {
// Socket s = null;
try {
ss = new ServerSocket(SERVERPORT );
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
try {


Socket socket = ss.accept();
connectedDeviceCount++;

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 = QUESTION_ID;
try {
if (s == null)
s = ss.accept();
BufferedReader input = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String st = null;
st = input.readLine();
String[] temp = parseReadMessage(st);
mClientMsg = temp[1];
if(temp.length > 2) {
mClientExtraMsg = temp[2];
}

m.what = Integer.parseInt(temp[0]);
myUpdateHandler.sendMessage(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

客户端代码

public class ClassAidClient4Activity extends Activity {

//telnet localhost 5554
//redir add tcp:5000:6000
private Socket socket;
private String serverIpAddress = "192.168.1.102";
// if "redir add" is disabled this should be 6000
private static final int REDIRECTED_SERVERPORT = 5000;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
mQuestionAdapter.add("UnknownHostException");
e1.printStackTrace();
} catch (IOException e1) {
mQuestionAdapter.add("IOException");
e1.printStackTrace();
}
}
}

最佳答案

我想通了。我需要创建一个代理。我用了this SO post作为引用。并进行了大量修改 this code为多个连接工作。它现在正在工作。万岁!

关于java - 使用不同计算机上的模拟器将客户端连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290596/

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