Exemple #1
0
        /// <summary>
        /// relays message to all clients
        /// </summary>
        /// <param name="code"></param>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        private void RelayToAllClients(EMessageCode code, string sender, string message)
        {
            if (this.clientsDict == null || this.clientsDict.Count == 0)
            {
                return;
            }

            foreach (KeyValuePair <string, LClient> entry in this.clientsDict)
            {
                this.RelayToClient(entry.Value, code, sender, message);
            }
        }
Exemple #2
0
        /// <summary>
        /// relays message from client to another client
        /// </summary>
        /// <param name="cl"></param>
        /// <param name="code"></param>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        private void RelayToClient(LClient cl, EMessageCode code, string sender, string message)
        {
            try
            {
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes((((char)code) + "|" + sender + "|" + message + "|").ToCharArray());

                buffer = this.EncrptyDecrypt(buffer);

                cl.MySocket.Send(buffer, buffer.Length, 0);
            }
            catch (Exception e)
            {
                this.RemoveListBox(this.listBoxUsers, cl.LogData());
                cl.Shutdown();
                this.clientsDict.Remove(cl.UserName);
            }
        }
Exemple #3
0
        /// <summary>
        /// sends a message over network through server
        /// </summary>
        /// <param name="code">type of message</param>
        /// <param name="message">message contents</param>
        private void Message(EMessageCode code, string message)
        {
            try
            {
                //create string for message
                string m = ((char)code) + "|" + this.txtBoxUsername.Text + "|" + message + "|";
                //load string into buffer
                Byte[] buffer = System.Text.Encoding.ASCII.GetBytes(m.ToCharArray());

                //encrypt buffer
                buffer = this.EncrptyDecrypt(buffer);

                //write buffer to stream
                this.netStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception e)
            {
                this.SetText(this.labelStatus, "ERROR: Failed to send message.");
                Console.WriteLine("ERROR: Unable to send message");
            }
        }
