Search code examples
c#tcpclient

Continuous communication between server and client


I am working on a program that works as a server and client. What I am trying to do is have the client display a message with whether or not it is connected to the server. I figured the best way to do this was every second have the client send an "alive" message and then have the server respond.

At the moment the client sends one message to the server and it will display that it is connected once then every time after that display that it is not connected. So I know the client is working as it does report not connected, but the server is only responding once and then halting. My issue is with the server only seeming to respond to the client once. After that i get no response from the server at all.

Note: The connection to the server is called before I check the connection. Didn't think it was relevant to the question

Client Code

public static void CheckServerConection(object Sender, EventArgs e)
    {
        try
        {
            NetworkStream nwStream = tcpClnt.GetStream();
            byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("Alive");

            nwStream.Write(bytesToSend, 0, bytesToSend.Length);

            byte[] bytesToRead = new byte[tcpClnt.ReceiveBufferSize];
            int bytesRead = nwStream.Read(bytesToRead, 0, tcpClnt.ReceiveBufferSize);
            string received = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);

            if (received == "Alive")
            {
                ActiveConnection = true;
                Console.WriteLine("Connected");
            }
            else
            {
                ActiveConnection = false;
                Console.WriteLine("Not Connected");
            }
        }
        catch (Exception exception)
        {
            ActiveConnection = false;
            Console.WriteLine("Not Connected");
        } 
    }

Server Code

public static void startServer()
    {
        try
        {
            IPAddress ipAd = IPAddress.Parse("0.0.0.0"); //Servers local IP address.
            int port = 36512;
            TcpListener listener = new TcpListener(ipAd, port);
            int i = 0;

            while (i == 0)
            {
                listener.Start();

                Console.WriteLine("Server has been started." + Environment.NewLine + "Running on: " + ipAd + ":" + port);
                Console.WriteLine("Waiting for client connection...");

                TcpClient client = listener.AcceptTcpClient();

                NetworkStream nwStream = client.GetStream();
                byte[] buffer = new byte[client.ReceiveBufferSize];

                int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

                string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine(dataReceived);

                nwStream.Write(buffer, 0, bytesRead);
                listener.Stop();
                client.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

Solution

  • You are instancing the Listener outside the loop. You're calling the listener.Stop() method in the loop, which closes the Listener. After that, it can't restart. See this link for a correct and simple implementation of a TcpListener in the "Example" section.

    https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx