diff --git a/.gitignore b/.gitignore index 8dd4607..aa3cf19 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ *.userosscache *.sln.docstates +Server.exe + # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs @@ -395,4 +397,4 @@ FodyWeavers.xsd *.msp # JetBrains Rider -*.sln.iml \ No newline at end of file +*.sln.iml diff --git a/code/server/ClientConsole/ClientConsole.csproj b/code/server/ClientConsole/ClientConsole.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/code/server/ClientConsole/ClientConsole.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/code/server/ClientConsole/Program.cs b/code/server/ClientConsole/Program.cs new file mode 100644 index 0000000..dc744ca --- /dev/null +++ b/code/server/ClientConsole/Program.cs @@ -0,0 +1,45 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Welcome to LeapHit Multiplayer - Client"); + StartClient(); + } + + static void StartClient() + { + IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3131); + UdpClient client = new UdpClient(); + + // Send connection message to server + string connectionMessage = "Connect"; + byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage); + client.Send(connectionData, connectionData.Length, serverEndPoint); + + // Receive connection message from server + IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + byte[] receivedData = client.Receive(ref remoteEndPoint); + string receivedPort = Encoding.ASCII.GetString(receivedData); + Console.WriteLine("Received port: " + receivedPort); + + // Send data to server + string message = ""; + while (message != "exit") + { + serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(receivedPort)); + + Console.Write("Enter message to send (or 'exit' to quit): "); + message = Console.ReadLine(); + byte[] data = Encoding.ASCII.GetBytes(message); + client.Send(data, data.Length, serverEndPoint); + } + + // Close client + client.Close(); + } +} \ No newline at end of file diff --git a/code/server/Server.sln b/code/server/Server.sln index 164c901..d97c22f 100644 --- a/code/server/Server.sln +++ b/code/server/Server.sln @@ -11,10 +11,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataBase", "DataBase\DataBase.csproj", "{FCCE7DEC-1F7B-4C66-8C61-3FCB668F9008}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestControleurs", "TestControleurs\TestControleurs.csproj", "{A9DFF203-7F27-44BA-A460-F65C01D0EDFD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestControleurs", "TestControleurs\TestControleurs.csproj", "{A9DFF203-7F27-44BA-A460-F65C01D0EDFD}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{12A97D16-34BB-4D4F-9F76-D74061A9BA77}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientConsole", "ClientConsole\ClientConsole.csproj", "{33F7C629-F92B-459C-BCC5-6A4601D6D9EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,6 +43,10 @@ Global {A9DFF203-7F27-44BA-A460-F65C01D0EDFD}.Debug|Any CPU.Build.0 = Debug|Any CPU {A9DFF203-7F27-44BA-A460-F65C01D0EDFD}.Release|Any CPU.ActiveCfg = Release|Any CPU {A9DFF203-7F27-44BA-A460-F65C01D0EDFD}.Release|Any CPU.Build.0 = Release|Any CPU + {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33F7C629-F92B-459C-BCC5-6A4601D6D9EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/code/server/Server/Program.cs b/code/server/Server/Program.cs index d7eab11..ccd8ca2 100644 --- a/code/server/Server/Program.cs +++ b/code/server/Server/Program.cs @@ -1,10 +1,15 @@ -using System; +using System; +using System.Collections.Generic; using System.Net; using System.Net.Sockets; - +using System.Text; +using System.Threading; class Program { + static Dictionary clients = new Dictionary(); + static int nextPort = 3132; + static void Main(string[] args) { Console.WriteLine("Welcome to LeapHit Multiplayer - Server"); @@ -13,17 +18,48 @@ class Program static void StartServer() { - IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 3131); - UdpClient server = new UdpClient(endPoint); + IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 3131); + UdpClient serverSocket = new UdpClient(serverEndPoint); Console.WriteLine("Server started, waiting for clients to connect..."); - Console.WriteLine(endPoint.Address.ToString()); while (true) { - IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0); - byte[] data = server.Receive(ref clientEndPoint); - string dataReceived = System.Text.Encoding.ASCII.GetString(data); - Console.WriteLine("Data received from client: " + dataReceived + " from " + clientEndPoint.ToString()); + IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + byte[] receivedData = serverSocket.Receive(ref remoteEndPoint); + string message = Encoding.ASCII.GetString(receivedData); + + if (message == "Connect") + { + Console.WriteLine("New connection from " + remoteEndPoint.ToString()); + + // Assign a unique port to the client + IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort++); ; + UdpClient clientSocket = new UdpClient(clientEndPoint); + clients[remoteEndPoint] = clientSocket; + + // Send connection message to client + string connectionMessage = clientEndPoint.Port.ToString(); + byte[] connectionData = Encoding.ASCII.GetBytes(connectionMessage); + serverSocket.Send(connectionData, connectionData.Length, remoteEndPoint); + + // Start thread to receive data from client + Thread receiveThread = new Thread(()=>ReceiveMessages(clientSocket)); + receiveThread.Start(); + + } + } + } + + static void ReceiveMessages(UdpClient clientSocket) + { + IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + + while (true) + { + byte[] receivedData = clientSocket.Receive(ref remoteEndPoint); + string receivedMessage = Encoding.ASCII.GetString(receivedData); + Console.WriteLine("Received from " + remoteEndPoint.ToString() + ": " + receivedMessage); + } } -} +} \ No newline at end of file diff --git a/code/server/Server/start.sh b/code/server/Server/start.sh new file mode 100755 index 0000000..953bfe1 --- /dev/null +++ b/code/server/Server/start.sh @@ -0,0 +1,4 @@ +#! /bin/bash + +mcs -out:Server.exe Program.cs +screen -d -m -S leapServer mono Server.exe