Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I want to make a server that will send messages to some clients and the clients will execute a specific code for each command.
I have the following code for the client:
C#
private async Task StartEchoClientAsync()
        {
            try
            {
                using (var tcpClient = new TcpClient("192.168.1.100", 9999))
                {

                    lbEchoClients.Items.Add("TcpClient: " + tcpClient.Client.LocalEndPoint + "created.");
                    var s = tcpClient.GetStream();

                    var data = new byte[1000];
                    var read = new byte[1000];

                    for (var i = 0; i < 120; i++)
                    {
                        await Task.Delay(1000);
                        try
                        {
                            data = Encoding.ASCII.GetBytes("Mesaj");
                            await s.WriteAsync(data, 0, data.Length);
                            data = Encoding.ASCII.GetBytes("");
                        }
                        catch (Exception e)
                        {
                            textBox1.Text += e.ToString();
                        }

                        try
                        {
                            await s.ReadAsync(read, 0, read.Length);
                            byte[] recBuf = new byte[read.Length];
                            Array.Copy(read, recBuf, read.Length);
                            string text = Encoding.ASCII.GetString(recBuf);
                            lbEchoClients.Items.Add("Received Text: " + text);
                            read = Encoding.ASCII.GetBytes("");
                        }
                        catch (Exception e)
                        {
                            textBox1.Text += e.ToString();
                        }
                        
                    }
                    tcpClient.Close();
                }
            }
            catch (Exception e)
            {
                    textBox1.Text += e.ToString();
            }
        }

And the next code is the server :
C#
<pre>private async Task StartServerAsync()
        {
            try
            {
                var tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 9999));
                tcpListener.Start();

                btnStartServer.Enabled = false;
                Console_textBox.Text += "Serverul este online !\r\n";

                while (true)
                {
                    var tcpClient = await tcpListener.AcceptTcpClientAsync();

                    _ = StartServerSideClient(tcpClient);
                }
            }
            catch(Exception e)
            {
                Console_textBox.Text += ">e.ToString() + "\r\n";
            }
        }
private async Task StartServerSideClient(TcpClient tcpClient)
        {
            Console_textBox.Text += "Client: " + tcpClient.Client.RemoteEndPoint.ToString() + " Connected\r\n";
            var s = tcpClient.GetStream();
            var buffer = new byte[1000];

            int bytesRead = 0;
            int bytesWrote = 0;

            while (true)
            {
                ok = false;
                await s.ReadAsync(buffer, 0, buffer.Length);

                byte[] recBuf = new byte[buffer.Length];
                Array.Copy(buffer, recBuf, buffer.Length);
                string text = Encoding.ASCII.GetString(recBuf);

                if (string.IsNullOrEmpty(text))
                    break;

                bytesRead += recBuf.Length;
                Console_textBox.Text +="Text : " + text + "  -  " + ok;
                switch (ok)
                {
                    case false:
                        byte[] data1 = Encoding.ASCII.GetBytes("command1");
                        var wrt1 = s.WriteAsync(data1, 0, data1.Length);
                        bytesWrote += data1.Length;
                        Console_textBox.Text += " command 1 \r\n";
                        break;
                    case true:
                        byte[] data2 = Encoding.ASCII.GetBytes("comand2");
                        var wrt2 = s.WriteAsync(data2, 0, data2.Length);
                        bytesWrote += data2.Length;
                        Console_textBox.Text += " command 2 \r\n";
                        break;
                }
            }
            
            s.Close();

        }

So at var wrt1 = s.WriteAsync(data1, 0, data1.Length); and at var wrt2 = s.WriteAsync(data2, 0, data2.Length); when the code runs after the first message is sent it won't send any more messages and both variables will run into a problem that doesen't kill the program Id = 11, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}".If I'm doing it completely wrong, I'm sorry, but I'm still learning.

What I have tried:

I tried to follow the code with debug and try to spot the problem but it wasn't anything obvious .
Posted
Updated 5-Jan-20 9:54am
Comments
Christian Graus 5-Jan-20 16:01pm    
That response means you didn't await a call.

1 solution

That response means you didn't await a call.
 
Share this answer
 
Comments
Member 14544459 6-Jan-20 4:29am    
How can I await a the call ?
Christian Graus 6-Jan-20 17:14pm    
you said it. await xxxx().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900