online_security_project/server/Program.cs

44 lines
1.3 KiB
C#
Raw Normal View History

2024-12-14 15:17:58 +00:00
using System;
using System.Net;
using System.Net.Sockets;
namespace Server;
public class Program
{
static void Main(string[] args)
{
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();
}
}
}