www.pudn.com > sharewindows.rar > Nblh_Link_Client.cs, change:2016-01-07,size:7846b


using NBLH_LINK.Args; 
using NBLH_LINK.Common; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
 
namespace NBLH_LINK 
{ 
    public delegate void ClientReceiveHandle(object sender, Nblh_Args_Buffer args); 
    public delegate void ClientConnectedHandle(object sender, Nblh_Args_Connected args); 
    public delegate void ClientDisConnectHandle(object sender, Nblh_Args_DisConnect args); 
    public class Nblh_Link_Client : Nblh_Link_Base 
    { 
        public event ClientReceiveHandle ReveiveEvent; 
        public event ClientConnectedHandle ConnectedEvent; 
        public event ClientDisConnectHandle DisConnectEvent; 
 
 
        public Nblh_Link_Client() 
        { 
            StatusType = STATUS_TYPE.STOP; 
        } 
        ~Nblh_Link_Client() 
        { 
            Func_Link_Free(); 
        } 
        public override void Func_Link_Init(IPAddress ipaddress, int port) 
        { 
            base.Func_Link_Init(ipaddress, port); 
        } 
        public override void Func_Link_Init(string ip, int port) 
        { 
            if (StatusType == STATUS_TYPE.START || StatusType== STATUS_TYPE.RESTART|| StatusType== STATUS_TYPE.PAUSE) 
            { 
                return; 
            } 
            base.Func_Link_Init(ip, port); 
 
            try 
            { 
                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
                m_socket.Connect(ip, port); 
                if(m_socket.Connected==true) 
                { 
                    StatusType = STATUS_TYPE.START; 
                    if (ConnectedEvent != null) 
                    { 
                        ConnectedEvent(this, new Nblh_Args_Connected(string.Format(Nblh_Link_Resource.STRING_CLIENT_LINK_SUCCESS))); 
                    } 
                    ThreadPool.QueueUserWorkItem(k => { Func_Link_Receive(); }); 
                } 
            } 
            catch (ArgumentNullException ex) 
            { 
                SendMessage(ex.Message); 
            } 
            catch (FormatException ex) 
            { 
                SendMessage(ex.Message); 
            } 
            catch (SocketException ex) 
            { 
                SendMessage(ex.Message); 
            } 
            catch (ObjectDisposedException ex) 
            { 
                SendMessage(ex.Message); 
            } 
            catch (Exception ex) 
            { 
                SendMessage(ex.Message); 
            } 
 
        } 
        /// <summary> 
        /// 初始化连接 
        /// </summary> 
        /// <param name="port"></param> 
        public override void Func_Link_Init(int port) 
        { 
            //Func_Link_Init(IPAddress.Any, port); 
        } 
        public override void Func_Link_Free() 
        { 
            base.Func_Link_Free(); 
 
            if(m_socket.Connected==true) 
            { 
                m_socket.Close();                 
            } 
            this.StatusType = STATUS_TYPE.STOP; 
        } 
         
        /// <summary> 
        /// 接收服务端数据 
        /// </summary> 
        /// <param name="result"></param> 
        private void Func_Link_Receive() 
        { 
           bool flag=true; 
           while (flag) 
           { 
               try 
               { 
                   if (m_socket.Connected == false) 
                   { 
                       Func_Link_Deal_DisConnect(m_socket, Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
                       return; 
                   } 
                   int len = 0; 
                   byte[] buffer = new byte[sizeof(int)]; 
                   len = m_socket.Receive(buffer, sizeof(int), SocketFlags.None); 
                   if (len <= 0) 
                   { 
                       Func_Link_Deal_DisConnect(m_socket, Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
                       return; 
                   } 
                   int size = BitConverter.ToInt32(buffer, 0); 
                   byte[] bytes = new byte[size]; 
 
 
                   int recvLen = 0; 
                   while (recvLen < size) 
                   { 
                       len = m_socket.Receive(bytes, recvLen, size - recvLen, SocketFlags.None); 
                       if (len <= 0) 
                       { 
                           Func_Link_Deal_DisConnect(m_socket, Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
                           return; 
                       } 
                       recvLen += len; 
                   } 
                   SendMessage(string.Format(Nblh_Link_Resource.STRING_SERVER_CLINET_RECEIVED, m_socket.RemoteEndPoint.ToString(), recvLen)); 
                   if (len <= 0) 
                   { 
                       Func_Link_Deal_DisConnect(m_socket, Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
                       return; 
                   } 
                   if (ReveiveEvent != null) 
                   { 
                       ReveiveEvent(m_socket, new Nblh_Args_Buffer(bytes, 0, recvLen)); 
                   } 
 
               } 
               catch (SocketException ex) 
               { 
                   Func_Link_Deal_DisConnect(m_socket, ex.Message); 
                   flag = false; 
               } 
               catch (Exception ex) 
               { 
                   SendMessage(ex.Message); 
                   flag = false; 
               } 
           } 
             
        } 
        /// <summary> 
        /// 发送客户端数据 
        /// </summary> 
        /// <param name="result"></param> 
        public override void Func_Link_SendData(byte[] bytes, int index, int length) 
        { 
            try 
            { 
                if (m_socket.Connected == false) 
                { 
                    Func_Link_Deal_DisConnect(m_socket, Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
                    return; 
                } 
                int size = 0; 
                //  
                size = m_socket.Send(BitConverter.GetBytes(length), sizeof(int), SocketFlags.None); 
                if (size <= 0) 
                { 
                    //Func_Link_Deal_DisConnect(m_socket, ex.Message); 
                } 
                int sendIndex = index; 
                int endIndex = index + length; 
                while (sendIndex < endIndex) 
                { 
                    size = m_socket.Send(bytes, sendIndex, endIndex - sendIndex, SocketFlags.None); 
                    if (size <= 0) 
                    { 
                        // 可能通讯断开 
                    } 
                    sendIndex += size; 
                } 
            } 
            catch (SocketException ex) 
            { 
                Func_Link_Deal_DisConnect(m_socket, ex.Message); 
            } 
            catch (Exception ex) 
            { 
                Func_Link_Deal_DisConnect(m_socket, ex.Message); 
            } 
        } 
        
        /// <summary> 
        /// 处理断开连接套接字 
        /// </summary> 
        /// <param name="result"></param> 
        /// <param name="message"></param> 
        protected virtual void Func_Link_Deal_DisConnect(Socket workSocket, string message) 
        {                         
            if(DisConnectEvent!=null) 
            { 
                DisConnectEvent(this, new Nblh_Args_DisConnect(message)); 
            } 
            if(workSocket.Connected==true) 
            { 
                workSocket.Close(); 
            } 
            StatusType = STATUS_TYPE.STOP; 
            SendMessage(Nblh_Link_Resource.STRING_SERVER_CLINET_LOST); 
        } 
         
    } 
}