0

I'm writing TCP client server software using C#. I want client be connected with server automatically as soon as server started. To do this task, client may need to know whether the server is started or not. But how, is it possible?

My situation: There is one client and one server on the same LAN network. Each one knows IP address of other one. Here is code section to make connection between them:

Server side:

// Starting the Server ...
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();

...

// Listening for client's connection in a thread ...
while (true)
{
    clientSocket = serverSocket.AcceptTcpClient();
    msg(" The client connected");
}

...

Client side:

// Here, client makes connection to the server when user clicks a button
clientSocket.Connect("192.168.1.1", "8888");

...
Orgil
  • 187
  • 3
  • 13
  • 2
    Simple solution: the clients could poll the server and when discovered / hand-shake you will know the server is there... Or... You could get server to broadcast an "I am alive" message when it starts if you have clients that are listening. – Belogix Mar 18 '15 at 15:46
  • Please read http://stackoverflow.com/help/how-to-ask. There is not enough context in your question to provide a good answer. What is the network environment in which your client and server are each running? Are they on the same LAN? Or is the server on the Internet away from the clients? Does this need to work for _all_ possible clients? Or is it sufficient for it to work for clients already known to the server? How often does the server change its status from "not started" to "started" and vice a versa? Would it be acceptable to use an intermediate match-making server? Etc. – Peter Duniho Mar 18 '15 at 16:05
  • Thanks for the advice. I provided my situation and code sections as you suggested – Orgil Mar 19 '15 at 02:51
  • If this is lan only traffic look into using multicast. – Matthew Whited Mar 19 '15 at 03:17

1 Answers1

1

It seems the one of the possible solutions is the following: the Client should "poll" the Server, i.e. Client must try to connect to the Server periodically until the connection is established.

To specify the timeout of "connect" operation, please consider using TcpClient.BeginConnect Method and the IAsyncResult.AsyncWaitHandle Property of the result to wait for the "timeout event". Let's introduce a factory to encapsulate the described functionality:

internal static class TcpClientFactory
{
    public static bool TryConnect(string host, int port, TimeSpan timeout, out TcpClient resultClient)
    {
        var client = new TcpClient();
        var asyncResult = client.BeginConnect(host, port, null, null);

        var success = asyncResult.AsyncWaitHandle.WaitOne(timeout);
        if (!success)
        {
            resultClient = default(TcpClient);
            return false;
        }

        client.EndConnect(asyncResult);
        resultClient = client;
        return true;
    }
}

The client code which uses the factory looks like:

var timeout = TimeSpan.FromSeconds(3);
TcpClient client;
do
{
    Console.WriteLine("Attempting to connect...");
}
while (!TcpClientFactory.TryConnect("host here", port here, timeout, out client));

// Here the connected "client" instance can be used...

Also, please refer to this question: How to set the timeout for a TcpClient?.

Community
  • 1
  • 1