Advertisement
TermSpar

Actual Work Tracker

Jan 12th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.68 KB | None | 0 0
  1. ////////////////// Server //////////////////
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.IO;
  15.  
  16. namespace NetworkTest
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         // Server variables:
  21.         public string recieve;
  22.         public String TextToSend;
  23.         string displayIP = "";
  24.         bool isStarted = false;
  25.         TcpListener listener;
  26.         List<TcpClient> clientList = new List<TcpClient>();
  27.         List<StreamReader> readerList = new List<StreamReader>();
  28.         List<StreamWriter> writerList = new List<StreamWriter>();
  29.  
  30.         // Tracker variables:
  31.         List<string> blockedApps = new List<string>();
  32.         List<string> employeesList = new List<string>();
  33.  
  34.         public Form1()
  35.         {
  36.             InitializeComponent();
  37.  
  38.             // Get local IP address and add it to txtServerIP:
  39.             IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
  40.             foreach (IPAddress address in localIP)
  41.             {
  42.                 if (address.AddressFamily == AddressFamily.InterNetwork)
  43.                 {
  44.                     displayIP = address.ToString();
  45.                     txtServerIP.Text = displayIP;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private void btnStart_Click(object sender, EventArgs e)
  51.         {
  52.             // Start listening for TCP connections:
  53.             listener = new TcpListener(IPAddress.Any, int.Parse(txtServerPort.Text));
  54.             listener.Start();
  55.             txtChat.AppendText($"Server started on IP: {displayIP} and Port: {txtServerPort.Text}\n");
  56.             isStarted = true;
  57.             Server.RunWorkerAsync();
  58.         }
  59.  
  60.         private void Server_DoWork(object sender, DoWorkEventArgs e)
  61.         {
  62.             while (true)
  63.             {
  64.                 TcpClient client = listener.AcceptTcpClient();
  65.                 clientList.Add(client);
  66.                 StreamReader STR = new StreamReader(client.GetStream());
  67.                 readerList.Add(STR);
  68.                 StreamWriter STW = new StreamWriter(client.GetStream());
  69.                 writerList.Add(STW);
  70.                 STW.AutoFlush = true;
  71.  
  72.                 if (!backgroundWorker1.IsBusy && !backgroundWorker2.IsBusy)
  73.                 {
  74.                     backgroundWorker1.RunWorkerAsync();
  75.                     backgroundWorker2.WorkerSupportsCancellation = true;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         // Recieve messages from the employee's computer:
  81.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  82.         {
  83.             try
  84.             {
  85.                 foreach (StreamReader str in readerList)
  86.                 {
  87.                     recieve = str.ReadLine();
  88.  
  89.                     this.txtChat.Invoke(new MethodInvoker(delegate ()
  90.                     {
  91.                         txtChat.AppendText(recieve + "\n");
  92.                         if (recieve.Contains(">"))
  93.                         {
  94.                             // Add employee name to connected list:
  95.                             string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
  96.                             employeesList.Add(name);
  97.                             lbConnected.Items.Add(name);
  98.                         }
  99.                         else if (recieve.Contains("<"))
  100.                         {
  101.                             string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
  102.                             employeesList.Remove(name);
  103.                             lbConnected.Items.Clear();
  104.                             foreach (string employee in employeesList)
  105.                             {
  106.                                 lbConnected.Items.Add(employee);
  107.                             }
  108.                         }
  109.                     }));
  110.                     recieve = "";
  111.                 }
  112.             }
  113.             catch (Exception ex)
  114.             {
  115.                 Console.Write(ex.Message.ToString());
  116.             }
  117.  
  118.         }
  119.  
  120.         // Send messages to employee's computer:
  121.         private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
  122.         {
  123.             foreach(TcpClient tcp in clientList)
  124.             {
  125.                 if (tcp.Connected)
  126.                 {
  127.                     foreach(StreamWriter stw in writerList)
  128.                     {
  129.                         stw.WriteLine(TextToSend);
  130.                     }
  131.                 }
  132.                 else
  133.                 {
  134.                     MessageBox.Show("Sending failed");
  135.                 }
  136.             }
  137.         }
  138.  
  139.         private void btnSend_Click(object sender, EventArgs e)
  140.         {
  141.             if (isStarted)
  142.             {
  143.                 foreach(TcpClient tcp in clientList)
  144.                 {
  145.                     if (tcp.Connected)
  146.                     {
  147.                         if (txtMessage.Text != "")
  148.                         {
  149.                             TextToSend = txtMessage.Text.ToLower();
  150.                             // Block specified apps:
  151.                             if (TextToSend.Contains("block"))
  152.                             {
  153.                                 backgroundWorker2.RunWorkerAsync();
  154.                                 string blocked = TextToSend;
  155.                                 txtChat.AppendText($"You have blocked the app: {blocked} \n");
  156.                                 blockedApps.Add(blocked);
  157.                                 lbBlocked.Items.Add($"*{blocked.Split(new[] { "block " }, StringSplitOptions.None)[1]}* is blocked");
  158.                             }
  159.                             else if (TextToSend.Contains("clear") || TextToSend.Contains("cls"))
  160.                             {
  161.                                 txtChat.Clear();
  162.                             }
  163.                             else if (TextToSend.Contains("notify"))
  164.                             {
  165.                                 backgroundWorker2.RunWorkerAsync();
  166.                                 string notifiedEmp = TextToSend.Split(new string[] { "notify " }, StringSplitOptions.None)[1].Split('"')[0].Trim();
  167.                                 string message = TextToSend.Split(new string[] { "\"" }, StringSplitOptions.None)[1].Split('"')[0].Trim();
  168.                                 txtChat.AppendText($"Employee {notifiedEmp} has been notified \"{message}\"\n");
  169.                             }
  170.                         }
  171.                         txtMessage.Text = "";
  172.                     }
  173.                     else
  174.                     {
  175.                         MessageBox.Show("You don't have any connected employees!", "Error");
  176.                     }
  177.                 }
  178.                
  179.             }
  180.             else
  181.             {
  182.                 MessageBox.Show("You haven't started your server!", "Error");
  183.             }
  184.         }
  185.  
  186.         private void txtConnected_SelectedIndexChanged(object sender, EventArgs e)
  187.         {
  188.             string userInfo = "";
  189.  
  190.             foreach(string name in employeesList)
  191.             {
  192.                 // If the employer has selected to look at a certain employee:
  193.                 if(lbConnected.GetItemText(lbConnected.SelectedItem) == name)
  194.                 {
  195.                     // Search for info pertaining to said employee
  196.                     /*foreach(string info in lbChat.Items)
  197.                     {
  198.                         if (info.Contains(name))
  199.                         {
  200.                             userInfo += info + "\n";
  201.                         }
  202.                     }*/
  203.  
  204.                     // Display all info related to that employee:
  205.                     MessageBox.Show(userInfo, $"Info for: {name}");
  206.                 }
  207.             }
  208.         }
  209.  
  210.         private void txtMessage_KeyDown(object sender, KeyEventArgs e)
  211.         {
  212.             if(e.KeyCode == Keys.Enter)
  213.             {
  214.                 btnSend.PerformClick();
  215.                 txtMessage.Clear();
  216.             }
  217.         }
  218.     }
  219. }
  220.  
  221. ////////////////// Client //////////////////
  222.  
  223. using System;
  224. using System.Collections.Generic;
  225. using System.ComponentModel;
  226. using System.Data;
  227. using System.Drawing;
  228. using System.Linq;
  229. using System.Text;
  230. using System.Threading.Tasks;
  231. using System.Windows.Forms;
  232. using System.Net;
  233. using System.Net.Sockets;
  234. using System.IO;
  235. using System.Diagnostics;
  236.  
  237. namespace Client
  238. {
  239.     public partial class Form1 : Form
  240.     {
  241.         // Network variables:
  242.         private TcpClient client;
  243.         public StreamReader STR;
  244.         public StreamWriter STW;
  245.         public string recieve;
  246.         public String TextToSend;
  247.         bool connected = false;
  248.  
  249.         // App tracker variables:
  250.         List<string> blockedApps = new List<string>();
  251.         List<string> blockedRunning = new List<string>();
  252.         string name = "";
  253.         string displayIP;
  254.  
  255.         public Form1()
  256.         {
  257.             InitializeComponent();
  258.             TextBox.CheckForIllegalCrossThreadCalls = false;
  259.             blockedRunning.Add("ICXCNIKA");
  260.             IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
  261.             foreach (IPAddress address in localIP)
  262.             {
  263.                 if (address.AddressFamily == AddressFamily.InterNetwork)
  264.                 {
  265.                     displayIP = address.ToString();
  266.                     txtClientIP.Text = displayIP;
  267.                 }
  268.             }
  269.         }
  270.  
  271.         private void btnConnect_Click(object sender, EventArgs e)
  272.         {
  273.             if(txtName.Text != "")
  274.             {
  275.                 client = new TcpClient();
  276.                 IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(txtClientIP.Text), int.Parse(txtClientPort.Text));
  277.  
  278.                 try
  279.                 {
  280.                     // Try to connect to the server:
  281.                     client.Connect(IpEnd);
  282.  
  283.                     // If connection is successful start readers, writers, timer, and minimize window:
  284.                     if (client.Connected)
  285.                     {
  286.                         STW = new StreamWriter(client.GetStream());
  287.                         STR = new StreamReader(client.GetStream());
  288.                         STW.AutoFlush = true;
  289.                         backgroundWorker1.RunWorkerAsync();
  290.                         backgroundWorker2.WorkerSupportsCancellation = true;
  291.                         connected = true;
  292.                         tmrCheck.Start();
  293.  
  294.                         name = txtName.Text;
  295.                         TextToSend = $"> Employee '{name}' has connected";
  296.                         txtText.AppendText($"You have successfully connected to {displayIP} on port {txtClientPort.Text}!\n");
  297.                         if (!backgroundWorker2.IsBusy)
  298.                         {
  299.                             backgroundWorker2.RunWorkerAsync();
  300.                         }
  301.                     }
  302.                 }
  303.                 catch (Exception ex)
  304.                 {
  305.                     MessageBox.Show(ex.Message.ToString());
  306.                 }
  307.             }
  308.             else
  309.             {
  310.                 MessageBox.Show("Please enter a name", "Error");
  311.             }
  312.         }
  313.  
  314.         // Communicate with employer's computer:
  315.         private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
  316.         {
  317.             if (client.Connected)
  318.             {
  319.                 STW.WriteLine(TextToSend);
  320.             }
  321.             else
  322.             {
  323.                 MessageBox.Show("Sending failed");
  324.             }
  325.             backgroundWorker2.CancelAsync();
  326.         }
  327.  
  328.         private void tmrCheck_Tick(object sender, EventArgs e)
  329.         {
  330.             if (connected)
  331.             {
  332.                 foreach (Process item in Process.GetProcesses())
  333.                 {
  334.                     foreach(string blocked in blockedApps)
  335.                     {
  336.                         // If a blocked process is running and has not been accounted for yet:
  337.                         if (item.ProcessName.ToString().Contains(blocked) && !blockedRunning.Contains(blocked))
  338.                         {
  339.                             // Inform the employer:
  340.                             TextToSend = $"Your employee {name} has opened a blocked app: {blocked}!";
  341.                             txtText.AppendText($"You just opened a blocked app: {blocked}!\n");
  342.                             if (!backgroundWorker2.IsBusy)
  343.                             {
  344.                                 backgroundWorker2.RunWorkerAsync();
  345.                             }
  346.  
  347.                             // Add to running blocked list:
  348.                             blockedRunning.Add(blocked);
  349.                             break;
  350.                         }
  351.                         // If a blocked process is running and it has been accounted for:
  352.                         else if (item.ProcessName.ToString().Contains(blocked) && blockedRunning.Contains(blocked))
  353.                         {
  354.                             // If the blocked process is terminated:
  355.                             if (item.HasExited)
  356.                             {
  357.                                 // Remove from running blocked list:
  358.                                 blockedRunning.Remove(blocked);
  359.  
  360.                                 // Inform the employer:
  361.                                 TextToSend = $"The blocked app {blocked} has been closed!";
  362.                                 txtText.AppendText($"You closed a blocked app: {blocked}!\n");
  363.                                 if (!backgroundWorker2.IsBusy)
  364.                                 {
  365.                                     backgroundWorker2.RunWorkerAsync();
  366.                                 }
  367.                                 break;
  368.                             }
  369.                         }
  370.                     }
  371.                 }
  372.             }
  373.         }
  374.  
  375.         // Recieve messages from employer:
  376.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  377.         {
  378.             while (client.Connected)
  379.             {
  380.                 try
  381.                 {
  382.                     // Recieve and keep track of blocked apps from the employer (live time):
  383.                     recieve = STR.ReadLine();
  384.                     if (recieve.Contains("block"))
  385.                     {
  386.                         string tempBlock = recieve.Split(new[] { "block " }, StringSplitOptions.None)[1].ToLower();
  387.                         blockedApps.Add(tempBlock);
  388.                         txtText.AppendText($"Your employer has blocked the following app: {tempBlock}!\n");
  389.                     } else if (recieve.Contains("notify"))
  390.                     {
  391.                         string notifiedEmp = recieve.Split(new string[] { "notify " }, StringSplitOptions.None)[1].Split('"')[0].Trim();
  392.                         if(notifiedEmp == name)
  393.                         {
  394.                             string message = recieve.Split(new string[] { "\"" }, StringSplitOptions.None)[1].Split('"')[0].Trim();
  395.                             txtText.AppendText($"Message from employer: {message}\n");
  396.                         }
  397.                     }
  398.                     recieve = "";
  399.                 }
  400.                 catch (Exception ex)
  401.                 {
  402.                     MessageBox.Show(ex.Message.ToString());
  403.                 }
  404.             }
  405.         }
  406.  
  407.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  408.         {
  409.             TextToSend = $"< Employee '{name}' has disconnected";
  410.             if (!backgroundWorker2.IsBusy)
  411.             {
  412.                 backgroundWorker2.RunWorkerAsync();
  413.             }
  414.         }
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement