Mqtt connection fails when connecting to public brokers - c#

I am trying to connect to some mqtt public brokers for test, but I can't connect to any.
I tried to follow these instruction for the code: https://docs.emqx.com/en/cloud/latest/connect_to_deployments/c_sharp_sdk.html#connection
with error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
I tried to connect to other public servers as broker.hivemq.com, mqtt.eclipse.org, test.mosquitto.org
But nothing seems to work. Did anyone succeed to connect to a public broker with c#?
I would highly appreciate any recommendations
using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace csharpMQTT
{
class Program
{
static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
{
MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
client.Connect(clientId, username, password);
if (client.IsConnected)
{
Console.WriteLine("Connected to MQTT Broker");
}
else
{
Console.WriteLine("Failed to connect");
}
return client;
}
static void Main(string[] args)
{
string broker = "broker.emqx.io";
int port = 1883;
string clientId = Guid.NewGuid().ToString();
string username = "emqx";
string password = "public";
MqttClient client = ConnectMQTT(broker, port, clientId, username, password);
}
}
}
Also, I tried to eliminate credentials:
using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace csharpMQTT
{
class Program
{
static MqttClient ConnectMQTT(string broker, int port, string clientId)
{
MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
client.Connect(clientId);
if (client.IsConnected)
{
Console.WriteLine("Connected to MQTT Broker");
}
else
{
Console.WriteLine("Failed to connect");
}
return client;
}
static void Main(string[] args)
{
string broker = "broker.hivemq.com";
int port = 1883;
string clientId = Guid.NewGuid().ToString();
MqttClient client = ConnectMQTT(broker, port, clientId);
}
}
}
I used target framework for project : .Net Core 3.1 and nuget package M2Mqtt 4.3.0

Related

RabbitMq Server is giving me the message "missed heartbeats from client"

The RabbitMQ Server is giving me the message: Missed heartbeats from client. I know this message occour when the cliente stop sending the heartbeat to server, then RabbitMq server close the connection.
The problem is that I'm using RabbitMq in localhost, so I think it isn't about network blocking. My client uses the EasyNetQ(.Net 4.6.1 / Component Version: 6.3.1) component and it should had handled this heartbeat by itself.
Why the client wouldn't send the heartbeat even I'm using RabbitMq in localhost?
Bellow an example from my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyNetQ;
using EasyNetQ.Topology;
using InteraxaFramework.Genesys.Common;
using log4net;
namespace Bridge.Genesys.Services
{
public class RabbitProducer
{
private string _hostName;
private int _port;
private string _userName;
private string _password;
private IConnection _connection;
private IModel _channel;
private int _timeout;
private IBus bus;
private MessageProperties _msgProperties;
private Dictionary<string, Exchange> _exchages;
public RabbitProducer(string hostName, int port, string userName, string password, int timeout = 60)
{
_hostName = hostName;
_port = port;
_userName = userName;
_password = password;
_timeout = timeout;
createConnection();
_msgProperties = new MessageProperties();
_exchages = new Dictionary<string, Exchange>();
}
private Exchange GetExchange(string exchange)
{
if (!_exchages.ContainsKey(exchange))
{
_exchages[exchange] = new Exchange(exchange);
}
return _exchages[exchange];
}
private void createConnection()
{
if (bus == null)
{
bus = RabbitHutch.CreateBus($"host={_hostName}:{_port};username={_userName};password={_password};timeout={_timeout}");
}
}
public async Task PublishExchangeAsync(string exchange, byte[] body)
{
await bus.Advanced.PublishAsync(
GetExchange(exchange),
string.Empty,
false,
_msgProperties,
body);
}
public void Disconnect()
{
if (_exchages != null)
{
_exchages.Clear();
}
if (bus != null)
{
bus.Dispose();
}
}
}
}
Other parts of my code uses this class as singleton. The program is a windows service that keeps always running and uses just one connection and one channel during the service lifetime.
The creation of singleton object:
this.rabbitProducer = new RabbitProducer("localhost", 5672, "guest", "guest", 60);
The utilization of the publisher:
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, JsonObj);
var bytes = ms.ToArray();
var bodyBytes = bytes;
await rabbitProducer.PublishExchangeAsync(queueName, bodyBytes);
}

What are the way to sed a udp packet to openvpn

