online_security_project/server/Program.cs

51 lines
1.7 KiB
C#
Raw Normal View History

2024-12-14 15:17:58 +00:00
using System;
2024-12-17 18:41:30 +00:00
using System.IO;
2024-12-14 15:17:58 +00:00
using System.Net;
using System.Net.Sockets;
2024-12-17 18:41:30 +00:00
using System.Security.Cryptography;
2024-12-14 15:17:58 +00:00
namespace Server;
public class Program
{
static void Main(string[] args)
{
2024-12-17 18:41:30 +00:00
// Generally this key would be static but since its not production yet we can generate it every time to make sure
// the users has the key and could load it from file
RSA key = RSA.Create(2048);
File.WriteAllText("server_key.pem", key.ExportRSAPublicKeyPem());
2024-12-14 15:17:58 +00:00
int port = 12345;
TcpListener server = new(IPAddress.Parse("0.0.0.0"), port);
try {
server.Start();
byte[] buffer = new byte[256];
while(true) {
// Currently, every time it gets a block, it will simply send it back but ToUpper
using TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Got a client!");
var stream = client.GetStream();
int readLen;
while((readLen = stream.Read(buffer, 0, buffer.Length)) != 0) {
// for now, lets just read it as an ascii string
string input = System.Text.Encoding.ASCII.GetString(buffer, 0, readLen);
Console.WriteLine($"Got block: {input}");
byte[] ret = System.Text.Encoding.ASCII.GetBytes(input.ToUpper());
stream.Write(ret, 0, ret.Length);
}
}
}
catch(Exception ex) {
Console.WriteLine($"Server error: {ex.Message}");
Console.WriteLine("Trace: " + ex.StackTrace);
}
finally {
server.Stop();
}
}
}