Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////// Server //////////////////
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace NetworkTest
- {
- public partial class Form1 : Form
- {
- // Server variables:
- public string recieve;
- public String TextToSend;
- string displayIP = "";
- bool isStarted = false;
- TcpListener listener;
- List<TcpClient> clientList = new List<TcpClient>();
- List<StreamReader> readerList = new List<StreamReader>();
- List<StreamWriter> writerList = new List<StreamWriter>();
- // Tracker variables:
- List<string> blockedApps = new List<string>();
- List<string> employeesList = new List<string>();
- public Form1()
- {
- InitializeComponent();
- // Get local IP address and add it to txtServerIP:
- IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
- foreach (IPAddress address in localIP)
- {
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
- displayIP = address.ToString();
- txtServerIP.Text = displayIP;
- }
- }
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- // Start listening for TCP connections:
- listener = new TcpListener(IPAddress.Any, int.Parse(txtServerPort.Text));
- listener.Start();
- txtChat.AppendText($"Server started on IP: {displayIP} and Port: {txtServerPort.Text}\n");
- isStarted = true;
- Server.RunWorkerAsync();
- }
- private void Server_DoWork(object sender, DoWorkEventArgs e)
- {
- while (true)
- {
- TcpClient client = listener.AcceptTcpClient();
- clientList.Add(client);
- StreamReader STR = new StreamReader(client.GetStream());
- readerList.Add(STR);
- StreamWriter STW = new StreamWriter(client.GetStream());
- writerList.Add(STW);
- STW.AutoFlush = true;
- if (!backgroundWorker1.IsBusy && !backgroundWorker2.IsBusy)
- {
- backgroundWorker1.RunWorkerAsync();
- backgroundWorker2.WorkerSupportsCancellation = true;
- }
- }
- }
- // Recieve messages from the employee's computer:
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- try
- {
- foreach (StreamReader str in readerList)
- {
- recieve = str.ReadLine();
- this.txtChat.Invoke(new MethodInvoker(delegate ()
- {
- txtChat.AppendText(recieve + "\n");
- if (recieve.Contains(">"))
- {
- // Add employee name to connected list:
- string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
- employeesList.Add(name);
- lbConnected.Items.Add(name);
- }
- else if (recieve.Contains("<"))
- {
- string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
- employeesList.Remove(name);
- lbConnected.Items.Clear();
- foreach (string employee in employeesList)
- {
- lbConnected.Items.Add(employee);
- }
- }
- }));
- recieve = "";
- }
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message.ToString());
- }
- }
- // Send messages to employee's computer:
- private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
- {
- foreach(TcpClient tcp in clientList)
- {
- if (tcp.Connected)
- {
- foreach(StreamWriter stw in writerList)
- {
- stw.WriteLine(TextToSend);
- }
- }
- else
- {
- MessageBox.Show("Sending failed");
- }
- }
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- if (isStarted)
- {
- foreach(TcpClient tcp in clientList)
- {
- if (tcp.Connected)
- {
- if (txtMessage.Text != "")
- {
- TextToSend = txtMessage.Text.ToLower();
- // Block specified apps:
- if (TextToSend.Contains("block"))
- {
- backgroundWorker2.RunWorkerAsync();
- string blocked = TextToSend;
- txtChat.AppendText($"You have blocked the app: {blocked} \n");
- blockedApps.Add(blocked);
- lbBlocked.Items.Add($"*{blocked.Split(new[] { "block " }, StringSplitOptions.None)[1]}* is blocked");
- }
- else if (TextToSend.Contains("clear") || TextToSend.Contains("cls"))
- {
- txtChat.Clear();
- }
- else if (TextToSend.Contains("notify"))
- {
- backgroundWorker2.RunWorkerAsync();
- string notifiedEmp = TextToSend.Split(new string[] { "notify " }, StringSplitOptions.None)[1].Split('"')[0].Trim();
- string message = TextToSend.Split(new string[] { "\"" }, StringSplitOptions.None)[1].Split('"')[0].Trim();
- txtChat.AppendText($"Employee {notifiedEmp} has been notified \"{message}\"\n");
- }
- }
- txtMessage.Text = "";
- }
- else
- {
- MessageBox.Show("You don't have any connected employees!", "Error");
- }
- }
- }
- else
- {
- MessageBox.Show("You haven't started your server!", "Error");
- }
- }
- private void txtConnected_SelectedIndexChanged(object sender, EventArgs e)
- {
- string userInfo = "";
- foreach(string name in employeesList)
- {
- // If the employer has selected to look at a certain employee:
- if(lbConnected.GetItemText(lbConnected.SelectedItem) == name)
- {
- // Search for info pertaining to said employee
- /*foreach(string info in lbChat.Items)
- {
- if (info.Contains(name))
- {
- userInfo += info + "\n";
- }
- }*/
- // Display all info related to that employee:
- MessageBox.Show(userInfo, $"Info for: {name}");
- }
- }
- }
- private void txtMessage_KeyDown(object sender, KeyEventArgs e)
- {
- if(e.KeyCode == Keys.Enter)
- {
- btnSend.PerformClick();
- txtMessage.Clear();
- }
- }
- }
- }
- ////////////////// Client //////////////////
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Diagnostics;
- namespace Client
- {
- public partial class Form1 : Form
- {
- // Network variables:
- private TcpClient client;
- public StreamReader STR;
- public StreamWriter STW;
- public string recieve;
- public String TextToSend;
- bool connected = false;
- // App tracker variables:
- List<string> blockedApps = new List<string>();
- List<string> blockedRunning = new List<string>();
- string name = "";
- string displayIP;
- public Form1()
- {
- InitializeComponent();
- TextBox.CheckForIllegalCrossThreadCalls = false;
- blockedRunning.Add("ICXCNIKA");
- IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
- foreach (IPAddress address in localIP)
- {
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
- displayIP = address.ToString();
- txtClientIP.Text = displayIP;
- }
- }
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- if(txtName.Text != "")
- {
- client = new TcpClient();
- IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(txtClientIP.Text), int.Parse(txtClientPort.Text));
- try
- {
- // Try to connect to the server:
- client.Connect(IpEnd);
- // If connection is successful start readers, writers, timer, and minimize window:
- if (client.Connected)
- {
- STW = new StreamWriter(client.GetStream());
- STR = new StreamReader(client.GetStream());
- STW.AutoFlush = true;
- backgroundWorker1.RunWorkerAsync();
- backgroundWorker2.WorkerSupportsCancellation = true;
- connected = true;
- tmrCheck.Start();
- name = txtName.Text;
- TextToSend = $"> Employee '{name}' has connected";
- txtText.AppendText($"You have successfully connected to {displayIP} on port {txtClientPort.Text}!\n");
- if (!backgroundWorker2.IsBusy)
- {
- backgroundWorker2.RunWorkerAsync();
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- else
- {
- MessageBox.Show("Please enter a name", "Error");
- }
- }
- // Communicate with employer's computer:
- private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
- {
- if (client.Connected)
- {
- STW.WriteLine(TextToSend);
- }
- else
- {
- MessageBox.Show("Sending failed");
- }
- backgroundWorker2.CancelAsync();
- }
- private void tmrCheck_Tick(object sender, EventArgs e)
- {
- if (connected)
- {
- foreach (Process item in Process.GetProcesses())
- {
- foreach(string blocked in blockedApps)
- {
- // If a blocked process is running and has not been accounted for yet:
- if (item.ProcessName.ToString().Contains(blocked) && !blockedRunning.Contains(blocked))
- {
- // Inform the employer:
- TextToSend = $"Your employee {name} has opened a blocked app: {blocked}!";
- txtText.AppendText($"You just opened a blocked app: {blocked}!\n");
- if (!backgroundWorker2.IsBusy)
- {
- backgroundWorker2.RunWorkerAsync();
- }
- // Add to running blocked list:
- blockedRunning.Add(blocked);
- break;
- }
- // If a blocked process is running and it has been accounted for:
- else if (item.ProcessName.ToString().Contains(blocked) && blockedRunning.Contains(blocked))
- {
- // If the blocked process is terminated:
- if (item.HasExited)
- {
- // Remove from running blocked list:
- blockedRunning.Remove(blocked);
- // Inform the employer:
- TextToSend = $"The blocked app {blocked} has been closed!";
- txtText.AppendText($"You closed a blocked app: {blocked}!\n");
- if (!backgroundWorker2.IsBusy)
- {
- backgroundWorker2.RunWorkerAsync();
- }
- break;
- }
- }
- }
- }
- }
- }
- // Recieve messages from employer:
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- while (client.Connected)
- {
- try
- {
- // Recieve and keep track of blocked apps from the employer (live time):
- recieve = STR.ReadLine();
- if (recieve.Contains("block"))
- {
- string tempBlock = recieve.Split(new[] { "block " }, StringSplitOptions.None)[1].ToLower();
- blockedApps.Add(tempBlock);
- txtText.AppendText($"Your employer has blocked the following app: {tempBlock}!\n");
- } else if (recieve.Contains("notify"))
- {
- string notifiedEmp = recieve.Split(new string[] { "notify " }, StringSplitOptions.None)[1].Split('"')[0].Trim();
- if(notifiedEmp == name)
- {
- string message = recieve.Split(new string[] { "\"" }, StringSplitOptions.None)[1].Split('"')[0].Trim();
- txtText.AppendText($"Message from employer: {message}\n");
- }
- }
- recieve = "";
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- TextToSend = $"< Employee '{name}' has disconnected";
- if (!backgroundWorker2.IsBusy)
- {
- backgroundWorker2.RunWorkerAsync();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement