0

I'm trying to create C# console app.It's like public chat with multiples clients.Everything works fine I just try to understand how I can know when somebody goes offline.App is work with TCPListener and TCPClient.So on the server side I wrote some code which trying to detect who is offline.I'm not sure but i'm thinking the problem is in TCPListener because when he waiting a new connection software is freezed and can't continue loop.

Server code:

        string clientUsername = string.Empty;
        IPAddress ip = IPAddress.Parse("192.168.1.2");
        int port = 5555;
        TcpListener tcpServer = new TcpListener(ip,port);

        tcpServer.Start();

        int clientCounter = 0;
        List<TcpClient> tcpClients = new List<TcpClient>();

        while (true)
        {
            tcpClients.Add(tcpServer.AcceptTcpClient());

            NetworkStream networkStream = tcpClients[clientCounter].GetStream();
            BinaryReader readUsername = new BinaryReader(networkStream);
            clientUsername = readUsername.ReadString();

            if (tcpClients[clientCounter].Connected)
            {
                Console.WriteLine("Client " + clientUsername + " connected!");
                clientCounter++;
            }

            foreach (TcpClient client in tcpClients)
            {
                if (!client.Connected)
                {
                    Console.WriteLine("Client " + clientUsername + " is offline!");
                }
            }

            if (tcpClients.Count >= 3)
            {
                for (int i = 0; i < tcpClients.Count; i++)
                {
                    NetworkStream stream = tcpClients[i].GetStream();
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write("Server sending message to every client!");
                }
            }

        }

Client code:

        IPAddress ip = IPAddress.Parse("192.168.1.2");
        int port = 5555;
        TcpClient client = new TcpClient();

        client.Connect(ip,port);

        NetworkStream networkStream = client.GetStream();

        BinaryWriter sendUsername = new BinaryWriter(networkStream);
        sendUsername.Write(Environment.UserName);

        while (true)
        {
            NetworkStream stream = client.GetStream();
            BinaryReader reader = new BinaryReader(stream);
            string messageFromServer = reader.ReadString();
            Console.WriteLine(messageFromServer);
        }
XANDRO
  • 27
  • 4
  • TCP is designed to be resilient. Normally, there are only two things that would cause a socket to become disconnected or reset: the remote endpoint has _explicitly_ done so, either through graceful closure of the socket, or by simply resetting the connction; or the local endpoint attempts to send data to the remote endpoint and that fails because the remote endpoint is not reachable for some reason. If you want a disconnect for other reasons, it's up to you to implement that. See duplicate for one possible approach. – Peter Duniho May 13 '21 at 06:28
  • Use Async instead of Sync methods so you will get events. If connection breaks due to hardware like PC being turned off or somebody disconnecting an ethernet cable then you need to impliment Keep-Alive. Keep-Alive sends a empty packets from client to server so if the TCP ACK is not received you can tell that the connection is no longer working. – jdweng May 13 '21 at 07:11

0 Answers0