Ejemplo n.º 1
0
    public static bool Ping(PingData[] PingList, uint timeout)
    {
        uint tick = Utility.GetTickCount();

        for (int i = 0; i < PingList.Length; ++i)
        {
            PingData pd = PingList[i];
            pd.Socket     = TCPClient.CreateSocket(true, true);
            pd.Ping       = tick;
            pd.Completion = false;
            pd.endPoint   = new IPEndPoint(IPAddress.Parse(pd.IP), pd.Port);
            Thread th = new Thread(new ParameterizedThreadStart(ThreadConnect));
            th.Start(pd);
        }
        while (Utility.GetTickCount() - tick < timeout)
        {
            uint k = 0;
            for (int i = 0; i < PingList.Length; ++i)
            {
                if (PingList[i].Completion)
                {
                    ++k;
                }
            }
            if (k == PingList.Length)
            {
                return(true);
            }
        }
        return(false);
    }
Ejemplo n.º 2
0
    FTPConnectData CreateConnectData(ServerIPData sid)
    {
        FTPConnectData fcd = new FTPConnectData();
        Socket         s   = TCPClient.CreateSocket(true, true);

        s.ReceiveBufferSize = 65536 * 2;
        s.ReceiveTimeout    = 10000000;
        fcd.connected       = false;
        fcd.socket          = s;
        fcd.ippoint         = new IPEndPoint(IPAddress.Parse(sid.IP), sid.Port);
        Thread th = new Thread(new ParameterizedThreadStart(ThreadConnect));

        th.Start(fcd);
        return(fcd);
    }
Ejemplo n.º 3
0
    public bool Connect(string ip, int port, uint newip, ushort newport, bool bThread = true)
    {
        const uint CONNECT_TIME_OUT = 3000;

        try
        {
            m_bConnected = false;
            CloseSocket();
            m_Socket = TCPClient.CreateSocket(true, true);
            IPEndPoint endpoit = new IPEndPoint(IPAddress.Parse(ip), port);
            Thread     th      = new Thread(new ParameterizedThreadStart(ThreadConnect));
            th.Start(endpoit);
            //连接TCP
            //===============================================
            uint tick = GetTickCount();
            while (GetTickCount() - tick < CONNECT_TIME_OUT && m_bConnected == false)
            {
                Thread.Sleep(10);
            }
            if (m_Socket.Connected == false)
            {
                CloseSocket();
                return(false);
            }
            //接收TCP返回数据
            //===============================================
            byte[] buff = new byte[64];
            int    ret  = m_Socket.Receive(buff, SocketFlags.None);
            CloseSocket();

            if (ret != 16 || System.BitConverter.ToUInt32(buff, 0) != ClientSetting.CONNECT_RESULT)
            {
                return(false);
            }
            uint Rand1 = System.BitConverter.ToUInt32(buff, 4) | 0xc0000000;
            uint Rand2 = System.BitConverter.ToUInt32(buff, 8) | 0xc0000000;

            int ServerPort = System.BitConverter.ToInt32(buff, 12);
            System.Array.Copy(System.BitConverter.GetBytes(0x8fffffff), 0, buff, 0, 4);
            System.Array.Copy(System.BitConverter.GetBytes(Rand1), 0, buff, 4, 4);
            System.Array.Copy(System.BitConverter.GetBytes(Rand2), 0, buff, 8, 4);
            System.Array.Copy(System.BitConverter.GetBytes(newip), 0, buff, 12, 4);
            System.Array.Copy(System.BitConverter.GetBytes(newport), 0, buff, 16, 2);

            m_Socket = TCPClient.CreateSocket(false, true);
            endpoit  = new IPEndPoint(IPAddress.Parse(ip), ServerPort);
            m_Socket.Connect(endpoit);
            m_Socket.Blocking = false;
            UnityEngine.Debug.Log(" connnect try connect udp1[" + ((EndPoint)endpoit).ToString() + "]");
            //等待UDP双向确认。
            //===============================================
            bool bind = false;
            tick = GetTickCount();
            while (GetTickCount() - tick < CONNECT_TIME_OUT)
            {
                try
                {
                    m_Socket.Send(buff, 0, 18, SocketFlags.None);
                    ret = m_Socket.Receive(BUFF, 0, BUFF.Length, SocketFlags.None);
                }
                catch
                {
                    ret = 0;
                }

                if (ret == 4 && System.BitConverter.ToUInt32(BUFF, 0) == ConstValue.HEARBEAT_ID)
                {
                    //与服务器绑定成功, 发送3次心跳。
                    for (int i = 0; i < 3; ++i)
                    {
                        try
                        {
                            m_Socket.Send(HEARBEAT_CMD, SocketFlags.None);
                        }
                        catch (System.Exception e)
                        {
                            //LogMgr.Log("UDP最后确认异常:" + e.ToString());
                        }
                        Thread.Sleep(50);
                    }
                    bind = true;
                    break;
                }
                Thread.Sleep(500);
            }
            if (bind == false)
            {
                CloseSocket();
                return(false);
            }
            //UDP连接成功。
            //===============================================
            m_bNotify    = false;
            m_RecvTick   = GetTickCount();
            m_SendTick   = GetTickCount();
            m_SendID     = 1;
            m_RecvID     = 1;
            m_bConnected = true;
            th           = new Thread(new ThreadStart(ThreadRecv));
            th.Start();
            return(true);
        }
        catch (System.Exception e)
        {
            LogMgr.Log("UDP连接失败:" + e.ToString());
        }
        return(false);
    }