我尝试使用TcpListener创建这个Web服务器,但它不会接受我所做的大约一半的请求。它只是在发出请求时挂起AcceptTcpClient
,在任何端口上的行为都相同。
this.main = new Thread(() =>
{
while (this.running)
{
TcpClient c = this.t.AcceptTcpClient();
new Task(() =>
{
handleClient(c);
}).Start();
}
});
this.main.Start();
答案 0 :(得分:0)
也许你可以尝试这样的事情:
public void Start()
{
Console.WriteLine("Server started...");
TcpListener listener = new TcpListener(System.Net.IPAddress.Loopback, 1234);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
new Thread(new ThreadStart(() =>
{
HandleClient(client);
})).Start();
}
}
private void HandleClient(TcpClient client)
{
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true };
StreamReader reader = new StreamReader(stream, Encoding.ASCII);
string inputLine = reader.ReadLine();
Console.WriteLine("The client with name " + " " + inputLine + " is conected");
}