Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        ID      = GameObject.Find("StartClient_Button").GetComponent <IP_Input>().id;
        address = GameObject.Find("StartClient_Button").GetComponent <IP_Input>().ip;

        state = State.SelectHost;

        IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

        System.Net.IPAddress hostAddress = hostEntry.AddressList[0];
        //address = hostAddress.ToString();

        m_connection = new EndPoint_Connection();
        m_connection.connectionIP = hostAddress.ToString();
        m_connection.ID           = ID;

        RequestConnection();

        positionSync.ID = ID;

        obj = GameObject.Find("Player");

        receive_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        receive_socket.Bind(new IPEndPoint(IPAddress.Any, receive_port));

        state = State.SendPosition;

        var thread = new Thread(Loop);

        thread.Start();
    }
Beispiel #2
0
    public void WaitConnection()
    {
        byte[]     buffer       = new byte[1400];
        IPEndPoint sender       = new IPEndPoint(IPAddress.Any, 0);
        EndPoint   senderRemote = (EndPoint)sender;

        if (firstConnection_socket.Poll(0, SelectMode.SelectRead))
        {
            int recvSize = firstConnection_socket.ReceiveFrom(buffer, SocketFlags.None, ref senderRemote);
            if (recvSize > 0)
            {
                GCHandle            gch  = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                EndPoint_Connection temp = (EndPoint_Connection)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(EndPoint_Connection));
                bool t = false;
                for (int i = 0; i < connections.Count; i++)
                {
                    if (connections[i].connectionIP.Equals(temp.connectionIP))
                    {
                        t = true;
                        break;
                    }
                }

                if (t == false)
                {
                    connections.Add(temp);
                    Debug.Log("Add new end point:" + temp.connectionIP);
                }
                gch.Free();
            }
        }
    }