为什么我的TCP服务端总是占用CPU100%???
private void button1_Click(object sender, System.EventArgs e)
{
thrListen=new Thread(new ThreadStart(Listen));
thrListen.IsBackground=true;
thrListen.Start();
}
private void Listen()
{
try
{
IPHostEntry ipinfo=new IPHostEntry();
ipinfo=Dns.Resolve(Dns.GetHostName());
IPAddress ip=ipinfo.AddressList[0];
int port=Convert.ToInt16(GetConfigValue("PORT"),10);
IPEndPoint ep=new IPEndPoint(ip,port);
Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
s.Bind(ep);
s.Listen(500);
//s.Bind((EndPoint)ep);
while(true)
{
Socket sa=s.Accept();
client=sa;
thrClientService=new Thread(new ThreadStart(ClientService));
thrClientService.IsBackground=true;
thrClientService.Start();
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
private void ClientService()
{
while(true)
{
ReceiveData.......
}
}
以上是我的服务端结构,请行家分析下,我的问题到底出在哪里?我都郁闷死了,还有客户端和服务端有一方关闭连接(没有经过协商,一方是强行的),那么另一方怎么就自动检测到然后关闭自己的这个线程呢,要不然,有一方就会仍然在循环运行。?请高手赐教!