Ejemplo n.º 1
0
        /// <summary>
        /// Connect to client through specified ip and port.
        /// Port can also be "loopback".
        /// </summary>
        public override void Connect(string ip, int port, Action onConnected)
        {
#if TCPCLIENT_DEBUG
            Debug.Log("Connecting to server on: " + ip + ":" + port.ToString());
#endif
#if UNITY_EDITOR || UNITY_STANDALONE
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var endPoint = new IPEndPoint(
                ip == "loopback" ? IPAddress.Loopback : IPAddress.Parse(ip),
                port
                );

            connectState.callback = onConnected;
            client.BeginConnect(endPoint, OnConnected, null);
#elif NETFX_CORE
            client = HoloLensSocket.Connect(ip, port, () => { } +onConnected);
#endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Connect to client through specified ip and port.
        /// Port can also be "any" and "loopback".
        /// </summary>
        public override void Connect(string ip, int port, Action onConnected)
        {
#if TCPSERVER_DEBUG
            Debug.Log("Waiting for client on: " + ip + ":" + port.ToString());
#endif
#if UNITY_EDITOR || UNITY_STANDALONE
            var ipAddress = default(IPAddress);
            if (ip == "any")
            {
                ipAddress = IPAddress.Any;
            }
            else if (ip == "loopback")
            {
                ipAddress = IPAddress.Loopback;
            }
            else
            {
                ipAddress = IPAddress.Parse(ip);
            }

            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(ipAddress, port));
            server.Listen(1);

            connectState.callback = onConnected;
            server.BeginAccept(OnConnected, null);
#elif NETFX_CORE
            HoloLensSocket.Connect(port, (StreamSocket client) =>
            {
                this.client = client;
                if (onConnected != null)
                {
                    onConnected();
                }
            });
#endif
        }