C
C#15mo ago
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(); } 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
20 Replies
Iron
IronOP15mo ago
Ignore return Encoding.Unicode.GetString(msg); its only sending back to the main form the message. after i click the get button on that client, i can get the input in my server, and i can also write to the stream to display a message in the messagebox of the client, however i cannot close the messagebox because it keeps looping like this:
Iron
IronOP15mo ago
Kouhai
Kouhai15mo ago
If you write two message (one after another) to the client, do two messageboxes popup with their content?
Iron
IronOP15mo ago
Hmm no the first pops up, then the second once the first is closed and then it continues with blank messageboxes. What could this mean?
Kouhai
Kouhai15mo ago
I had assumed the client was trying to receive more data from the server and was stuck in an infinite loop because it couldn't receive any, but that doesn't seem to be the case What happens if you keep the TcpClient and NetworkStream open, does the client attempt to send more data? Something like
public async Task ListenAsync()
{
using (TcpClient client = await Host.AcceptTcpClientAsync())
{
using (NetworkStream stream = client.GetStream())
{
int read = 0;
do
{
byte[] msg = new byte[1024];
read = await stream.ReadAsync(msg, 0, msg.Length);
} while(read > 0);
}
}
}
public async Task ListenAsync()
{
using (TcpClient client = await Host.AcceptTcpClientAsync())
{
using (NetworkStream stream = client.GetStream())
{
int read = 0;
do
{
byte[] msg = new byte[1024];
read = await stream.ReadAsync(msg, 0, msg.Length);
} while(read > 0);
}
}
}
Iron
IronOP15mo ago
I tried this now with hope but did not solve it :/ still looping on each messagebox close. Appriciate it tho, any other suggestions?
Kouhai
Kouhai15mo ago
Are you receiving any more data though? Or just the initial message?
Anchy
Anchy15mo ago
what does the client code look like
Iron
IronOP15mo ago
No i am only recieve one string from the client , then i send response back to client. And client is looping messageboxes , even if i dont send a message to the client, it will loop messageboxes I dont have access to it because its a school assignment .exe
Anchy
Anchy15mo ago
ask the lecturer for the source or use a free .net reflector like ILSpy if it still shows the MessageBox when you dont send anything either you are running old code because you have errors in your server or there is a problem with the client
Iron
IronOP15mo ago
The full code is above. But yee in the assignment it says i should use the client they provide, however i am allowed to create my own so i am probably just doing that. Was just curious if this was a server issue or not
Anchy
Anchy15mo ago
you will learn more doing that and have complete control
Iron
IronOP15mo ago
Thanks for your help
Azrael
Azrael15mo ago
Vart är detta?
Iron
IronOP15mo ago
Vad menar du ? Skoluppgift
Azrael
Azrael15mo ago
Trevligt.
Iron
IronOP15mo ago
Ja om uppgiften hade fungerat 😂 My teacher says program works fine. And says i need to convert a string using getbytes , however i already knew this and what does that have to do with the fact that the client messagebox keeps opening xD
Kouhai
Kouhai15mo ago
Your best bet is downloading ILSpy or Dnspy and decompiling the client
Iron
IronOP15mo ago
For anyone else having this issue , my server was actually the issue where it opens a connection to receive a client and then disposes of the client , however after that its not listening for any clients which resulted in the messagebox spamming.
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server