/// <summary> /// send message button activated /// </summary> private void SendMessageActivated() { if (!this.isRunning) { return; } switch (this.comboBoxMessageType.SelectedItem.ToString()) { case "All": this.SendToAllClients(EMessageCode.MessageAll, this.txtBoxCommand.Text); break; case "Whisper": { LClient c = GetSelectedClient(); if (c == null) { return; } this.SendToClient(c, EMessageCode.MessageWhisper, this.txtBoxCommand.Text); } break; case "File": { //tests if a client was selected LClient temp = this.GetSelectedClient(); //return if no user selected or if you have yourself selected if (temp == null) { return; } //send a file to the user this.SendFile(temp, this.txtBoxCommand.Text); } break; case "Kick": { LClient c = GetSelectedClient(); if (c == null) { return; } this.SendToClient(c, EMessageCode.DropConnection, this.txtBoxCommand.Text); } break; } this.UpdateReader(this.comboBoxMessageType.SelectedItem.ToString() + " : Admin : " + this.txtBoxCommand.Text + Environment.NewLine); System.Threading.Thread.Sleep(100); this.SetText(this.txtBoxCommand, ""); }
public HomeController( UserManager <IdentityUser> userManager, SignInManager <IdentityUser> signInManager, RoleManager <IdentityRole> roleManager, IServiceProvider serviceProvider) { client = new LClient(userManager, signInManager, roleManager); }
/// <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); } }
/// <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); } }
private void saveButton_Click(object sender, EventArgs e) { if (isPhys) { if (!Utils.IsNotEmptyTextInputs(pSurname, pName, pPatronymic, pAddress, pMobile, pEmail) || !Utils.IsValidFullName(pSurname, pName, pPatronymic) || !Utils.IsValidMobile(pMobile) || !Utils.IsValidEmail(pEmail)) { return; } pClient = new PClient( pSurname.Text, pName.Text, pPatronymic.Text, pAddress.Text, Convert.ToInt64(pMobile.Text), pEmail.Text ); } else { if (!Utils.IsNotEmptyTextInputs(lName, lAddress, lMobile, lEmail) || !Utils.IsValidMobile(lMobile) || !Utils.IsValidEmail(lEmail)) { return; } lClient = new LClient( lName.Text, lAddress.Text, Convert.ToInt64(lMobile.Text), lEmail.Text ); } IsSaved = true; this.Close(); }
/// <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"); } }
public EditClientsForm(bool isPhys, DataGridViewRow row) : this(isPhys) { if (isPhys) { pClient = new PClient( (string)row.Cells[0].Value, (string)row.Cells[1].Value, (string)row.Cells[2].Value, (string)row.Cells[3].Value, (long)row.Cells[4].Value, (string)row.Cells[5].Value ); } else { lClient = new LClient( (string)row.Cells[0].Value, (string)row.Cells[1].Value, (long)row.Cells[2].Value, (string)row.Cells[3].Value ); } }
/// <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); }
/// <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; } } }