I'm using NetMQ sockets to perform client - server communications. I have a single server that listens to port 5555 and a client, that .bind()
-s to it.
Here's my code sample:
using (NetMQContext ctx = NetMQContext.Create())
{
using (var client = ctx.CreateRequestSocket())
{
client.Connect("tcp://127.0.0.1:5555");
client.SendFrame(jData);
}
}
What I would like to do is to communicate to the user if the client doesn't find any server listening to that port.
What it is actually happening is that if there is no server listening to that port then no exceptions are raised and the .sendFrame()
is called and the application crashes.
Is there any method, such as Exceptions or state code, that can inform me if the connection succeded or not?
The best way to do that is with timeout of the response. So send a request (with timeout) and then wait for the response with timeout. If the server didn't response within the timeout, you can go and try to connect to next server. Like so:
if (client.TrySendFrame(TimeSpan.FromSeconds(2), jData) &&
client.TryReceiveFrame(TimeSpan.FromSeconds(2), out jData) )
{
// Server is online
}
else
{
// Server is down
}