Example #1
0
        public void Listener()
        {
            if (server != null)
            {
                while (!stop)
                {
                    client = server.Accept();
                    Console.WriteLine("Client connected from: " + (( IPEndPoint )client.RemoteEndPoint).Address + ":" + (( IPEndPoint )client.RemoteEndPoint).Port);
                    /* Message chat system only for one client implementation */

                    MessageCommunication ms = new MessageCommunication(client);
                    ChatManager          cm = new ChatManager();
                    Message     message     = null;
                    TextMessage textMessage;

                    // Message
                    while (client.Connected)
                    {
                        message = ms.Recive();
                        if (message != null)
                        {
                            if (message is TextMessage)
                            {
                                textMessage = ( TextMessage )message;
                                TimeSpan MessageTime = new TimeSpan(textMessage.Date);

                                // Latency, message created to message recived
                                double latency = ((DateTime.Now.ToUniversalTime().Ticks - textMessage.Date) / 10000);

                                // if type of text message is ToAll
                                if (textMessage.Type == TextMessageType.ToAll)
                                {
                                    Console.WriteLine("MessageTime: " + MessageTime.Hours + ":" + MessageTime.Minutes + ":" + MessageTime.Seconds + " Latency: " + latency + "ms Message: " + textMessage.Text);
                                }
                            }
                        }


                        // Sending message to Server
                        textMessage = cm.CreateTextMessage("Hello client");
                        Console.WriteLine("[Server] Server ---> Client | Sending message");
                        ms.Send(textMessage);
                    } // End of message
                    Console.WriteLine("Client: " + (( IPEndPoint )client.RemoteEndPoint).Address + ":" + (( IPEndPoint )client.RemoteEndPoint).Port + " disconnected");
                    /* ... */
                }
            }
        }
Example #2
0
        public void Listener()
        {
            if (client != null && client.Connected)
            {
                ms = new MessageCommunication(client);
                ChatManager cm      = new ChatManager();
                Message     message = null;
                TextMessage textMessage;

                // Message
                while (client.Connected)
                {
                    // Sending message to Server
                    textMessage = cm.CreateTextMessage("Hello server");
                    Console.WriteLine("[Client] Client ---> Server | Sending message");
                    ms.Send(textMessage);

                    // Recive message from Server
                    message = ms.Recive();

                    if (message != null)
                    {
                        Console.WriteLine("[Client] Server ---> Client | Recived message");
                        if (message is TextMessage)
                        {
                            textMessage = ( TextMessage )message;

                            TimeSpan MessageTime = new TimeSpan(textMessage.Date);
                            // Latency, message created to message recived
                            double latency = ((DateTime.Now.ToUniversalTime().Ticks - textMessage.Date) / 10000);

                            // if type of text message is ToAll
                            if (textMessage.Type == TextMessageType.ToAll)
                            {
                                Console.WriteLine("MessageTime: " + MessageTime.Hours + ":" + MessageTime.Minutes + ":" + MessageTime.Seconds + " Latency: " + latency + "ms Message: " + textMessage.Text);
                            }
                        }
                    }


                    Thread.Sleep(1000);
                } // End of message
            }
            Console.Write("Lost connection to server, press any key to exit.");
            Console.Read();
        }