C#:一个增强的TcpListener(二)线程池

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net.Sockets;  
  4. using System.Threading;  
  5.   
  6. namespace Splash.Net.Sockets  
  7. {  
  8.     public partial class TcpListenerPlus : TcpListener  
  9.     {  
  10.         /// <summary>  
  11.         /// 委托声明  
  12.         /// </summary>  
  13.         /// <param name="sender">事件发送者</param>  
  14.         /// <param name="e">事件参数</param>  
  15.         public delegate void ThreadTaskRequest(object sender, EventArgs e);  
  16.   
  17.         /// <summary>  
  18.         /// 线程任务请求事件  
  19.         /// </summary>  
  20.         public event ThreadTaskRequest OnThreadTaskRequest;  
  21.   
  22.         // 已接受的Tcp连接列表  
  23.         protected List<TcpClient> _tcpClients;  
  24.   
  25.         /// <summary>  
  26.         /// 连接列表操作互斥量  
  27.         /// </summary>  
  28.         private Mutex _mutexClients;  
  29.   
  30.         /// <summary>  
  31.         /// 侦听连接线程  
  32.         /// </summary>  
  33.         private void ListenThreadAction()  
  34.         {   // 启动侦听  
  35.             Start();  
  36.   
  37.             // 初始化连接列表和互斥量  
  38.             _tcpClients = new List<TcpClient>();  
  39.             _mutexClients = new Mutex();  
  40.   
  41.             // 接受连接  
  42.             while (true)  
  43.             {  
  44.                 TcpClient tcpClient = null;  
  45.                 try  
  46.                 {   // 接受挂起的连接请求  
  47.                     tcpClient = AcceptTcpClient();  
  48.   
  49.                     // 将该连接通信加入线程池队列  
  50.                     ThreadPool.QueueUserWorkItem(ThreadPoolCallback, tcpClient);  
  51.   
  52.                     // 连接加入列表  
  53.                     _mutexClients.WaitOne();  
  54.                     _tcpClients.Add(tcpClient);  
  55.                     _mutexClients.ReleaseMutex();  
  56.                 }  
  57.   
  58.                 catch (SocketException)  
  59.                 {   // 结束侦听线程  
  60.                     break;  
  61.                 }  
  62.   
  63.                 catch (Exception)  
  64.                 {   // 加入队列失败  
  65.                     if (tcpClient != null)  
  66.                     {  
  67.                         tcpClient.Close();  
  68.                     }  
  69.                 }  
  70.             }  
  71.         }  
  72.   
  73.         /// <summary>  
  74.         /// 线程池回调方法  
  75.         /// </summary>  
  76.         /// <param name="state">回调方法要使用的信息对象</param>  
  77.         private void ThreadPoolCallback(Object state)  
  78.         {   // 如果无法进行转换,则 as 返回 null 而非引发异常  
  79.             TcpClient tcpClient = state as TcpClient;  
  80.             try  
  81.             {   // 执行任务  
  82.                 if (OnThreadTaskRequest != null)  
  83.                 {  
  84.                     OnThreadTaskRequest(tcpClient, EventArgs.Empty);  
  85.                 }  
  86.             }  
  87.   
  88.             catch  
  89.             {  
  90.                 // 阻止异常抛出  
  91.             }  
  92.   
  93.             finally  
  94.             {   // 关闭连接  
  95.                 tcpClient.Close();  
  96.   
  97.                 // 从列表中移除连接  
  98.                 _mutexClients.WaitOne();  
  99.                 if (_tcpClients != null)  
  100.                 {  
  101.                     _tcpClients.Remove(tcpClient);  
  102.                 }  
  103.                 _mutexClients.ReleaseMutex();  
  104.             }  
  105.         }  
  106.   
  107.         /// <summary>  
  108.         /// 关闭侦听器  
  109.         /// </summary>  
  110.         /// <remarks>显示隐藏从基类继承的成员</remarks>  
  111.         public new void Stop()  
  112.         {   // 检测是否已开启侦听  
  113.             if (Active)  
  114.             {  
  115.                 // 关闭侦听器  
  116.                 base.Stop();  
  117.   
  118.                 // 关闭已建立的连接  
  119.                 _mutexClients.WaitOne();  
  120.                 if (_tcpClients != null)  
  121.                 {  
  122.                     foreach (TcpClient client in _tcpClients)  
  123.                     {  
  124.                         client.Close();  
  125.                     }  
  126.   
  127.                     // 清空连接列表  
  128.                     _tcpClients.Clear();  
  129.                     _tcpClients = null;  
  130.                 }  
  131.                 _mutexClients.ReleaseMutex();  
  132.             }  
  133.         }  
  134.     }  
  135. }  
  136. 原文转自:http://blog.csdn.net/jhqin/article/details/7552852
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值