Beispiel #1
0
        public async void Listen()
        {
#if NETFX_CORE
            listener = new StreamSocketListener();
            listener.ConnectionReceived += NewConnection;
            listener.Control.KeepAlive   = false;

            try
            {
                await listener.BindServiceNameAsync(Port.ToString());
            }
            catch (Exception exception)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }

                UDebug.LogException(exception);
            }
#else
            TcpListener server = null;
            try
            {
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");

                server = new TcpListener(localAddr, Port);

                // start listening for client requests.
                server.Start();

                // enter the listening loop
                while (true)
                {
                    // perform a blocking call to accept requests.
                    using (TcpClient tcpClient = server.AcceptTcpClient())
                    {
                        USocketClient client = new USocketClient(tcpClient);
                        ConnectionReceived?.Invoke(this, client);
                    }
                }
            }
            catch (Exception e)
            {
                UDebug.LogException(e);
            }
            finally
            {
                // stop listening for new clients
                server.Stop();
            }
#endif
        }
Beispiel #2
0
        private void NewConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            USocketClient client = new USocketClient(args.Socket);

            ConnectionReceived?.Invoke(this, client);
        }