Java Raspberry Pi和Android之间的客户机-服务器通信
我正在尝试设置一个客户端-服务器系统,Pi作为服务器,Android设备作为客户端。每次我运行代码(我从互联网上拼凑而成)时,客户端都会抛出一个IOException:Java Raspberry Pi和Android之间的客户机-服务器通信,java,android,c++,client-server,Java,Android,C++,Client Server,我正在尝试设置一个客户端-服务器系统,Pi作为服务器,Android设备作为客户端。每次我运行代码(我从互联网上拼凑而成)时,客户端都会抛出一个IOException: Connection timed out: connect 我刚刚发现,当我尝试ping Pi的IP时,它是无法访问的。 我怎样才能解决这个问题 注意:我目前正在Windows PC上测试Java代码,直到它正常工作为止 客户端代码(Java): 服务器代码(C):我使用的代码与编写的完全相同。编译为服务器。从./serve
Connection timed out: connect
我刚刚发现,当我尝试ping Pi的IP时,它是无法访问的。
我怎样才能解决这个问题
注意:我目前正在Windows PC上测试Java代码,直到它正常工作为止
客户端代码(Java):
服务器代码(C):我使用的代码与编写的完全相同。编译为服务器。从./server 4242开始在我的情况下,解决方案是显式转发路由器中的客户端端口。谢谢,修复了这个问题。知道它为什么不工作吗?
静态字符串homeIp=“localhost”代码>-您需要提供raspberry pi设备的ip。使用您当前的代码,Android设备正试图连接到同一手机上的4242端口。
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);
}
}
}