|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -16,64 +18,47 @@ class Program
|
|
|
|
|
|
|
|
|
|
static void StartServer()
|
|
|
|
|
{
|
|
|
|
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 3131);
|
|
|
|
|
UdpClient server = new UdpClient(endPoint);
|
|
|
|
|
Thread receiveThread = new Thread(ServerReceiveMessages);
|
|
|
|
|
receiveThread.Start(server);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
// 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 remoteEndpoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
byte[] data = serverSocket.Receive(ref remoteEndpoint);
|
|
|
|
|
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
byte[] receivedData = serverSocket.Receive(ref remoteEndPoint);
|
|
|
|
|
string message = Encoding.ASCII.GetString(receivedData);
|
|
|
|
|
|
|
|
|
|
if (!clients.ContainsKey(remoteEndpoint))
|
|
|
|
|
if (message == "Connect")
|
|
|
|
|
{
|
|
|
|
|
clients[remoteEndpoint] = new UdpClient();
|
|
|
|
|
clients[remoteEndpoint].Client.Bind(new IPEndPoint(IPAddress.Any, 31323));
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("New connection from " + remoteEndPoint.ToString());
|
|
|
|
|
|
|
|
|
|
string connectionMessage = "Connection established.";
|
|
|
|
|
byte[] connectionData = System.Text.Encoding.ASCII.GetBytes(connectionMessage);
|
|
|
|
|
clients[remoteEndpoint].Send(connectionData, connectionData.Length, remoteEndpoint.Address.ToString(), remoteEndpoint.Port);
|
|
|
|
|
// Assign a unique port to the client
|
|
|
|
|
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, nextPort++); ;
|
|
|
|
|
UdpClient clientSocket = new UdpClient(clientEndPoint);
|
|
|
|
|
clients[remoteEndPoint] = clientSocket;
|
|
|
|
|
|
|
|
|
|
// Receive data on the connection socket
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
byte[] receivedData = clients[remoteEndpoint].Receive(ref remoteEndpoint);
|
|
|
|
|
string receivedMessage = System.Text.Encoding.ASCII.GetString(receivedData);
|
|
|
|
|
Console.WriteLine("Received from " + remoteEndpoint + ": " + receivedMessage);
|
|
|
|
|
// Send connection message to client
|
|
|
|
|
string connectionMessage = "Connection established on port " + 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);
|
|
|
|
|
receiveThread.Start(clientSocket);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ReceiveMessages(object obj)
|
|
|
|
|
{
|
|
|
|
|
UdpClient clientSocket = (UdpClient)obj;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|