参考资料:http://hi.baidu.com/cdmember_daihw/blog/item/26e40a86d4ed7b3866096e49.html
1: SmartThreadPool介绍:http://www.codeproject.com/KB/threads/smartthreadpool.aspx
2: 下载SmartThreadPool——http://smartthreadpool.codeplex.com/
3:在VS中添加引用:SmartThreadPool.dll,其中dll有几个版本,找到项目对应的DLL。
4:创建socket
public Socket ListenerClassFromSmartThreadPool()
{
//建立socket偵聽連接
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint locEP = new IPEndPoint(IPAddress.Any, 2000);
listener.Bind(locEP);
listener.Listen(100);
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 100;
stp.IdleTimeout = 10;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
//SmartThreadPool smart = new SmartThreadPool();
//smart.Start();
IWorkItemResult result = smart.QueueWorkItem(new WorkItemCallback(this.ClientWorkThreadFromSmartThreadPool), null);
//SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
smart.WaitForIdle();
/* 其中 注意WaitAll 、WaitAny、WaitForIdle之间关系 */
//Console.WriteLine(result.Result);
//smart.Shutdown();
//建立一个线程池
//ThreadPool.QueueUserWorkItem(new WaitCallback(ClientWorkThread));
}
catch (Exception ex)
{
help2.logWrite("建立监控出错:" + ex.Message);
}
return listener;
}
5: socket中具体过程,注意:SmartThreadPool要求调用过程返值是object.而.net自带要求不带返回值
private object ClientWorkThreadFromSmartThreadPool(Object stateInfo)
{
ClientWorkThread(null);
return null;
}
//处理连接请求
private void ClientWorkThread(Object stateInfo)
{
byte[] buffer = new byte[8192];
try
{
while (true)
{
try
{
Socket socket = listener.Accept();
IPEndPoint remoEP = ((IPEndPoint)socket.RemoteEndPoint);
string recString = "接收到来自" + remoEP.Address.ToString() + "的连接。";
//Console.WriteLine(recString);
int receCount = socket.Receive(buffer);
if (receCount > 0)
{
//string recievestring = Encoding.Default.GetString(buffer, 0, receCount);
string recievestring = Encoding.GetEncoding("gb2312").GetString(buffer, 0, receCount);
//recievestring = "sdfsdfs:" + recievestring;
if (recievestring != "")
{
recievestring = GetUserAuth(recievestring);
//byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str);
//byte[] sendbuffer = new byte[8192];
//sendbuffer = Encoding.ASCII.GetBytes(recievestring);
byte[] sendbuffer = Encoding.GetEncoding("gb2312").GetBytes(recievestring);
recString = "来自客户端" + remoEP.Address.ToString() + "的消息:" + recievestring;
//Console.WriteLine(recString);
AddListItemHandler addhandle = new AddListItemHandler(AddListItem);
addhandle.Invoke(recString); //调用窗体来执行委托
socket.Send(sendbuffer, sendbuffer.Length, SocketFlags.None);
}
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (Exception ex)
{
help2.logWrite("接收信息出错_1,循环停摆1分钟:" + ex.Message);
System.Threading.Thread.Sleep(60000);
}
}
}
catch (Exception ex)
{
help2.logWrite("接收信息出错:" + ex.Message);
}
}
转自:http://hillstone.blog.hexun.com/54716371_d.html
1: SmartThreadPool介绍:http://www.codeproject.com/KB/threads/smartthreadpool.aspx
2: 下载SmartThreadPool——http://smartthreadpool.codeplex.com/
3:在VS中添加引用:SmartThreadPool.dll,其中dll有几个版本,找到项目对应的DLL。
4:创建socket
public Socket ListenerClassFromSmartThreadPool()
{
//建立socket偵聽連接
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint locEP = new IPEndPoint(IPAddress.Any, 2000);
listener.Bind(locEP);
listener.Listen(100);
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 100;
stp.IdleTimeout = 10;
SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();
//SmartThreadPool smart = new SmartThreadPool();
//smart.Start();
IWorkItemResult result = smart.QueueWorkItem(new WorkItemCallback(this.ClientWorkThreadFromSmartThreadPool), null);
//SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
smart.WaitForIdle();
/* 其中 注意WaitAll 、WaitAny、WaitForIdle之间关系 */
//Console.WriteLine(result.Result);
//smart.Shutdown();
//建立一个线程池
//ThreadPool.QueueUserWorkItem(new WaitCallback(ClientWorkThread));
}
catch (Exception ex)
{
help2.logWrite("建立监控出错:" + ex.Message);
}
return listener;
}
5: socket中具体过程,注意:SmartThreadPool要求调用过程返值是object.而.net自带要求不带返回值
private object ClientWorkThreadFromSmartThreadPool(Object stateInfo)
{
ClientWorkThread(null);
return null;
}
//处理连接请求
private void ClientWorkThread(Object stateInfo)
{
byte[] buffer = new byte[8192];
try
{
while (true)
{
try
{
Socket socket = listener.Accept();
IPEndPoint remoEP = ((IPEndPoint)socket.RemoteEndPoint);
string recString = "接收到来自" + remoEP.Address.ToString() + "的连接。";
//Console.WriteLine(recString);
int receCount = socket.Receive(buffer);
if (receCount > 0)
{
//string recievestring = Encoding.Default.GetString(buffer, 0, receCount);
string recievestring = Encoding.GetEncoding("gb2312").GetString(buffer, 0, receCount);
//recievestring = "sdfsdfs:" + recievestring;
if (recievestring != "")
{
recievestring = GetUserAuth(recievestring);
//byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str);
//byte[] sendbuffer = new byte[8192];
//sendbuffer = Encoding.ASCII.GetBytes(recievestring);
byte[] sendbuffer = Encoding.GetEncoding("gb2312").GetBytes(recievestring);
recString = "来自客户端" + remoEP.Address.ToString() + "的消息:" + recievestring;
//Console.WriteLine(recString);
AddListItemHandler addhandle = new AddListItemHandler(AddListItem);
addhandle.Invoke(recString); //调用窗体来执行委托
socket.Send(sendbuffer, sendbuffer.Length, SocketFlags.None);
}
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (Exception ex)
{
help2.logWrite("接收信息出错_1,循环停摆1分钟:" + ex.Message);
System.Threading.Thread.Sleep(60000);
}
}
}
catch (Exception ex)
{
help2.logWrite("接收信息出错:" + ex.Message);
}
}
转自:http://hillstone.blog.hexun.com/54716371_d.html