コード例 #1
0
        private void ListenThread()
        {
            Task clientTask = new Task((c) =>
            {
                while (true)
                {
                    Byte[] bytes;
                    try
                    {
                        if (!TCPUtilities.IsConnected(tcpClient))
                        {
                            throw new Exception("Client Disconnected");
                        }

                        NetworkStream ns = tcpClient.GetStream();
                        if (tcpClient.ReceiveBufferSize > 0)
                        {
                            bytes = new byte[tcpClient.ReceiveBufferSize];
                            ns.Read(bytes, 0, tcpClient.ReceiveBufferSize);
                            string msg = Encoding.ASCII.GetString(bytes);

                            ReceivedMessage message = new ReceivedMessage((Client)c, msg);

                            if (onReceiveCallback != null)
                            {
                                onReceiveCallback.Invoke(message);
                            }
                        }
                    }
                    catch
                    {
                        if (onDisconnectCallBack != null)
                        {
                            onDisconnectCallBack.Invoke((Client)c);
                        }

                        break;
                    }
                }
            }, client);

            clientTask.Start();
            ListenTask = clientTask;
        }
コード例 #2
0
        private void StartListeningThread()
        {
            Thread listenConnection = new Thread(async() =>
            {
                while (true)
                {
                    TcpClient tcpClient = null;
                    try
                    {
                        tcpClient = await Server.AcceptTcpClientAsync();
                    }
                    catch (ObjectDisposedException) { break; }

                    string[] addressArray = tcpClient.Client.RemoteEndPoint.ToString().Split(':');
                    Client client         = new Client(addressArray[0], int.Parse(addressArray[1]));
                    ClientToTCPMap.Add(client, tcpClient);

                    if (onConnectCallBack != null)
                    {
                        onConnectCallBack.Invoke(client);
                    }


                    Task clientTask = new Task(async(c) =>
                    {
                        Byte[] bytes;
                        while (true)
                        {
                            try
                            {
                                if (!TCPUtilities.IsConnected(tcpClient))
                                {
                                    throw new Exception("Client Disconnected");
                                }

                                NetworkStream ns = tcpClient.GetStream();

                                if (tcpClient.ReceiveBufferSize > 0)
                                {
                                    string receivedMessage = string.Empty;

                                    do
                                    {
                                        bytes = new byte[tcpClient.ReceiveBufferSize];
                                        ns.Read(bytes, 0, tcpClient.ReceiveBufferSize);
                                        string msg       = Encoding.ASCII.GetString(bytes);
                                        receivedMessage += msg;
                                        await Task.Delay(1);
                                    }while (ns.DataAvailable);

                                    ReceivedMessage message = null;
                                    message = new ReceivedMessage((Client)c, receivedMessage);


                                    if (onReceiveCallback != null)
                                    {
                                        onReceiveCallback.Invoke(message);
                                    }
                                }
                            }
                            catch
                            {
                                ClientToTCPMap.Remove((Client)c);
                                if (onDisconnectCallBack != null)
                                {
                                    onDisconnectCallBack.Invoke((Client)c);
                                }
                                break;
                            }
                        }
                    }, client);
                    clientTask.Start();
                    clientTasks.Add(clientTask);
                }
            });

            listenConnection.Start();
        }