首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[转]Socket连接池

Posted on 2010-07-28 10:45  ♂游泳的鱼  阅读(2997)  评论(0编辑  收藏  举报
  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using System.Web.UI.HtmlControls;
 10 using System.Net;
 11 using System.Net.Sockets;
 12 
 13 /// <summary>
 14 /// Mysocket 的摘要说明
 15 /// </summary>
 16 public class Mysocket : Socket
 17 {
 18     byte[] _recedata = new byte[2048];
 19     private bool status = true;//socket的状态true为空闲,false为忙碌
 20     static EndPoint _destination = (EndPoint)(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000));//mysocket发送数据的目标地址也就是转发中心
 21 
 22     public bool Status
 23     {
 24         get { return status; }
 25         set { status = value; }
 26     }
 27     public Mysocket(AddressFamily af, SocketType st, ProtocolType pt)
 28         : base(af, st, pt)
 29     {
 30     }
 31 
 32     public new void Close()
 33     {
 34         status = true;
 35     }
 36 
 37     public string sendMessage(string mess)
 38     {
 39         try
 40         {
 41             byte[] _sendmsg = System.Text.Encoding.ASCII.GetBytes(mess);
 42             SendTo(_sendmsg, _destination);
 43             return "success";
 44         }
 45         catch (SocketException se)
 46         {
 47             return se.Message.ToString();
 48         }
 49     }
 50 
 51     public string receMessage()
 52     {
 53         try
 54         {
 55             ReceiveFrom(_recedata, ref _destination);
 56             return System.Text.Encoding.UTF8.GetString(_recedata);
 57         }
 58         catch (SocketException e)
 59         {
 60             return e.Message.ToString();
 61         }
 62     }
 63 }
 64 
 65 public class socketPool
 66 {
 67     private int min_size = 2;
 68     private int max_size = 3;
 69     private Mysocket[] _socketPool = null;
 70 
 71     static socketPool _instance = new socketPool();
 72     public static socketPool getInstance()
 73     {
 74         return _instance;
 75     }
 76 
 77     private socketPool()
 78     {
 79         init();
 80     }
 81     private void init()
 82     {
 83         _socketPool = new Mysocket[max_size];
 84         for (int i = 0; i < min_size; i++)
 85         {
 86             _socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 87             EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
 88             _socketPool[i].Bind(ep);
 89         }
 90     }
 91 
 92     public Mysocket getMysocket()//获取池里的一个连接
 93     {
 94         Mysocket ms = null;
 95         for (int i = 0; i < _socketPool.Length; i++)
 96         {
 97             if (_socketPool[i] != null)
 98             {
 99                 if (_socketPool[i].Status)
100                 {
101                     ms = _socketPool[i];
102                     ms.Status = false;
103                     return ms;
104                 }
105             }
106             else
107             {
108                 while (ms == null)
109                 {
110                     ms = _socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
111                 }
112                 ms.Status = false;
113                 return ms;
114             }
115         }
116         if (ms == null)
117         {
118             ms = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
119         }
120         ms.Status = false;
121         return ms;
122     }
123 
124     public void destroy()//注销连接池
125     {
126         for (int i = 0; i < _socketPool.Length; i++)
127         {
128             if (_socketPool[i] != null)
129             {
130                 _socketPool[i].Close();
131             }
132         }
133     }
134 
135     public void restart()
136     {
137         destroy();
138         init();
139     }
140 }