Esempio n. 1
0
        public void Start(string host, int port)
        {
            // 初始化连接池
            conns = new Conn[maxConn];
            for (int i = 0; i < maxConn; i++)
            {
                conns[i] = new Conn();
            }
            // 初始化socket
            listenfd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress  ipAdr = IPAddress.Parse(host);
            IPEndPoint ipEp  = new IPEndPoint(ipAdr, port);

            listenfd.Bind(ipEp);
            // 监听
            listenfd.Listen(maxConn);

            recv = new NetAsynRecv(listenfd, ref conns, eServerType.Fsp);
            recv.Init();
            send = new NetAsynSend(eServerType.Fsp);
            send.Init();
            Console.WriteLine("帧服务器网络初始化完毕");
        }
Esempio n. 2
0
        public void Conn(string ip, int port, Action coonCb)
        {
            Debug.Log("new start Connect");
            Stop();
            m_netState          = NetState.Connecting;
            m_recvHeartBeatTime = 0;

            m_ip          = ip;
            m_port        = port;
            m_connectedCb = coonCb;

            IPAddress ipAddres = null;

            if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
            {
                Debug.Log("Connect wifi");
                IPAddress[] address = null;
                try
                {
                    address = Dns.GetHostAddresses(m_ip);
                }
                catch (Exception e)
                {
                    Debug.Log("Dns解析异常, ip:" + m_ip + " port:" + m_port + " " + e);
                    m_netState = NetState.ConnFail;
                    return;
                }

                if (address[0].AddressFamily == AddressFamily.InterNetworkV6)
                {
                    Debug.Log("Connect InterNetworkV6");
                    m_socketClient = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                }
                else
                {
                    Debug.Log("Connect InterNetworkV4");
                    m_socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                ipAddres = address[0];
            }
            else
            {
                Debug.Log("Connect 4g, 源 ip:" + m_ip);
                if (m_ip.Contains(".com"))
                {
                    DnsCsv     csv  = CsvManager.Inst.GetCsv <DnsCsv>((int)eAllCSV.eAC_Dns);
                    DnsCsvData data = csv.GetData(m_ip);
                    m_ip = data.ip;
                    Debug.Log("Connect 4g, 最终 ip:" + m_ip);
                }

                m_socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress.TryParse(m_ip, out ipAddres);
            }

            if (ipAddres == null)
            {
                Debug.Log("ipAddres is null, ip:" + m_ip + " port:" + m_port);
                m_netState = NetState.ConnFail;
                return;
            }

            m_nSend = new NetAsynSend(m_socketClient);
            m_nRecv = new NetAsynRecv(m_socketClient);

            IPEndPoint serverIP = new IPEndPoint(ipAddres, m_port);

            m_socketClient.BeginConnect(serverIP, ConnSucc, null);
        }