0% found this document useful (0 votes)
14 views3 pages

Server Code (Teacher's Machine)

Uploaded by

tungnv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Server Code (Teacher's Machine)

Uploaded by

tungnv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Server Code (Teacher's Machine)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace TeacherApp
{
public partial class ServerForm : Form
{
private WebSocketServer _server;

public ServerForm()
{
InitializeComponent();
}

private void StartServerButton_Click(object sender, EventArgs e)


{
_server = new WebSocketServer("ws://localhost:8080");
_server.AddWebSocketService<StudentInfoBehavior>("/student");
_server.Start();

logTextBox.AppendText("Server started at ws://localhost:8080\n");


}

protected override void OnFormClosing(FormClosingEventArgs e)


{
_server?.Stop();
base.OnFormClosing(e);
}

public class StudentInfoBehavior : WebSocketBehavior


{
protected override void OnMessage(MessageEventArgs e)
{
string studentInfo = e.Data;
ServerForm form = (ServerForm)Application.OpenForms[0];
form.Invoke((MethodInvoker)(() =>
{
form.studentListBox.Items.Add(studentInfo);
}));
}
}
}
}

// Client Code (Student's Machine)


using System;
using System.Management;
using System.Windows.Forms;
using WebSocketSharp;

namespace StudentApp
{
public partial class ClientForm : Form
{
private WebSocket _client;

public ClientForm()
{
InitializeComponent();
}

private void ConnectButton_Click(object sender, EventArgs e)


{
_client = new WebSocket("ws://localhost:8080/student");
_client.OnOpen += (s, ev) =>
{
logTextBox.AppendText("Connected to the server.\n");
string hardwareInfo = GetHardwareInfo();
_client.Send(hardwareInfo);
};

_client.Connect();
}

private string GetHardwareInfo()


{
StringBuilder info = new StringBuilder();

// Mainboard
var searcher = new ManagementObjectSearcher("select * from
Win32_BaseBoard");
foreach (var obj in searcher.Get())
{
info.AppendLine("Mainboard: " + obj["Product"]);
}

// CPU
searcher = new ManagementObjectSearcher("select * from
Win32_Processor");
foreach (var obj in searcher.Get())
{
info.AppendLine("CPU: " + obj["Name"]);
}

// RAM
searcher = new ManagementObjectSearcher("select * from
Win32_PhysicalMemory");
foreach (var obj in searcher.Get())
{
info.AppendLine("RAM: " +
Math.Round(Convert.ToDouble(obj["Capacity"]) / (1024 * 1024 * 1024), 2) + " GB");
}

// Hard Drive
searcher = new ManagementObjectSearcher("select * from
Win32_DiskDrive");
foreach (var obj in searcher.Get())
{
info.AppendLine("Hard Drive: " + obj["Model"]);
}

// Mouse and Keyboard (Basic Info)


info.AppendLine("Mouse: Connected");
info.AppendLine("Keyboard: Connected");

// Monitor
searcher = new ManagementObjectSearcher("select * from
Win32_DesktopMonitor");
foreach (var obj in searcher.Get())
{
info.AppendLine("Monitor: " + obj["Caption"]);
}

return info.ToString();
}

protected override void OnFormClosing(FormClosingEventArgs e)


{
_client?.Close();
base.OnFormClosing(e);
}
}
}

You might also like