简体   繁体   English

MQTT-如何检查端口和IP是否在mqtt中运行

[英]MQTT - how to check port and IP is running in mqtt

I am trying to check the given IP and port running or not, if not then I have to return exception to client. 我正在尝试检查给定的IP和端口是否在运行,否则,我必须将异常返回给客户端。

Here is my code, 这是我的代码,

MqttMessage message2 = new MqttMessage();
MQTT mqtt_connect = new MQTT();
mqtt_connect.setHost(Host_Address, Integer.parseInt(port));
String topic = "/call/MQTT_Config";
mqtt_connect.setClientId("MQTT_Config");
mqtt_connect.setWillRetain(false);
mqtt_connect.isWillRetain();
mqtt_connect.setWillTopic(topic);
BlockingConnection m_publisher = mqtt_connect.blockingConnection();
m_publisher.connect();
if(connection establish){ 
    // here how to check the condition 
    do publish  here
}
else{
    return "connection failure";
}

Someone tell me the validation in my code. 有人告诉我代码中的验证。

Wrap up your code in try/catch as follow 在try / catch中包装代码,如下所示

try{
    MqttMessage message2 = new MqttMessage();
    MQTT mqtt_connect = new MQTT();
    mqtt_connect.setHost(Host_Address, Integer.parseInt(port));
    String topic = "/call/MQTT_Config";
    mqtt_connect.setClientId("MQTT_Config");
    mqtt_connect.setWillRetain(false);
    mqtt_connect.isWillRetain();
    mqtt_connect.setWillTopic(topic);
    BlockingConnection m_publisher = mqtt_connect.blockingConnection();
    m_publisher.connect();

}
catch(Exception e){
    add message for connection not established
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM