Tcpclient only receives the first message while connected

Asked

Viewed 68 times

1

I’m developing a program for hobby, in order to understand how the TcpClient. Well, I am doing a program that from time to time receives a package from a secondary program and sends an OK back, confirming that the connection is still established.

On the first request, my program sends the OK normally. However, on the second request, it no longer works. It’s like he didn’t receive any package from client, but I can see this second that the package was sent.

Below is my server:

static void Main(string[] args)
    {
        int InternalLoop = 0;
        bool Finished = false;
        TcpListener serverSocket = new TcpListener(System.Net.IPAddress.Any, 10000);
        int requestCount = 0;
        TcpClient clientSocket = default(TcpClient);
        serverSocket.Start();
        while (true)
        {
            bool LoopReceive = true;
            bool LoopSend = false;

            Console.WriteLine(" :::: SERVER STARTED OK");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" :::: CONNECTED TO CLIENT");
            requestCount = 0;
            NetworkStream networkStream = clientSocket.GetStream();

            string Packettosend = "";

            while (LoopReceive == true)
            {
                try
                {
                    //Gets the Client Packet
                    requestCount = requestCount + 1;
                    byte[] bytesFrom = new byte[128];
                    networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                    Packettosend = "ALIVE";
                    Console.WriteLine(" ::: SENDING ALIVE PACKET");
                    LoopReceive = false;
                    LoopSend = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            while (LoopSend == true || InternalLoop < 2)
            {
                try
                {
                    InternalLoop += 1;
                    if (Packettosend == "ALIVE")
                    {
                        Byte[] sendBytes1 = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
                        networkStream.Write(sendBytes1, 0, sendBytes1.Length);
                        networkStream.Flush();

                        LoopReceive = true;
                        LoopSend = false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }
}

If I close the client or the server and open again, the server gets back the expected package and sends back the OK, but only on the first request.

I’ve looked for something similar before creating this topic, but nothing has worked.

  • Can you put in the code of client too? I made a client for testing and worked.

1 answer

0

Good Afternoon! Try using Finally after the first catch. It would look something like this:

catch (Exception ex) { Console.Writeline(ex.Tostring()); }

Finally { tcpListener.Stop(); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.