Пример #1
0
        public void ListenTcp()
        {
            int receivedDataBytesCount;

            byte[] receivedDataBuffer;
            tcpSocket.Listen(ClientsLimits);
            while (true)
            {
                try
                {
                    Socket connectedSocket = tcpSocket.Accept();
                    receivedDataBuffer = new byte[1024];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        do
                        {
                            receivedDataBytesCount = connectedSocket.Receive(receivedDataBuffer, receivedDataBuffer.Length, SocketFlags.None);
                            memoryStream.Write(receivedDataBuffer, 0, receivedDataBytesCount);
                        }while (udpSocket.Available > 0);
                        if (receivedDataBytesCount > 0)
                        {
                            HandleReceivedMessage(messageSerializer.Deserialize(memoryStream.ToArray()), connectedSocket);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }
Пример #2
0
        public void ListenTcp()
        {
            int receivedDataBytesCount;

            byte[] receivedDataBuffer;
            try
            {
                while (true)
                {
                    receivedDataBuffer = new byte[1024];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        do
                        {
                            receivedDataBytesCount = tcpSocket.Receive(receivedDataBuffer, receivedDataBuffer.Length, SocketFlags.None);
                            memoryStream.Write(receivedDataBuffer, 0, receivedDataBytesCount);
                        }while (tcpSocket.Available > 0);
                        if (receivedDataBytesCount > 0)
                        {
                            HandleReceivedMessage(messageSerializer.Deserialize(memoryStream.ToArray()));
                        }
                    }
                }
            }
            catch (SocketException)
            {
                DisconnectFromServer();
            }
        }
Пример #3
0
        private void ListenTcp()
        {
            int ReceiveDataBytesCount;

            byte[] ReceiveDataBuffer;
            while (true)
            {
                try
                {
                    ReceiveDataBuffer = new byte[1024];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        do
                        {
                            ReceiveDataBytesCount = tcpSocket.Receive(ReceiveDataBuffer, ReceiveDataBuffer.Length, SocketFlags.None);
                            memoryStream.Write(ReceiveDataBuffer, 0, ReceiveDataBytesCount);
                        }while (tcpSocket.Available > 0);
                        if (ReceiveDataBytesCount > 0)
                        {
                            ReceiveMessageEvent(messageSerializer.Deserialize(memoryStream.ToArray()));
                        }
                    }
                }
                catch (SocketException)
                {
                    ClientDisconnectedEvent(this);
                    FunctionsCommon.CloseAndNullSocket(ref tcpSocket);
                    FunctionsCommon.CloseAndNullThread(ref listenTcpThread);
                }
            }
        }