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

时间:2015-12-14 06:40:40

标签: java mqtt

我正在尝试检查给定的IP和端口是否运行,如果没有,那么我必须将异常返回给客户端。

这是我的代码,

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";
}

有人告诉我代码中的验证。

1 个答案:

答案 0 :(得分:0)

在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
}