Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在TcpListener中等待客户端_C#_Client Server - Fatal编程技术网

C# 正在TcpListener中等待客户端

C# 正在TcpListener中等待客户端,c#,client-server,C#,Client Server,我正在使用System.Net.Sockets.TcpListener tcpl、System.Net.Sockets.NetworkStream ns和System.Net.Sockets.TcpClient echoserver制作一个客户机-服务器应用程序并使用此代码保持应用程序响应: while (!tcpl.Pending()) Application.DoEvents(); echoserver = tcpl.AcceptTcpClient(); ns = echoser

我正在使用
System.Net.Sockets.TcpListener tcpl
System.Net.Sockets.NetworkStream ns
System.Net.Sockets.TcpClient echoserver制作一个客户机-服务器应用程序并使用此代码保持应用程序响应:

while (!tcpl.Pending())
   Application.DoEvents();
echoserver = tcpl.AcceptTcpClient();
   ns = echoserver.GetStream();
using (MemoryStream ms = new MemoryStream())
{
   while ((i = ns.Read(recivedbytes, 0, recivedbytes.Length)) != 0)
   {
        ms.Write(recivedbytes, 0, i);
   }
}
我正在等待客户端,但这会给处理器带来太多的负载。是否有其他逻辑可以在不增加处理器负载的情况下执行此操作?

while (!tcpl.Pending())
   Application.DoEvents();
echoserver = tcpl.AcceptTcpClient();
while
-语句实际上是多余的,因为
AcceptTcpCLient()
是一种阻塞方法。如果希望UI响应,请为
TcpListener
创建一个线程,并使用
AcceptTcpClient()
等待传入请求

有关更多参考,请参阅