[英]WiFi network programming in android
i want to program a server/client app in android. 我想在android中编写服务器/客户端应用程序。 i have one server class on my pc and client on my android phone. 我的PC上有一个服务器类,而Android手机上有一个客户端。 all permissions are ok. 所有权限都可以。 Here is client: 这是客户:
try {
mysocket = new Socket("My PC IP Address", 4444);
} catch (UnknownHostException e) {...
} catch (IOException e) {...
}
here is server: 这是服务器:
try {
myServerSocket = new ServerSocket(4444);
} catch (IOException e) {...
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.println("Connection Established.");
} catch (IOException e) {
}
I uses "Connectify" program on my pc to have an (virtual)access point such that my phone connect to that. 我在个人计算机上使用“ Connectify”程序来拥有一个(虚拟)接入点,以便手机可以连接到该接入点。 But when I try to connect to server in my android app, it hangs and then throws Timed-out exception. 但是,当我尝试连接到我的Android应用中的服务器时,它会挂起,然后抛出Timed-out异常。
This code doesn't look bad. 这段代码看起来还不错。
Are you certain Connectify is working well? 您确定Connectify运作良好吗? You shoud try running both the server and the client you have built on your PC, using two different processes (as an example the server as a standalone and the client in the Android Emulator). 您应该尝试使用两个不同的进程(例如,服务器作为独立服务器和Android Emulator中的客户端)运行在PC上构建的服务器和客户端。
If it works properly from localhost to localhost:4444, the the connection is the cause of the problem, not your code. 如果它可以从localhost正常运行到localhost:4444,则说明连接是问题的原因,而不是您的代码。 And otherwise, you will easily find the bug in your code. 否则,您将很容易在代码中找到该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.