我正在尝试设置一个客户端 - 服务器系统,其中Pi充当服务器,而Android设备是客户端。每次我运行代码(我从互联网上拼凑而成)时,客户端都会抛出IOException:
Connection timed out: connect
我刚刚发现,当我尝试ping Pi的IP时,它无法访问。 我该如何解决这个问题?
注意我目前正在Windows PC上测试Java代码,直到我开始工作。
客户端代码(Java):
import java.io.*;
import java.net.*;
public class client
{
static Socket clientSocket;
static String homeIp="192.168.0.105"; //internal ip of server (aka Pi in this case)
static int port=4242;
public static void main(String [] args)
{
sendToPi("I sent a message");
}
public static void sendToPi(String s)
{
//wordsList.append("in sendToPi()\n");
System.out.println("in sendToPi()");
//Log.e("aaa","in sendToPi()\n");
try {
clientSocket = new Socket(homeIp, port);
PrintStream out = new PrintStream(clientSocket.getOutputStream());
out.println(s);
} catch (UnknownHostException e) {
//Log.e("aaa","Don't know about host: "+homeIp+"."+e.getMessage());
System.out.println("Don't know about host: "+homeIp+"."+e.getMessage());
//System.exit(1);
} catch (IOException e) {
//Log.e("aaa","Couldn't get I/O for the connection to: "+homeIp+"."+e.getMessage());
System.out.println("Couldn't get I/O for the connection to: "+homeIp+"."+e.getMessage());
//System.exit(1);
}
}
}
服务器代码(C):我完全按照here编写的代码使用了代码。编译为服务器。以./server 4242
开头答案 0 :(得分:0)
在我的情况下,解决方案是在我的路由器中明确转发客户端端口。