这几天在看园子里Artech的博客,深感其技术研究之深,我只能望而兴叹,不知道何时才能到达他的功力。突然想到以前写程序时碰到困难,然后奋力解决是一件很快乐的事,可是却忘了把经验记录下来。去年因为要用到socket编程,但是创建和销毁socket是一件很费资源的事,所以打算用一种池化的形式。下面赋上代码,因为后来又忙别的,所以整个功能还不是很完善。
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
/// <summary>
/// Mysocket 的摘要说明
/// </summary>
public class Mysocket : Socket
{
byte[] _recedata = new byte[2048];
private bool status = true;//socket的状态true为空闲,false为忙碌
static EndPoint _destination = (EndPoint)(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000));//mysocket发送数据的目标地址也就是转发中心
public bool Status
{
get { return status; }
set { status = value; }
}
public Mysocket(AddressFamily af, SocketType st, ProtocolType pt)
: base(af, st, pt)
{
}
public new void Close()
{
status = true;
}
public string sendMessage(string mess)
{
try
{
byte[] _sendmsg = System.Text.Encoding.ASCII.GetBytes(mess);
SendTo(_sendmsg, _destination);
return "success";
}
catch (SocketException se)
{
return se.Message.ToString();
}
}
public string receMessage()
{
try
{
ReceiveFrom(_recedata, ref _destination);
return System.Text.Encoding.UTF8.GetString(_recedata);
}
catch (SocketException e)
{
return e.Message.ToString();
}
}
}
public class socketPool
{
private int min_size = 2;
private int max_size = 3;
private Mysocket[] _socketPool = null;
static socketPool _instance = new socketPool();
public static socketPool getInstance()
{
return _instance;
}
private socketPool()
{
init();
}
private void init()
{
_socketPool = new Mysocket[max_size];
for (int i = 0; i < min_size; i++)
{
_socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
_socketPool[i].Bind(ep);
}
}
public Mysocket getMysocket()//获取池里的一个连接
{
Mysocket ms = null;
for (int i = 0; i < _socketPool.Length; i++)
{
if (_socketPool[i] != null)
{
if (_socketPool[i].Status)
{
ms = _socketPool[i];
ms.Status = false;
return ms;
}
}
else
{
while (ms == null)
{
ms = _socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
ms.Status = false;
return ms;
}
}
if (ms == null)
{
ms = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
ms.Status = false;
return ms;
}
public void destroy()//注销连接池
{
for (int i = 0; i < _socketPool.Length; i++)
{
if (_socketPool[i] != null)
{
_socketPool[i].Close();
}
}
}
public void restart()
{
destroy();
init();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
/// <summary>
/// Mysocket 的摘要说明
/// </summary>
public class Mysocket : Socket
{
byte[] _recedata = new byte[2048];
private bool status = true;//socket的状态true为空闲,false为忙碌
static EndPoint _destination = (EndPoint)(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000));//mysocket发送数据的目标地址也就是转发中心
public bool Status
{
get { return status; }
set { status = value; }
}
public Mysocket(AddressFamily af, SocketType st, ProtocolType pt)
: base(af, st, pt)
{
}
public new void Close()
{
status = true;
}
public string sendMessage(string mess)
{
try
{
byte[] _sendmsg = System.Text.Encoding.ASCII.GetBytes(mess);
SendTo(_sendmsg, _destination);
return "success";
}
catch (SocketException se)
{
return se.Message.ToString();
}
}
public string receMessage()
{
try
{
ReceiveFrom(_recedata, ref _destination);
return System.Text.Encoding.UTF8.GetString(_recedata);
}
catch (SocketException e)
{
return e.Message.ToString();
}
}
}
public class socketPool
{
private int min_size = 2;
private int max_size = 3;
private Mysocket[] _socketPool = null;
static socketPool _instance = new socketPool();
public static socketPool getInstance()
{
return _instance;
}
private socketPool()
{
init();
}
private void init()
{
_socketPool = new Mysocket[max_size];
for (int i = 0; i < min_size; i++)
{
_socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
_socketPool[i].Bind(ep);
}
}
public Mysocket getMysocket()//获取池里的一个连接
{
Mysocket ms = null;
for (int i = 0; i < _socketPool.Length; i++)
{
if (_socketPool[i] != null)
{
if (_socketPool[i].Status)
{
ms = _socketPool[i];
ms.Status = false;
return ms;
}
}
else
{
while (ms == null)
{
ms = _socketPool[i] = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
ms.Status = false;
return ms;
}
}
if (ms == null)
{
ms = new Mysocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
ms.Status = false;
return ms;
}
public void destroy()//注销连接池
{
for (int i = 0; i < _socketPool.Length; i++)
{
if (_socketPool[i] != null)
{
_socketPool[i].Close();
}
}
}
public void restart()
{
destroy();
init();
}
}
写程序是一个程序员成长的过程,以后一定要养成写博客的好习惯,把自己成长的点滴记录下来。