0

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?

user3666197
  • 1
  • 6
  • 50
  • 92
Snake
  • 113
  • 1
  • 9
  • Could you ask for a property of the connection that would force the connection to b created or fail. For example could you ask for the TCP/IP connection ID from the local client which could force the connection to open – Sql Surfer Sep 18 '16 at 23:57
  • I tried to find some property or way to communicate if the connection is not possible. From my understanding the zeromq/netmq framework doesn't notify that. No matter if the client or server connect first,the library will try to connect when possible – Snake Sep 19 '16 at 18:57

1 Answers1

2

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
}
somdoron
  • 4,653
  • 2
  • 16
  • 24
  • Great it works! But when i check for the response within the timeout if the response is not sent the app crashes. If the check is false then the execution skip to the else statement which should return a false or whatever else message to the caller. It seems like the thread get stuck and doesn't get back to the caller – Snake Sep 20 '16 at 08:45
  • I'm not sure I understand, within the if statement you already have the response in the jData. What exception do you get? – somdoron Sep 20 '16 at 08:50
  • 1
    I'd like, if the server is down, to stop and exit the function so go ahead with my code. But in this case (Server down) if i return to the caller with else{ return false;} the app get stuck. In general debugging i noticed that when leaving the using statements without receiving a response from the server the app get stuck. I'm not english so hope it's clear – Snake Sep 20 '16 at 09:01
  • 1
    ahh, Set the linger of the socket to zero. socket.Options.Linger = TimeSpan.Zero; – somdoron Sep 20 '16 at 10:47
  • what if the server is 3d party and doesnt allow sending? i can "connect" to any random IP and my c# client wont know if I am really connected or not.. this cant be used in business critical app.. can it? – Boppity Bop Dec 06 '20 at 13:36