예제 #1
0
        /// <summary>
        /// Server listens to specified port and accepts connection from client
        /// </summary>
        public async void StartListen()
        {
            var ip = (networkInterface != null)
                ? GetInterfaceIpAddress()
                : IPAddress.Any;

            tcpServer = new StreamSocketListener();
            await tcpServer.BindServiceNameAsync(portNumber.ToString());

            tcpServer.ConnectionReceived += TcpServer_ConnectionReceived;

            isRunning = true;
            // Keep accepting client connection
            while (isRunning)
            {
                if (!tcpServer.Pending())
                {
                    Thread.Sleep(500);
                    continue;
                }
                // New client is connected, call event to handle it
                var clientThread = new Thread(NewClient);
                var tcpClient    = tcpServer.AcceptTcpClient();
                tcpClient.ReceiveTimeout = 20000;
                clientThread.Start(tcpClient.Client);
            }
        }