Basic TCP server

This commit is contained in:
Layla 2020-05-09 09:49:52 -04:00
parent 51b0e65ad7
commit da26a320b4
5 changed files with 136 additions and 0 deletions

37
authorizer/.gitignore vendored Normal file
View File

@ -0,0 +1,37 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/
# Visual Studio Code
.vscode
# Rider
.idea
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
# Visual Studio 2015
.vs/

7
authorizer/Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM mcr.microsoft.com/dotnet/core/runtime:3.1
COPY bin/Release/netcoreapp3.1/publish/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "authorizer.dll"]
EXPOSE 7778/tcp

23
authorizer/Program.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
namespace authorizer
{
class Program
{
static AuthServer server;
static void Main(string[] args)
{
server = new AuthServer();
server.Start();
string input;
do
{
input = Console.ReadLine();
}
while(input != "stop");
server.Stop();
}
}
}

61
authorizer/Server.cs Normal file
View File

@ -0,0 +1,61 @@
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
class AuthServer
{
private int port;
private IPAddress address;
private TcpListener server;
private Thread thread;
private bool running = false;
public AuthServer(string addr = "0.0.0.0", int p = 7778)
{
port = p;
address = IPAddress.Parse(addr);
server = new TcpListener(address, port);
}
private void ServerLoop()
{
while(running)
{
Console.WriteLine("Waiting for a connection...");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
Byte[] bytes = new byte[256];
String data = null;
NetworkStream stream = client.GetStream();
int i;
while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Recieved: {0}", data);
}
client.Close();
}
}
public void Start()
{
server.Start();
running = true;
ThreadStart entrypoint = new ThreadStart(ServerLoop);
thread = new Thread(entrypoint);
thread.Start();
}
public void Stop()
{
running = false;
}
public void ForceStop()
{
thread.Join();
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>