Wait for server to become available

If you are using TcpClient to connect to a Tcp server and you are dealing with Server connections problems but you’d like to allow your TcpClient to connect when the Server becomes available try the following code:

while (!client.Connected)
{
    try
    {
	client.Connect(server_address, server_listening_port);
    }
    catch (Exception)
    {
	Thread.Sleep(1000);
    }
}
Categories: C# Tags: , , ,
%d bloggers like this: