Esempio n. 1
0
        /// <summary>
        /// Method for exchanging messages with the server.
        /// </summary>
        public void OpenStream()
        {
            try
            {
                NetworkStream = TcpClient.GetStream();
                string message = string.Empty;
                while (true)
                {
                    message = string.Format("Client name: {0}; Client message: {1}", Name, Message);
                    NetworkStreamIO.SendMessage(message, NetworkStream);
                    string gettingMessage = NetworkStreamIO.GetMessage(NetworkStream);

                    GetNewMessage(gettingMessage);
                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                Debug.WriteLine("Client stop");
                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
            }
        }
        /// <summary>
        /// Method for exchanging messages with the client.
        /// </summary>
        public void OpenStream()
        {
            try
            {
                NetworkStream = TcpClient.GetStream();

                while (true)
                {
                    try
                    {
                        string gettingMessage = NetworkStreamIO.GetMessage(NetworkStream);

                        GetNewMessage(gettingMessage, ClientId);

                        NetworkStreamIO.SendMessage("Message from server received, Транслитом: сообщение от сервера принято", NetworkStream);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Client with id: " + ClientId + " disconected");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Server.ClientDisconnected(this);

                if (NetworkStream != null)
                {
                    NetworkStream.Close();
                }

                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Method for exchanging messages with the server.
        /// </summary>
        public void OpenStream()
        {
            int messageCounter = default;

            try
            {
                NetworkStream = TcpClient.GetStream();
                string message = string.Empty;
                while (true)
                {
                    message = string.Format("Client name: {0}; Client message: {1}", Name, Message);

                    NetworkStreamIO.SendMessage(message, NetworkStream);
                    string gettingMessage = NetworkStreamIO.GetMessage(NetworkStream);

                    GetNewMessage(gettingMessage);
                    messageCounter++;

                    if (messageCounter == NumOfMessages)
                    {
                        Console.WriteLine("Number of messages reached the limit " + NumOfMessages);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Client stop");
                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
            }
        }