Exemple #4
0
        /// <summary>
        /// recieves messages and processes
        /// </summary>
        /// <param name="client"></param>
        private void Recieve(Socket client)
        {
            bool isLooping = true;

            while (isLooping)
            {
                try
                {
                    Byte[] buffer = new Byte[2048];
                    client.Receive(buffer);

                    //decrypt file
                    buffer = this.EncrptyDecrypt(buffer);

                    string message = System.Text.Encoding.ASCII.GetString(buffer);

                    string[] tokens = message.Split(new Char[] { '|' });

                    //pulls code from recieved message
                    EMessageCode code = (EMessageCode)tokens[0][0];

                    switch (code)
                    {
                    case EMessageCode.None:
                    {
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.HandShake:
                    {
                        this.LogMessage(tokens);
                        //checks credentials
                        if (this.CheckCredentials(tokens[1], tokens[2]))
                        {
                            //checks if the client name isn't already in use.
                            if (!this.clientsDict.ContainsKey(tokens[1]))
                            {
                                //get endpoint for creating an LClient
                                EndPoint ep = client.RemoteEndPoint;
                                LClient  c  = new LClient(tokens[1], ep, clientService, client);

                                //add client into dictionary
                                this.clientsDict.Add(c.UserName, c);

                                //message client a handshake
                                this.SendToClient(c, EMessageCode.HandShake, "Very nice");

                                //add the client data to the list box.
                                this.AddListBox(this.listBoxUsers, c.LogData());

                                //update log reader
                                this.UpdateReader(c.UserName + " has Connected.");

                                //pause for the client to setup before receiving next message.
                                System.Threading.Thread.Sleep(250);

                                //send messages to all connected clients that a new client is connected.
                                //also sends a message to the client that connected with all connected clients.
                                foreach (KeyValuePair <string, LClient> entry in this.clientsDict)
                                {
                                    this.SendToClient(entry.Value, EMessageCode.UserConnected, c.UserName);
                                    this.SendToClient(c, EMessageCode.UsersOnline, entry.Value.UserName);
                                    System.Threading.Thread.Sleep(50);
                                }
                            }
                            else
                            {
                                //mulitple users logged in with the name. rejects connection.
                                string doubleTemp = "DOUBLE ACCESS:\nAttempting access: " + tokens[1] + "Current Logged in User: "******"Attempting Access: " + client.RemoteEndPoint.ToString();
                                //log warning added
                                this.UpdateReader(doubleTemp);
                                byte[] bufferUser = System.Text.Encoding.ASCII.GetBytes((((byte)EMessageCode.InvalidUsername) + "|Admin|User name in use.").ToCharArray());
                                client.Send(bufferUser, bufferUser.Length, 0);
                            }
                        }
                        else
                        {
                            //user tried to log in with invalid password. rejects connection obviously.
                            string passTemp = "INVALID PASSWORD: \n" + "User: "******"IP: " + client.RemoteEndPoint.ToString();
                            //log warning added
                            this.UpdateReader(passTemp);
                            byte[] bufferPass = System.Text.Encoding.ASCII.GetBytes((((byte)EMessageCode.InvalidPassword) + "|Admin|Invalid password.").ToCharArray());
                            client.Send(bufferPass, bufferPass.Length, 0);
                        }
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.GreatSuccess:
                    {
                        this.LogMessage(tokens);
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.MessageAll:
                    {
                        this.LogMessage(tokens);
                        string m = "";

                        for (int i = 2; i < tokens.Length - 1; i++)
                        {
                            m += tokens[i] + "|";
                        }

                        this.RelayToAllClients(EMessageCode.MessageAll, tokens[1], m);

                        isLooping = false;
                    }
                    break;

                    case EMessageCode.MessageWhisper:
                    {
                        this.LogMessage(tokens);
                        string m = "";

                        for (int i = 2; i < tokens.Length - 1; i++)
                        {
                            m += tokens[i] + "|";
                        }

                        if (this.clientsDict.ContainsKey(tokens[2]))
                        {
                            this.RelayToClient(this.clientsDict[tokens[2]], EMessageCode.MessageWhisper, tokens[1], m);
                        }
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.MessageFile:
                    {
                        this.LogMessage(tokens);
                        this.RelayFile(this.clientsDict[tokens[1]], this.clientsDict[tokens[2]], Convert.ToInt64(tokens[3]), tokens[4]);
                        //the below method was a method that would recieve files automatically from the client when sent
                        //this.RecieveFile(this.clientsDict[tokens[1]], this.clientsDict[tokens[2]], Convert.ToInt64(tokens[3]), tokens[4]);
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.UserDisconnect:
                    {
                        this.LogMessage(tokens);
                        this.RelayToAllClients(EMessageCode.UserDisconnect, tokens[1], "bye bye\n");
                        this.UpdateReader(tokens[1] + " has left the server.");
                        if (this.clientsDict.ContainsKey(tokens[1]))
                        {
                            this.RemoveListBox(this.listBoxUsers, this.clientsDict[tokens[1]].LogData());
                            this.clientsDict[tokens[1]].Shutdown();
                            this.clientsDict.Remove(tokens[1]);
                        }
                        isLooping = false;
                    }
                    break;

                    case EMessageCode.UserConnected:
                    {
                        this.LogMessage(tokens);
                        this.UpdateReader(tokens[1] + " has joined the server.");
                        isLooping = false;
                    }
                    break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    isLooping = false;
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// sents message to specific client
        /// </summary>
        /// <param name="cl"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        private void SendToClient(LClient cl, EMessageCode code, string message)
        {
            try
            {
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes((((char)code) + "|Admin|" + message + "|\n").ToCharArray());

                buffer = this.EncrptyDecrypt(buffer);

                cl.MySocket.Send(buffer, buffer.Length, 0);
            }
            catch (Exception e)
            {
                this.RemoveListBox(this.listBoxUsers, cl.LogData());
                cl.Shutdown();
                this.clientsDict.Remove(cl.UserName);
            }
        }
Exemple #6
0
        /// <summary>
        /// sends message to all clients
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        private void SendToAllClients(EMessageCode code, string message)
        {
            if (this.clientsDict == null || this.clientsDict.Count == 0) return;

            foreach(KeyValuePair<string,LClient> entry in this.clientsDict)
            {
                this.SendToClient(entry.Value, code, message);
            }
        }
Exemple #7
0
        /// <summary>
        /// sends a message over network through server
        /// </summary>
        /// <param name="code">type of message</param>
        /// <param name="message">message contents</param>
        private void Message(EMessageCode code, string message)
        {
            try
            {
                //create string for message
                string m = ((char)code) + "|" + this.txtBoxUsername.Text + "|" + message + "|";
                //load string into buffer
                Byte[] buffer = System.Text.Encoding.ASCII.GetBytes(m.ToCharArray());

                //encrypt buffer
                buffer = this.EncrptyDecrypt(buffer);

                //write buffer to stream
                this.netStream.Write(buffer, 0, buffer.Length);
            }
            catch(Exception e)
            {
                this.SetText(this.labelStatus, "ERROR: Failed to send message.");
                Console.WriteLine("ERROR: Unable to send message");
            }
        }