© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
23 replies
Iron

❔ Assignment: TCPListener that recieves and returns to tcpClient

Hey.

I do know how the tcpListener and tcpClient class works in c#, however im stuck on this assignment, the teacher supplied us with a .exe client that sends a string to the server when i click a button, then the server should return another string to the client, i am successfully able to recieve and return the message back to client but in the client, the messagebox that shows the return msg keeps looping, i have tried alot, it just keeps reopening infinte loop and showing blank after the first msg, and i have to close the client using task manager.

And i dont dare to ask my teacher if this is a issue with the client and i am not even sure it is

Here is my mainForm code:

      tcpServer server = new tcpServer();

        private async void mainForm_Load(object sender, EventArgs e)
        {
             labelStatus.Text = "Status: Inväntar data...";
             await server.ListenAsync();

        }
      tcpServer server = new tcpServer();

        private async void mainForm_Load(object sender, EventArgs e)
        {
             labelStatus.Text = "Status: Inväntar data...";
             await server.ListenAsync();

        }

and here is the tcpServer.cs class:

 public class tcpServer
    {
        // We create our datatypes
        private int portAdress { get; set;}
        private TcpListener Host { get; set; }

        public tcpServer() 
        { // Constructor

            portAdress = 12345;
            Host = new TcpListener(IPAddress.Any, portAdress);
            Host.Start();
            

        }

        public async Task<string> ListenAsync()
        {
            using (TcpClient client =  await Host.AcceptTcpClientAsync())
            {

                using (NetworkStream stream = client.GetStream())
                {
                    //Used to send and receive messages
                    byte[] msg = new byte[1024];
                    await stream.ReadAsync(msg, 0, msg.Length);
                    return Encoding.Unicode.GetString(msg);
                }
            }
        }
    }
 public class tcpServer
    {
        // We create our datatypes
        private int portAdress { get; set;}
        private TcpListener Host { get; set; }

        public tcpServer() 
        { // Constructor

            portAdress = 12345;
            Host = new TcpListener(IPAddress.Any, portAdress);
            Host.Start();
            

        }

        public async Task<string> ListenAsync()
        {
            using (TcpClient client =  await Host.AcceptTcpClientAsync())
            {

                using (NetworkStream stream = client.GetStream())
                {
                    //Used to send and receive messages
                    byte[] msg = new byte[1024];
                    await stream.ReadAsync(msg, 0, msg.Length);
                    return Encoding.Unicode.GetString(msg);
                }
            }
        }
    }


Note that the class above i am just reading the stream to test and it still loops the messagebox.

Any help appriciated
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

SocketException, TCPListener, TCPClient reason?
C#CC# / help
2y ago
✅ TcpClient is connected to TcpListener but TcpListener isn't connected...
C#CC# / help
3y ago
Canceling TcpClient after TcpListener has been stopped
C#CC# / help
2y ago
TcpClient connection
C#CC# / help
4y ago