封装的一个套接字----服务端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace EarlyServer
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class ConnectClient : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Member#region Member
private Socket m_Socket;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string m_NetWorkName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Thread m_Thread;
private bool disposed = false;
private bool m_IsRuning = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event CommandReceiveEventHandler CommandReceived;
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ConnectClient(Socket socket, string netWorkName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Socket = socket;
m_NetWorkName = netWorkName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Methods#region Public Methods
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Start()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_Thread = new Thread(new ThreadStart(ThreadFunction));
m_Thread.IsBackground = true;
m_Thread.Start();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Methods#region Private Methods
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void ThreadFunction()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_IsRuning = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (m_IsRuning)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] byteMessage = null; ;
string tmpStr = string.Empty;
while (ServiceStartFlag)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (m_Socket.Connected)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byteMessage = new byte[1000];
int len = m_Socket.Receive(byteMessage);
if (len > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tmpStr = Encoding.UTF8.GetString(byteMessage);
if (tmpStr.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] ary = tmpStr.Split('|');
if (ary.Length > 3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//命令|发送者|目标|内容
MsgCommand cmd = new MsgCommand(ary[0], ary[1], ary[2], tmpStr.Replace(ary[0] + "|" + ary[1] + "|" + ary[2] + "|", ""));
this.OnCommandReceived(this, new CommandEventArgs(cmd));
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
catch (SocketException ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_IsRuning = true;
ClientData.List.Remove(m_NetWorkName);
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
event Methods#region event Methods
private void OnCommandReceived(object sender, CommandEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (CommandReceived != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CommandReceived(this, e);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IDisposable 成员#region IDisposable 成员
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispose(true);
GC.SuppressFinalize(this);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
protected virtual void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!this.disposed)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Close();
// 注意这里不是线程安全的
}
disposed = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Close()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposed)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_IsRuning = false;
m_Thread.Abort();
}
MsgCommand cmd = new MsgCommand(CommandType.Outing, this.m_ServerIPAddress, this.m_NetWorkName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.SendToServer(cmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.m_Socket.Close();
this.m_NetworkStream.Close();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
~ConnectClient()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispose(false);
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class ClientData
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static Dictionary<string, ConnectClient> List = new Dictionary<string, ConnectClient>();
}
public delegate void CommandReceiveEventHandler(object sender, CommandEventArgs eventArgs);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class CommandEventArgs : EventArgs
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private MsgCommand m_MsgCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public MsgCommand Command
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_MsgCommand; }
}
public CommandEventArgs(MsgCommand msgCommand)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_MsgCommand = msgCommand;
}
}
public class MsgCommand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string commnadString;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 发送者名称
/// </summary>
public string CommnadString
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return commnadString; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ commnadString = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string senderName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 发送者名称
/// </summary>
public string SenderName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return senderName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ senderName = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string targetName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 目标名称
/// </summary>
public string TargetName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return targetName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ targetName = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string commandBody;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 内容
/// </summary>
public string MetaData
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return commandBody; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ commandBody = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public MsgCommand(string commnadstring, string sendername, string targetName, string metaData)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.commnadString = commnadstring;
this.senderName = sendername;
this.commandBody = metaData;
this.targetName = targetName;
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2009-07-17 21:15
天纯蓝
阅读(
386)
评论()
编辑
收藏
举报