各位大侠,知道博客园的牛人很多,经常来看,第一次提问,忘解答。
问题如下:目前用SOCKET的BeginReceive和EndReceive接收服务端的数据,造成客户端的CPU利用率过高,基本上一直都超过了50%,怎样写才能把CPU降下去?
我在网上看到有人说把系统的缓冲区调大一些,默认是8K,这个方法可行吗?
源码:
private void ReceiveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
try
{
if (state.workSocket.Available != 0)//缓冲区有数据
{
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
byte[] data = new byte[bytesRead];
Array.Copy(state.buffer, 0, data, 0, bytesRead);
lock (((ICollection)dataBuffers).SyncRoot)
{
dataBuffers.Enqueue(data);
}
}
}
else
{
Thread.Sleep(1000);
}
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
自己已经解决,CPU过高不在这一块,是在处理的时候有一个while(true) ``