Initial commit

This commit is contained in:
2026-07-13 09:25:40 +07:00
parent c08ff54676
commit bccfb156d7
1938 changed files with 641646 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
using System.Net;
using System.Net.Sockets;
namespace SpeedTest.Server
{
internal class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: SpeedTest.Server <IP> <Port>");
return;
}
if (!IPAddress.TryParse(args[0], out IPAddress? ipAddress))
{
Console.WriteLine($"Invalid IP address: {args[0]}");
return;
}
if (!int.TryParse(args[1], out int port) || port < 1 || port > 65535)
{
Console.WriteLine($"Invalid port: {args[1]}");
return;
}
TcpListener? listener = null;
try
{
listener = new TcpListener(ipAddress, port);
listener.Start();
Console.WriteLine($"Server started on {ipAddress}:{port}");
Console.WriteLine("Waiting for client connection...");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client connected!");
NetworkStream stream = client.GetStream();
long bytesInCurrentSecond = 0;
bool isReceiving = true;
// Thread để nhận dữ liệu
Thread receiveThread = new Thread(() =>
{
byte[] buffer = new byte[64 * 1024]; // 64KB buffer
try
{
while (isReceiving)
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
isReceiving = false;
break;
}
Interlocked.Add(ref bytesInCurrentSecond, bytesRead);
}
}
catch (Exception ex)
{
Console.WriteLine($"Receive thread error: {ex.Message}");
isReceiving = false;
}
});
receiveThread.Start();
Console.WriteLine("Receiving data... (Press Ctrl+C to stop)");
Console.WriteLine("Time\t\tBytes/s\t\tMB/s\t\tGB/s");
// Main thread chỉ tính toán và in kết quả
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
while (isReceiving)
{
Thread.Sleep(1000); // Đợi đúng 1 giây
long bytesThisSecond = Interlocked.Exchange(ref bytesInCurrentSecond, 0);
double bytesPerSecond = bytesThisSecond;
double mbPerSecond = bytesPerSecond / (1024.0 * 1024.0);
double gbPerSecond = bytesPerSecond / (1024.0 * 1024.0 * 1024.0);
Console.WriteLine($"{DateTime.Now:HH:mm:ss}\t{bytesPerSecond:N0}\t\t{mbPerSecond:F2}\t\t{gbPerSecond:F4}");
}
receiveThread.Join();
Console.WriteLine("Client disconnected.");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
listener?.Stop();
}
}
}
}