【发布时间】:2018-12-17 17:57:41
【问题描述】:
尝试使用 Android 客户端连接到套接字时,我收到 ECONNREFUSED ConnectException。
当我在我的 PC 上运行代码时,它运行良好,我能够毫无问题地连接到远程服务器(树莓派、haskell)(因此我们可以排除这种可能性)。
ConnectionManager.class 中的相关代码(在 s=connect 行中抛出错误):
@Override
protected void onHandleIntent(Intent intent) {
try {
s=connect();
socket_out = new PrintWriter(s.getOutputStream(), true);
socket_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
连接函数(“返回新套接字”行中抛出的错误)
private Socket connect() {
try {
return new Socket("Thereisarealip in here",Realport);
} catch (IOException ex) {
Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
}
throw new RuntimeException();
}
XML 权限包括以下内容
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<service android:name=".ConnectionManager"></service>
ConnectionManager Java 类是从主线程调用的:
Intent intent = new Intent(this, ConnectionManager.class);
startService(intent);
【问题讨论】:
-
您的手机/应用程序是否在您的 pi 和 pc 所在的局域网(通过无线)上 - 还是 PI 在您的局域网之外?
-
你用的是模拟器还是真机?
-
@Andy PI 在一个完全不同的网络上。它具有静态 IP 并具有工作端口转发。我可以使用运行相同代码的 PC 连接到它,也可以使用手机上的 SSH 连接到它。
-
@KhalidTaha 我通过 android studio 运行它,但在我的真实手机上通过 USB 运行