示例#1
0
        public void Connect(string addr, int port, string pub, bool async = true)
        {
            var ip = NetHelper.ParseIpAddressV6(addr);

            if (ip == null)
            {
                throw new Exception("Unknown addr = " + addr);
            }
            Close(string.Empty);

            m_SocketArgs          = new SocketArgs();
            m_SocketArgs.m_RsaPub = pub;
            if (!Rsa.CheckIsPub(pub))
            {
                throw new Exception(string.Format("RsaPub error {0}", pub));
            }
            m_SocketArgs.m_Socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_SocketArgs.m_Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            m_SocketArgs.m_Socket.NoDelay = true;
            LogHelper.Debug("NetClient Connect {0}:{1}", ip, port);
            if (async)
            {
                m_SocketArgs.m_Socket.BeginConnect(new IPEndPoint(ip, port), new AsyncCallback(ConnectCallback), m_SocketArgs);
            }
            else
            {
                m_SocketArgs.m_Socket.Connect(new IPEndPoint(ip, port));
                LogHelper.Debug("NetClient Connect {0}", "OK");
                OnConnected(m_SocketArgs);
            }
        }