Considering that openvpn is a udp server running on port 1194, I am intrested to see , how can I send a packet using udp c#.
I am aware of the fact that openVpn is a protocol itself, and considering the lack of knowledge in that domain , I started writing a simple udp client to send a message to server.
Here is the code.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConnectOpenVPN
{
public struct Received
{
public IPEndPoint Sender;
public string Message;
}
abstract class UdpBase
{
protected UdpClient Client;
protected UdpBase()
{
Client = new UdpClient();
}
public async Task<Received> Receive()
{
var result = await Client.ReceiveAsync();
return new Received()
{
Message = Encoding.ASCII.GetString(result.Buffer, 0, result.Buffer.Length),
Sender = result.RemoteEndPoint
};
}
}
//Client
class UdpUser : UdpBase
{
private UdpUser() { }
public static UdpUser ConnectTo(IPAddress serverIP, int port)
{
var connection = new UdpUser();
connection.Client.Connect(serverIP, port);
return connection;
}
public void Send(string message)
{
var datagram = Encoding.ASCII.GetBytes(message);
Client.Send(datagram, datagram.Length);
}
}
class Program
{
static void Main(string[] args)
{
//create a new client
var client = UdpUser.ConnectTo(IPAddress.Parse("13.23.118.17"), 1194);
Task.Run(async () => {
while (true)
{
try
{
var received = await client.Receive();
Console.WriteLine(received.Message);
if (received.Message.Contains("quit"))
break;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
});
string read;
do
{
read = Console.ReadLine();
client.Send(read);
} while (read != "quit");
}
}
}
So , I am unable to recieve any message from server and just witing for ever. Not even an denied or exception.
I was planning to send credentials and openvpn config next.
What are the thing I should do when creating client program for open vpn client ? Is creating an UDP client even correct or I should be using openVPn protocol itseld and how?

'Command "list "inbox" "*"" failed' while using MailSystem.net

I'm trying to use the MailSystem.Net to retrieve mails from my gmail account but i get the above error. I don't seem to find any link related to such error on googl. Here's my code
public class MailRepository
{
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
this is the error i got : Command "list "inbox" "*"" failed : 171031010631135 BAD Unknown command b7mb174701481wmf
private void ReadImap()
{
var mailRespository = new MailUtil.MailRepository("imap.gmail.com", 993, true, "myGmailAccount", "Mypassword");
var emailList = mailRespository.GetAllMails("inbox");
foreach(Message email in emailList)
{
//DoSomething
if(email.Attachments.Count > 0)
{
//DoSomething
}
}
}
What am i doing wrong?? I'm just replicating what i read online here for Demo purposes.
Have you tried logging in?
It looks like you're getting that error because you never logged in, so the LIST command is not valid.
From your example, you dropped the Client.Login:
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password); // LINE YOU MISSED
}

Connecting from websocket client to server

I have implemented web socket server using web API and Microsoft.WebSockets package.
public class MyWSHandler : Microsoft.Web.WebSockets.WebSocketHandler
{
public string _username;
public MyWSHandler(string username)
{
_username = username;
}
public override void OnOpen()
{
System.Diagnostics.Debug.Write("Socket Opened!");
if (!Module._clients.Any(x => ((MyWSHandler)x)._username == this._username)) {
Module._clients.Add(this);
}
Module._clients.Broadcast("User "+this._username+" has connected!");
}
}
I can connect to the server from JavaScript client code. Then, I try to connect to the server from a desktop application using System.Net.WebSockets.WebSocketClient class with C#.
try
{
var websocket = new ClientWebSocket();
await websocket.ConnectAsync(new Uri(#"ws://localhost:5587/api/ws?username=admin"), CancellationToken.None);
await Task.WhenAll(Receive(websocket), Send(websocket));
}
catch(Exception ex){
Console.WriteLine("Exception: {0}", ex);
}
However, the remote server throws an error (404). Is there anything I missed?

Read emails from a web mail version of a client email using C#

I have been trying to read emails from a web mail version of a client email. Searched a lot in the internet. I was using a code I found online
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
using System;
namespace GmailReadImapEmail
{
public class MailRepository {
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password) {
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox) {
return GetMails(mailBox, "ALL").Cast < Message > ();
}
public IEnumerable<Message> GetUnreadMails(string mailBox) {
return GetMails(mailBox, "UNSEEN").Cast < Message > ();
}
protected Imap4Client Client
{
get {
return client ??(client = new Imap4Client());
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase) {
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
public static void Main() {
var mailRepository = new MailRepository(
"outlook.office365.com",
143,
true,
"username",
"password"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach(Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0) {
foreach(MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}
}
}
I am getiing an exception in Client.ConnectSsl(mailserver,port)
The exception is : No connection could be made because the target machine actively refused it

Categories

Resources