Example #1
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);
            }
        }
Example #2
0
        /// <summary>
        /// sends file to client
        /// </summary>
        /// <param name="client"></param>
        /// <param name="fileLocationPlusName"></param>
        private void SendFile(LClient client, string fileLocationPlusName)
        {
            string fname = fileLocationPlusName;

            try
            {
                //file doesnt exist.
                if (!File.Exists(fname))
                    return;

                Byte[] buffer = new Byte[1024];

                string[] words = fname.Split(new Char[] { '\\' });

                int i = 0;
                for (i = 0; i < words.Length; i++) { }

                using (FileStream reader = new FileStream(fname, FileMode.Open, FileAccess.Read))
                {
                    long l = reader.Length;

                    this.SendToClient(client, EMessageCode.MessageFile, "Admin|" + l + "|" + words[i - 1]);

                    while (reader.Read(buffer, 0, 1024) > 0)
                    {
                        client.MySocket.Send(buffer, 0, 1024, SocketFlags.None);
                    }
                    System.Threading.Thread.Sleep(1000);
                    reader.Dispose();
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("Error reading file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
Example #3
0
        /// <summary>
        /// recieves file from client
        /// </summary>
        /// <param name="client"></param>
        /// <param name="length"></param>
        /// <param name="filename"></param>
        private void RecieveFile(LClient client, long length, string filename)
        {
            //client agreed
            int max = 1024;
            Byte[] data = new Byte[max];
            long l = length;
            if (!Directory.Exists("RecievedFiles"))
                Directory.CreateDirectory("RecievedFiles");
            string fname = "RecievedFiles\\" + filename;

            try
            {
                using (FileStream write = new FileStream(fname, FileMode.Create, FileAccess.ReadWrite))
                {
                    while (client.MySocket.Receive(data, 0, max, SocketFlags.None) > 0 && l > 0)
                    {
                        write.Write(data, 0, max);
                        write.Flush();
                        l -= data.Length;
                    }
                    System.Threading.Thread.Sleep(300);
                    write.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error creating file:\n\n" + e, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

            this.SendToClient(client, EMessageCode.GreatSuccess, (char)EMessageCode.MessageFile + "|" + (char)EMessageCode.GreatSuccess);
        }
Example #4
0
        /// <summary>
        /// relays file from one client to another
        /// </summary>
        /// <param name="clientSender"></param>
        /// <param name="clientReciever"></param>
        /// <param name="length"></param>
        /// <param name="filename"></param>
        private void RelayFile(LClient clientSender, LClient clientReciever, long length, string filename)
        {
            try
            {
                long le = length;
                long countR = 0;
                Byte[] buffer = new Byte[1024];

                this.SendToClient(clientReciever, EMessageCode.MessageFile, clientSender.UserName + "|" + length + "|" + filename);

                while (countR < length)
                {
                    if (clientSender.MySocket.Receive(buffer) > 0)
                        countR += clientReciever.MySocket.Send(buffer);
                }
            }
            catch(SocketException e)
            {
                Console.WriteLine("RelayFile FAILED");
            }
        }
Example #5
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;
                }
            }
        }