Initial commit

This commit is contained in:
2023-08-01 14:04:38 +02:00
commit d64e49ce19
8 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
using System.Net;
using System.Net.Sockets;
namespace CardMarketServer
{
internal class Program
{
private static object _lock = new object();
private static readonly List<TcpClient> _clients = new List<TcpClient>();
public static TcpClient[] GetClients()
{
lock(_lock ) return _clients.ToArray();
}
public static int GetClientCount()
{
lock(_lock) return _clients.Count;
}
public static void RemoveClient(TcpClient client)
{
lock(_lock) _clients.Remove(client);
}
public static void AddClient(TcpClient client)
{
lock(_lock) _clients.Add(client);
}
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("0.0.0.0");
TcpListener ServerSocket = new TcpListener(ip, 4000);
ServerSocket.Start();
Console.WriteLine("Server started");
while(true)
{
TcpClient clientSocket = ServerSocket.AcceptTcpClient();
Console.WriteLine($"Client connected: {clientSocket.Client.RemoteEndPoint}");
AddClient(clientSocket);
handleClient client = new handleClient();
client.startClient(clientSocket);
Console.WriteLine($"{GetClientCount()} clients connected");
}
}
}
}