Пример #1
0
        //Task.Run(() => ReceiveMessageAsync());
        public async Task <bool> ReceiveMessageAsync(bool IsHost)
        {
            ServerSocket serverSocket = null;
            Socket       client       = null;

            while (true)
            {
                try
                {
                    if (serverSocket == null)
                    {
                        serverSocket = new ServerSocket(8888);
                        client       = await serverSocket.AcceptAsync();
                    }
                    WifiManager.DeviceMessage.ReceivedError = "";
                    WifiManager.DeviceMessage.IsHost        = IsHost;

                    var          inputstream = client.InputStream;
                    StreamReader reader      = new StreamReader(inputstream);
                    WifiManager.DeviceMessage.ReceivedMessage = reader.ReadToEnd();
                    WifiManager.DeviceMessage.ReceivedFrom    = client.InetAddress.ToString().Replace(@"/", "");
                    serverSocket.Close();
                    serverSocket = null;
                    client       = null;
                }
                catch (Exception ex)
                {
                    WifiManager.DeviceMessage.ReceivedError = ex.Message;
                }
            }
        }
Пример #2
0
        private void ShakeHandAsync()
        {
            var evetArgs = ShakeHandEventPool.Pop();

            //异步接收连接
            if (!ServerSocket.AcceptAsync(evetArgs))
            {
                //如果异步接收失败则同步接收
                ProcessAccept(evetArgs);
            }
        }
Пример #3
0
        private async void Run()
        {
            ServerSocket.Listen(200);

            while (!Exiting)
            {
                var client = await ServerSocket.AcceptAsync();

                var mqClient = new MessageQueueClient(client, OnMessage);

                Log.LogInformation("Client connected");

                Clients.Add(mqClient);

                mqClient.Start();
            }

            ServerSocket.Shutdown(SocketShutdown.Both);
            ServerSocket.Dispose();
        }