c# - Program hangs, waiting for input that I can never give -


I am using Visual Studio to run this small TCP server.

It is specific that the server hears the port 1234 and is located at the IP 127.0.0.1. Our teacher gives us a program that tries to connect to that port from that IP when you click on "Connect". This is working for everyone, so this should be a coding error in my part.

When I connect, the program sends the word "GET" to the stream, for which I have to answer with the list until I disconnect, the program sends the word "RE" and I Just have to remove from my list (which is a normal list),

I have a class TCPaesar (we have to make our own), which is in the form of main code:

  this.tl = new TCPLSTener (IP address, any, port); Tl.Start (); While (true) {TcpClient tcl = tl.AcceptTcpClient (); // Even a person will always wait until connecting, which means that the "new thread" statement never arrives until anyone is connected. TcpHelper th = new TcpHelper (TCL, conf); New thread (new threadstart (th. Start)). Start (); // should be multi-threaded, not sure what it is //t.start (); }  

TcpHelper looks like this ("problem here is here" for use in comments):

  public class TcpHelper {Private TcpClient tc; Private IPEDpoint IPE; Get private string; Configure private configuration; Public TcpHelper (TcpClient TC, Configuration Conf) {this.tc = tc; This.conf = conf; } Using the public Zero Start () {(NetworkStream nws = this.tc.GetStream ()) ({StreamReader sr = New StreamReader (nws)) ({StreamWriter sw = new streamwiter (nws)} {this. Ipe = (IPEndPoint) tc.Client.RemoteEndPoint; this.conf.List.Add (this.ipe.Address); bool conn = true; while (conn) {this.get = sr.ReadLine (); // problem here Switches (this.get) {case "GET": foreach (IP address address in this .conf.List) {sw.WriteLine (address wired ());} Sw.WriteLine ("."); Breakdown; cases "RE": this.conf.List.Remove (this.ipe.Address); sw.WriteLine ("OK."); Conn = false; breakdown; default: break;}}}}}} #region properties public IPEndPoint Ipe {get {this.ipe;}} #endregion}  

Sorry, maybe I can not understand this ... have you written this code?

 < Code> this.tl = new TcpListener (IP address, any, port); tl.Start (); while (true) {TcpClient tcl = tl.AcceptTcpClient (); TcpHelper th = new TcpHelper (TCL, conf); New thread (new threadstart (th. Start)). Start (); //t.Start (); }  

This is going to blow **** from any computer. You are looping infinitely, creating new threads on each loop. Therefore, if you make a new thread per loop, and each loop takes a millisecond (you can say that it is very slow!), You will get 5,000 threads in five seconds. Trying to listen on every single port.

Try using a thread If this is the console app, then use the console. Readline () does not enter a key until blocking the main thread.


With the new information ... AcceptTcpClient block, but instead of creating a new thread, should be queued at Threadpool.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -