Example #1
0
        public void Connect(IPEndPoint ipep, AsyncCallback successful, AsyncCallback failure)
        {
            Action action = VoidAction;

            //创建套接字
            Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //开始对一个远程主机建立异步的连接请求
            Socket.BeginConnect(ipep, asyncResult =>
            {
                try
                {
                    //结束挂起的异步连接请求
                    Socket.EndConnect(asyncResult);

                    //执行异步回调
                    if (successful != null)
                    {
                        action.BeginInvoke(successful, null);
                    }
                    //接受消息
                    Recive();
                }
                catch (SocketException ex)
                {
                    //执行异步回调
                    if (failure != null)
                    {
                        action.BeginInvoke(failure, null);
                    }
                    //异常回调
                    EccExceptionListener.Ecc_ConnectionFail(ex);
                }
            }, null);
        }
Example #2
0
        public void Connect(IPEndPoint ipep, IEccReceiptListener listener)
        {
            //创建套接字
            Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //开始对一个远程主机建立异步的连接请求
            Socket.BeginConnect(ipep, asyncResult =>
            {
                try
                {
                    //结束挂起的异步连接请求
                    Socket.EndConnect(asyncResult);
                    //连接完成回调
                    if (listener != null)
                    {
                        EccReceiptListener.Ecc_Connection(listener, true);
                    }
                    //接受消息
                    Recive();
                }
                catch (SocketException ex)
                {
                    //与服务器连接失败
                    if (listener != null)
                    {
                        EccReceiptListener.Ecc_Connection(listener, false);
                    }
                    //异常回调
                    EccExceptionListener.Ecc_ConnectionFail(ex);
                }
            }, null);
        }