Esempio n. 1
0
        //------------------------------------------------------------
        #region 绑定端口函数
        public int Bind(int port = 0)
        {
            Debuger.Log(LOG_TAG, "Bind() port = " + port);
            if (m_SystemSocket == null)
            {
                return(0);
            }

            //如果Bind的端口为0,则会随机分配一个端口
            IPEndPoint ipep = IPUtils.GetIPEndPointAny(m_AddrFamily, port);

            m_SystemSocket.Bind(ipep);
            m_IsActive = true;
            return(SelfPort);
        }
Esempio n. 2
0
        //------------------------------------------------------------
        #region Functions to bind port
        public int Bind(int port = 0)
        {
            MyLogger.Log(LOG_TAG, "Bind() port = " + port);
            if (mSystemSocket == null)
            {
                return(0);
            }

            //if the bound port is 0, will assign a random port
            IPEndPoint ipep = IPUtils.GetIPEndPointAny(mAddrFamily, port);

            mSystemSocket.Bind(ipep);
            isActive = true;
            return(SelfPort);
        }
Esempio n. 3
0
        //------------------------------------------------------------
        #region ReceiveFrom和SendTo函数
        public int ReceiveFrom(byte[] buffer, int maxsize, ref IPEndPoint remoteEP)
        {
            int cnt = 0;

            EndPoint ip = null;

            if (!m_EnableBlockOnRecv)
            {
                if (m_SystemSocket.Available <= 0)
                {
                    return(0);
                }
            }


            if (m_AddrFamily == AddressFamily.InterNetwork)
            {
                //如果是IPv4环境,则(与Android的处理一样)
                ip  = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
                cnt = m_SystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);

                if (cnt > 0 && remoteEP != null && !remoteEP.Equals(ip))
                {
                    Debuger.LogWarning(LOG_TAG, "ReceiveFrom() 收到一个自来陌生IP:Port(" + ip + ")的数据包!");
                    return(0);
                }
            }
            else
            {
                //如果是IPv6环境,则:
                ip  = remoteEP;
                cnt = m_SystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);
            }

            remoteEP = ip as IPEndPoint;



            return(cnt);
        }
Esempio n. 4
0
        //------------------------------------------------------------
        #region ReceiveFrom,SendTo Function
        public int ReceiveFrom(byte[] buffer, int maxsize, ref IPEndPoint remoteEP)
        {
            int cnt = 0;

            EndPoint ip = null;

            if (!mEnableBlockOnRecv)
            {
                if (mSystemSocket.Available <= 0)
                {
                    return(0);
                }
            }


            if (mAddrFamily == AddressFamily.InterNetwork)
            {
                //In IPv4 ,the same as android
                ip  = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
                cnt = mSystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);

                if (cnt > 0 && remoteEP != null && !remoteEP.Equals(ip))
                {
                    MyLogger.LogWarning(LOG_TAG, "ReceiveFrom()", " receive msg from stranger IP:Port(" + ip + ")!");
                    return(0);
                }
            }
            else
            {
                //In IPv6
                ip  = remoteEP;
                cnt = mSystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);
            }

            remoteEP = ip as IPEndPoint;



            return(cnt);
        }
Esempio n. 5
0
 public void Init()
 {
     IPUtils.CheckSelfIPAddress();
 }