|
|
|
@ -2,13 +2,12 @@ using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static Dictionary<IPEndPoint, UdpClient> clients = new Dictionary<IPEndPoint, UdpClient>();
|
|
|
|
|
|
|
|
|
|
static int playerCount = 0;
|
|
|
|
|
static List<IPEndPoint> clientAddresses = new List<IPEndPoint>(); // Liste des adresses IP des clients connectés
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Welcome to LeapHit Multiplayer - Server");
|
|
|
|
@ -19,36 +18,62 @@ class Program
|
|
|
|
|
{
|
|
|
|
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 3131);
|
|
|
|
|
UdpClient server = new UdpClient(endPoint);
|
|
|
|
|
Thread receiveThread = new Thread(ServerReceiveMessages);
|
|
|
|
|
receiveThread.Start(server);
|
|
|
|
|
Console.WriteLine("Server started, waiting for clients to connect...");
|
|
|
|
|
Console.WriteLine(endPoint.Address.ToString());
|
|
|
|
|
|
|
|
|
|
while (playerCount < 2)
|
|
|
|
|
// Stop Server
|
|
|
|
|
Console.WriteLine("Press Enter to exit.");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
receiveThread.Abort();
|
|
|
|
|
foreach (UdpClient clientSocket in clients.Values)
|
|
|
|
|
{
|
|
|
|
|
clientSocket.Close();
|
|
|
|
|
}
|
|
|
|
|
server.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ServerReceiveMessages(object obj)
|
|
|
|
|
{
|
|
|
|
|
UdpClient serverSocket = (UdpClient)obj;
|
|
|
|
|
|
|
|
|
|
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[] data = serverSocket.Receive(ref remoteEndpoint);
|
|
|
|
|
|
|
|
|
|
if (!clientAddresses.Contains(clientEndPoint)) // Vérification si l'adresse IP est déjà présente dans la liste
|
|
|
|
|
if (!clients.ContainsKey(remoteEndpoint))
|
|
|
|
|
{
|
|
|
|
|
clientAddresses.Add(clientEndPoint); // Ajout de l'adresse IP à la liste
|
|
|
|
|
playerCount++;
|
|
|
|
|
|
|
|
|
|
if (playerCount == 2)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Deux joueurs connectés, le jeu va commencer...");
|
|
|
|
|
// On va mettre le code du pour demarrer le match ici
|
|
|
|
|
}
|
|
|
|
|
clients[remoteEndpoint] = new UdpClient();
|
|
|
|
|
clients[remoteEndpoint].Client.Bind(new IPEndPoint(IPAddress.Any, 31323));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
string connectionMessage = "Connection established.";
|
|
|
|
|
byte[] connectionData = System.Text.Encoding.ASCII.GetBytes(connectionMessage);
|
|
|
|
|
clients[remoteEndpoint].Send(connectionData, connectionData.Length, remoteEndpoint.Address.ToString(), remoteEndpoint.Port);
|
|
|
|
|
|
|
|
|
|
// Receive data on the connection socket
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Client with IP " + clientEndPoint.Address.ToString() + " has already sent a message and will not be counted.");
|
|
|
|
|
byte[] receivedData = clients[remoteEndpoint].Receive(ref remoteEndpoint);
|
|
|
|
|
string receivedMessage = System.Text.Encoding.ASCII.GetString(receivedData);
|
|
|
|
|
Console.WriteLine("Received from " + remoteEndpoint + ": " + receivedMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
server.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|