❔ 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:
and here is the tcpServer.cs class:
Note that the class above i am just reading the stream to test and it still loops the messagebox.
Any help appriciated
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();
}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);
}
}
}
}Note that the class above i am just reading the stream to test and it still loops the messagebox.
Any help appriciated