public TcpServer(int port, IntPtr guiHandle, NotifyIcon notifyIcon) { this.tcpPort = port; this.guiHandle = guiHandle; this.notifyIcon = notifyIcon; this.clients = new List <TcpClient>(); this.mouseUtils = new MouseUtils(); this.monitorUtils = new MonitorUtils(guiHandle); this.performanceUtils = new PerformanceUtils(); this.AutoSendInterval = 10; autoSendTimer = new System.Windows.Forms.Timer(); autoSendTimer.Interval = AutoSendInterval; this.autoSendTimer.Tick += new EventHandler(SendTimedMessages); autoSendTimer.Enabled = true; }
public TcpServer(int port, IntPtr guiHandle, NotifyIcon notifyIcon) { this.tcpPort = port; this.guiHandle = guiHandle; this.notifyIcon = notifyIcon; this.clients = new List<TcpClient>(); this.mouseUtils = new MouseUtils(); this.monitorUtils = new MonitorUtils(guiHandle); this.performanceUtils = new PerformanceUtils(); this.AutoSendInterval = 10; autoSendTimer = new System.Windows.Forms.Timer(); autoSendTimer.Interval = AutoSendInterval; this.autoSendTimer.Tick += new EventHandler(SendTimedMessages); autoSendTimer.Enabled = true; }
private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] inputMessage = new byte[4096]; int bytesRead; while (true) { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(inputMessage, 0, 4096); } catch { break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received ASCIIEncoding encoder = new ASCIIEncoding(); string inputString = encoder.GetString(inputMessage, 0, bytesRead); System.Diagnostics.Debug.WriteLine(inputString); string[] inputParams = inputString.Split(';'); string outputString = inputParams[0]; switch (inputParams[0]) { case "GetMousePosition": outputString = string.Format("MousePosition;{0};{1}", MouseUtils.GetMousePosition().X.ToString(), MouseUtils.GetMousePosition().Y.ToString()); break; case "GetMouseIdleSince": outputString = string.Format("MouseIdleSince;{0}", mouseUtils.GetMouseIdleSince()); break; case "CursorShow": MouseUtils.SetCursorVisible(true); break; case "CursorHide": MouseUtils.SetCursorVisible(false); break; case "StartScreenSaver": monitorUtils.StartScreenSaver(); break; case "ScreenPowerOff": monitorUtils.ScreenPowerOff(); break; case "ScreenPowerOn": monitorUtils.ScreenPowerOn(); break; case "FreeDiscSpace": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], DiscUtils.GetFreeDiscSpace(inputParams[1])); break; case "TotalDiscSpace": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], DiscUtils.GetTotalDiscSpace(inputParams[1])); break; case "DriveType": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], DiscUtils.GetDriveType(inputParams[1])); break; case "DriveFormat": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], DiscUtils.GetDriveFormat(inputParams[1])); break; case "IsDriveAvailable": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], DiscUtils.IsDriveAvailable(inputParams[1])); break; case "UsedCPU": outputString = string.Format("{0};{1}", inputParams[0], performanceUtils.GetUsedCPU()); break; case "AvailableMemory": outputString = string.Format("{0};{1}", inputParams[0], performanceUtils.GetAvailableMemory()); break; case "SystemUpTime": outputString = string.Format("{0};{1}", inputParams[0], performanceUtils.SystemUpTime()); break; case "ProcessMemory": outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], performanceUtils.getProcessMemory(inputParams[1])); break; case "RunProgram": Process.Start(inputParams[1], inputParams[2]); outputString = string.Format("{0};{1};{2}", inputParams[0], inputParams[1], inputParams[2]); break; case "NotifyInfo": notifyIcon.ShowBalloonTip(int.Parse(inputParams[1]), inputParams[2], inputParams[3], ToolTipIcon.Info); break; case "TaskBarHide": //monitorUtils.WindowsTaskBarVisible(false); Taskbar.Hide(); break; case "TaskBarShow": //monitorUtils.WindowsTaskBarVisible(true); Taskbar.Show(); break; default: outputString = "Unknown Command"; break; } WriteToStream(tcpClient, outputString); } clients.Remove(tcpClient); tcpClient.Close(); }