封装的一个套接字----服务端

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Net.Sockets;
using  System.Threading;

namespace  EarlyServer
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class ConnectClient : IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Member#region Member
        
private Socket m_Socket;

        
private string m_NetWorkName;

        
private Thread m_Thread;
        
private bool disposed = false;
        
private bool m_IsRuning = false;

        
public event CommandReceiveEventHandler CommandReceived;
        
#endregion


        
public ConnectClient(Socket socket, string netWorkName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            m_Socket 
= socket;
            m_NetWorkName 
= netWorkName;
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
Public Methods#region Public Methods

        
public void Start()
ExpandedSubBlockStart.gifContractedSubBlock.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();
        }




        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Private Methods#region Private Methods

        
private void ThreadFunction()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            m_IsRuning 
= true;

            
while (m_IsRuning)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
byte[] byteMessage = null; ;
                
string tmpStr = string.Empty;
                
while (ServiceStartFlag)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (m_Socket.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{

                            byteMessage 
= new byte[1000];
                            
int len = m_Socket.Receive(byteMessage);
                            
if (len > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{

                                tmpStr 
= Encoding.UTF8.GetString(byteMessage);
                                
if (tmpStr.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    
string[] ary = tmpStr.Split('|');
                                    
if (ary.Length > 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
{
                                        
//命令|发送者|目标|内容
                                        MsgCommand cmd = new MsgCommand(ary[0], ary[1], ary[2], tmpStr.Replace(ary[0+ "|" + ary[1+ "|" + ary[2+ "|"""));
                                        
this.OnCommandReceived(thisnew CommandEventArgs(cmd));
                                    }

                                }

                            }



                        }

                    }

                    
catch (SocketException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{

                        m_IsRuning 
= true;
                        ClientData.List.Remove(m_NetWorkName);
                    }

                }

            }

        }


        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
event Methods#region event Methods
        
private void OnCommandReceived(object sender, CommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (CommandReceived != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                CommandReceived(
this, e);
            }

        }

        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
IDisposable 成员#region IDisposable 成员

        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Dispose(
true);
            GC.SuppressFinalize(
this);

        }

        
protected virtual void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
if (!this.disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
this.Close();
                
// 注意这里不是线程安全的
            }

            disposed 
= true;
        }


        
public void Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                m_IsRuning 
= false;
                m_Thread.Abort();
            }

            MsgCommand cmd 
= new MsgCommand(CommandType.Outing, this.m_ServerIPAddress, this.m_NetWorkName);

            
this.SendToServer(cmd);

            
this.m_Socket.Close();
            
this.m_NetworkStream.Close();
        }


        
~ConnectClient()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Dispose(
false);
        }

        
#endregion

    }


    
public class ClientData
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public static Dictionary<string, ConnectClient> List = new Dictionary<string, ConnectClient>();
    }

    
public delegate void CommandReceiveEventHandler(object sender, CommandEventArgs eventArgs);

    
public class CommandEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private MsgCommand m_MsgCommand;

        
public MsgCommand Command
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_MsgCommand; }
        }

        
public CommandEventArgs(MsgCommand msgCommand)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.m_MsgCommand = msgCommand;
        }

    }

    
public class MsgCommand
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private string commnadString;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 发送者名称
        
/// </summary>

        public string CommnadString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return commnadString; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { commnadString = value; }
        }


        
private string senderName;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 发送者名称
        
/// </summary>

        public string SenderName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return senderName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { senderName = value; }
        }


        
private string targetName;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 目标名称
        
/// </summary>

        public string TargetName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return targetName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { targetName = value; }
        }


        
private string commandBody;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 内容
        
/// </summary>

        public string MetaData
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return commandBody; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { commandBody = value; }
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructors#region Constructors

        
public MsgCommand(string commnadstring, string sendername, string targetName, string metaData)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.commnadString = commnadstring;
            
this.senderName = sendername;
            
this.commandBody = metaData;
            
this.targetName = targetName;
        }

        
#endregion

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值