ServerTheading #6

Merged
bruno.da_costa_cunha merged 9 commits from ServerTheading into master 2 years ago

4
.gitignore vendored

@ -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
*.sln.iml

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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();
}
}

@ -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

@ -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<IPEndPoint, UdpClient> clients = new Dictionary<IPEndPoint, UdpClient>();
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);
}
}
}
}

@ -0,0 +1,4 @@
#! /bin/bash
mcs -out:Server.exe Program.cs
screen -d -m -S leapServer mono Server.exe
Loading…
Cancel
Save