51 lines
No EOL
1.7 KiB
C#
51 lines
No EOL
1.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace Server;
|
|
|
|
public class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// 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());
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |