无法从真实 Android 设备连接到本地 Mosquitto Broker
Can't Connect to Local Mosquitto Broker From Real Android Device
我使用 Mosquitto 库(版本 - 1.6.12)创建了一个 MQTT 代理。并创建了 Android 移动应用程序,充当 paho 客户端(使用的库 - 'libs/org.eclipse.paho.client.mqttv3-1.2.0.jar'、'libs/org.eclipse.paho.android.service-1.1.1.jar')
正在尝试将 android 应用连接到本地 Mosquitto 代理。我可以通过 Android 模拟器连接 当我尝试使用真正的 android 设备时出现套接字问题。
Android代码:
MqttAndroidClient client;
String clientId = MqttClient.generateClientId();
//Since broker is running in the same system I gave my local IP-ADDRESS - 192.168.0.100
//Port - 1883
client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.0.100:1883", clientId);
try {
MqttConnectOptions options = new MqttConnectOptions();
options.setKeepAliveInterval(60);
Log.d(TAG, "MqttConnectOptions : "+options.toString());
IMqttToken token = client.connect(options);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "onSuccess");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "onFailure ");
exception.printStackTrace();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
Android 工作室日志:
2020-10-06 20:27:31.775 23972-23972/com.exam.mqttwithlibs W/System.err: MqttException (0) - java.net.SocketTimeoutException: failed to connect to /192.168.0.100 (port 1883) from /192.168.0.102 (port 37459) after 30000ms
2020-10-06 20:27:31.778 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:715)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-10-06 20:27:31.781 23972-23972/com.exam.mqttwithlibs W/System.err: at java.lang.Thread.run(Thread.java:764)
2020-10-06 20:27:31.785 23972-23972/com.exam.mqttwithlibs W/System.err: Caused by: java.net.SocketTimeoutException: failed to connect to /192.168.0.100 (port 1883) from /192.168.0.102 (port 37459) after 30000ms
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:129)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.Socket.connect(Socket.java:621)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: ... 6 more
上图中 192.168.0.100 - 系统 IP 和 192.168.0.102 - Android 设备 IP
感谢您的帮助。
按照此 link 打开端口 1883 https://bytesofgigabytes.com/networking/how-to-open-port-in-windows/ 解决了这个问题
我使用 Mosquitto 库(版本 - 1.6.12)创建了一个 MQTT 代理。并创建了 Android 移动应用程序,充当 paho 客户端(使用的库 - 'libs/org.eclipse.paho.client.mqttv3-1.2.0.jar'、'libs/org.eclipse.paho.android.service-1.1.1.jar')
正在尝试将 android 应用连接到本地 Mosquitto 代理。我可以通过 Android 模拟器连接 当我尝试使用真正的 android 设备时出现套接字问题。
Android代码:
MqttAndroidClient client;
String clientId = MqttClient.generateClientId();
//Since broker is running in the same system I gave my local IP-ADDRESS - 192.168.0.100
//Port - 1883
client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.0.100:1883", clientId);
try {
MqttConnectOptions options = new MqttConnectOptions();
options.setKeepAliveInterval(60);
Log.d(TAG, "MqttConnectOptions : "+options.toString());
IMqttToken token = client.connect(options);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "onSuccess");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "onFailure ");
exception.printStackTrace();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
Android 工作室日志:
2020-10-06 20:27:31.775 23972-23972/com.exam.mqttwithlibs W/System.err: MqttException (0) - java.net.SocketTimeoutException: failed to connect to /192.168.0.100 (port 1883) from /192.168.0.102 (port 37459) after 30000ms
2020-10-06 20:27:31.778 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:715)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
2020-10-06 20:27:31.779 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-10-06 20:27:31.780 23972-23972/com.exam.mqttwithlibs W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-10-06 20:27:31.781 23972-23972/com.exam.mqttwithlibs W/System.err: at java.lang.Thread.run(Thread.java:764)
2020-10-06 20:27:31.785 23972-23972/com.exam.mqttwithlibs W/System.err: Caused by: java.net.SocketTimeoutException: failed to connect to /192.168.0.100 (port 1883) from /192.168.0.102 (port 37459) after 30000ms
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:129)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
2020-10-06 20:27:31.786 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2020-10-06 20:27:31.787 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at java.net.Socket.connect(Socket.java:621)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
2020-10-06 20:27:31.788 23972-23972/com.exam.mqttwithlibs W/System.err: ... 6 more
上图中 192.168.0.100 - 系统 IP 和 192.168.0.102 - Android 设备 IP
感谢您的帮助。
按照此 link 打开端口 1883 https://bytesofgigabytes.com/networking/how-to-open-port-in-windows/ 解决了这个问题