예제 #1
0
 private bool SendMessageToClient(CommJson json)
 {
     foreach (TConnectedClient lCient in clientList)
     {
         if (lCient.ClientName == json.ToC)
         {
             lCient.sendMessage(Newtonsoft.Json.JsonConvert.SerializeObject(json));
             return(true);
         }
         else
         {
             return(false);
         }
     }
     return(false);
 }
예제 #2
0
        // Kommunikation mit dem Client
        public void processClient(Object clientObj)
        {
            bool clientRunning = true;

            // neuen Client in der Liste speichern
            TConnectedClient connectedClient =
                new TConnectedClient((TcpClient)clientObj);

            // Connect des neuen Clients anzeigen
            gui.showServerMessage("Connected on IP: " + connectedClient.ClientEP.Address +
                                  " Port: " + connectedClient.ClientEP.Port);

            // neuen Client in Liste eintragen
            lock (clientList)
            {
                clientList.Add(connectedClient);
            }

            // Connect-Message an Client schicken
            connectedClient.sendMessage("Successfully connection to the server on port:" +
                                        connectedClient.ClientEP.Port);

            // Kommunikations-Schleife
            while (clientRunning)
            {
                try
                {
                    // Warten auf Message vom Client
                    string msg = connectedClient.receiveMessage();

                    CommJson json = Newtonsoft.Json.JsonConvert.DeserializeObject <CommJson>(msg);

                    connectedClient.ClientName = json.FromC;

                    // wenn nicht "Ende"
                    if (msg != "<q>")
                    {
                        // Message im Serverfenster anzeigen
                        gui.showServerMessage(json.FromC + " : " + json.MSG + " to " + json.ToC);

                        SendMessageToClient(json);
                    }
                    else
                    {
                        clientRunning = false;
                    }
                }
                catch (Exception e)
                {
                    gui.showServerMessage("Exception: " + e.Message);
                    clientRunning = false;
                }
            }

            // Client aus der Liste entfernen
            lock (clientList)
            {
                clientList.Remove(connectedClient);
            }

            // und schließen
            connectedClient.Client.Close();

            // Schließen im Serverfenster anzeigen
            gui.showServerMessage("Connection to IP: " + connectedClient.ClientEP.Address +
                                  " Port: " + connectedClient.ClientEP.Port + " closed");
        }