如何让简单的 Android 客户端和 PC 服务器开始工作
我的电脑上运行着一个简单的服务器,运行 android 2.2 的 htcdesire 上运行着一个简单的客户端。
服务器和客户端都使用相同的端口。我将服务器 IP 硬编码到客户端代码中。当我尝试通过 Android 上的客户端连接到正在运行的服务器时,客户端会抛出此异常:
IOException ...: java.net.SocketException: The operation timed out.
以下是客户端和服务器代码的一部分:
客户端代码
InetAddress addr;
Socket socket = null;
byte [] ipAddress = new byte[] {(byte)82,(byte)168,(byte)175,(byte)141};
try{
addr = InetAddress.getByAddress(ipAddress);
socket = new Socket(addr, 1234);
System.out.println("socket = "+socket);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
out.println("hello");
socket.close();
} catch (UnknownHostException e) {
System.out.println("UnknownHostException ...: "+e);
} catch (IOException e) {
System.out.println("IOException ...: "+e);
}
、
服务器代码
ServerSocket ss = new ServerSocket(PORT);
System.out.println("Started:"+ss);
try{
//block until a connection occures
Socket socket = ss.accept();
try{
System.out.println("Conncetion accepted:"+socket);
InputStream IS = socket.getInputStream();
InputStreamReader ISR = new InputStreamReader(IS);
BufferedReader in = new BufferedReader(ISR);
OutputStream OS = socket.getOutputStream();
OutputStreamWriter OSR= new OutputStreamWriter(OS);
BufferedWriter BW = new BufferedWriter(OSR);
//output automatically flushed by PrintWriter
PrintWriter out = new PrintWriter(BW,true);
while(true){
String str = in.readLine();
System.out.println("Client: "+str);
out.println("hi");
}
}finally{
System.out.println("Closing...");
socket.close();
}
}finally{
ss.close();
}
此代码使用 wifi 工作,但不通过 3g。
欢迎任何帮助...并提前感谢您。
I have a simple server running on my pc and a simple client on my htc desire running android 2.2.
The server and client both use the same port. I hard code the servers ip into the clients code. When I try to connect to the running server via the client on android the client throws this exception:
IOException ...: java.net.SocketException: The operation timed out.
Here are parts of the client and server codes:
client code
InetAddress addr;
Socket socket = null;
byte [] ipAddress = new byte[] {(byte)82,(byte)168,(byte)175,(byte)141};
try{
addr = InetAddress.getByAddress(ipAddress);
socket = new Socket(addr, 1234);
System.out.println("socket = "+socket);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
out.println("hello");
socket.close();
} catch (UnknownHostException e) {
System.out.println("UnknownHostException ...: "+e);
} catch (IOException e) {
System.out.println("IOException ...: "+e);
}
,
server code
ServerSocket ss = new ServerSocket(PORT);
System.out.println("Started:"+ss);
try{
//block until a connection occures
Socket socket = ss.accept();
try{
System.out.println("Conncetion accepted:"+socket);
InputStream IS = socket.getInputStream();
InputStreamReader ISR = new InputStreamReader(IS);
BufferedReader in = new BufferedReader(ISR);
OutputStream OS = socket.getOutputStream();
OutputStreamWriter OSR= new OutputStreamWriter(OS);
BufferedWriter BW = new BufferedWriter(OSR);
//output automatically flushed by PrintWriter
PrintWriter out = new PrintWriter(BW,true);
while(true){
String str = in.readLine();
System.out.println("Client: "+str);
out.println("hi");
}
}finally{
System.out.println("Closing...");
socket.close();
}
}finally{
ss.close();
}
This code is working using wifi but not through 3g.
Any help is welcome...and thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能使用路由器/wifi 接入点连接到互联网?
如果您的路由器使用 NAT 协议,则 Internet 上的任何设备都无法打开与您的 PC 的连接。 NAT 只允许 PC 打开与互联网的连接,反之亦然。
因此,当您的手机使用 wifi 时,它可以连接到 PC,因为它们都在同一网络上并且不使用 NAT。当您的手机使用 3g 网络并连接互联网时,您的手机和 PC 之间就会存在 NAT。
解决方案:
在路由器上启用端口转发,然后将应用程序连接到路由器的 IP,路由器会将此连接转发到您的 PC。
You probably use a router/wifi-access-point to connect to internet?
If your router uses NAT protocol, then any devices on the Internet can not open connectio to your PC. NAT only allows PC to open connection to the internet and not vice-versa.
So when your phone is on wifi it can connect to PC because they are both on the same network and NAT is not used. When your phone is on 3g, and hence of the internet, than there is NAT in-between your phone and your PC.
Solution:
Enable port forwarding on your router, than connect with your app to router's IP which will forward this connection to your PC.