2017-07-28 98 views
1

我开发的.NET的核心应用MQTT协议基于应用程序使用我下面的链接开发应用 mqttMQTT没有这样的主机是已知的错误

我的代码是

public static void Main(string[] args) 
{ 
    MqttClient client = new MqttClient("broker.hivemq.com"); 
    byte code = client.Connect(Guid.NewGuid().ToString(), "username", "password"); 
    Console.WriteLine("code " + code); 
    client.MqttMsgPublished += client_MqttMsgPublished;    
    ushort msgId = client.Publish("mytopic", // topic 
    Encoding.UTF8.GetBytes("Hai this is sample chat application"), // message body 
    MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level 
    true); // retained 
Console.WriteLine("msgId " + msgId); 
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; 

void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e) 
{ 
    Debug.WriteLine("MessageId = " + e.MessageId + " Published = " + e.IsPublished); 
    Console.WriteLine("MessageId = " + e.MessageId + " Published = " + e.IsPublished); 
} 

void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e) 
{ 
    Debug.WriteLine("Subscribed for id = " + e.MessageId); 
    Console.WriteLine("Subscribed for id = " + e.MessageId); 
} 

void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) 
{ 
    Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); 
    Console.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); 
} 

}

用户代码:

mosquitto_sub -h broker.hivemq.com -t mytopic(terminal) 

当我r取消该应用程序将消息发送到用户,但是当我试图从终端发布消息我的申请没有收到任何消息

在终端

mosquitto_pub -h broker.hivemq.com -t mytopic -m "Hai this is sample" 

公布的代码,因此如何从终端接收发布消息?

我使用MQTT客户机作为MqttClient client = new MqttClient("broker.hivemq.com");

,当我试图改变我的IP地址,没跑得到错误像

Unhandled Exception: System.AggregateException: One or more errors occurred. (No such host is known) ---> System.Net.Sockets.SocketException: No such host is known 

回答

1

与做工精细下面的代码

string[] topic = { "mytopic"}; 
byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }; 
client.Subscribe(topic, qosLevels); 

上面的代码在我的代码中缺失.C#console的终端和终端的c#console。

+0

但仍然在使用ipaddress时得到相同的错误。请给出任何想法? –

相关问题