Please tell me how to split a UDP socket into threads so that the server can simultaneously receive data from ten clients on one port?
UPD :
UPD2 :
Client :
// Конструктор клиента UdpClient udpClient = new UdpClient(11001); udpClient.Connect("127.0.0.1", 11000); repeat: // Тут мы отправляем "типа" данные byte[] sendBytes = Encoding.UTF8.GetBytes("Привет :D"); for (int i = 0; i < new Random().Next(100); i++) { udpClient.Send(sendBytes, sendBytes.Length); } //Это я не знаю для чего, но без него программа не работает ) IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); // А это мы получаем сообщение от клиента (Ну если условно, то сервера) Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.UTF8.GetString(receiveBytes); if (returnData == "Давай по новой") { goto repeat; } // Разрываем соединение. udpClient.Close(); Server :
async void RegisterTask(Task task) { try { activeTasks.Add(task); await task; } finally { activeTasks.Remove(task); } } const int listenPort = 11000; UdpClient listener = new UdpClient(listenPort); private async void button_Click(object sender, RoutedEventArgs e) { while (true) { var result = await listener.ReceiveAsync(); Console.WriteLine($"Received from {result.RemoteEndPoint} {result.Buffer.Length} bytes"); string returnData = Encoding.UTF8.GetString(result.Buffer); // если ваша обработка длительная, то стартуйте Task, обрабатывающий данные, здесь Task processTask = Process(result); RegisterTask(processTask); } } private Task Process(UdpReceiveResult result) { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = listener.Receive(ref RemoteIpEndPoint); string returnData = Encoding.UTF8.GetString(receiveBytes); listener.Send(Encoding.UTF8.GetBytes("Давай по новой"), Encoding.UTF8.GetBytes("Давай по новой").Length); return null; } 
UdpClient? UDP, if I'm not mistaken, does not require a connection, so there is essentially no difference between the client and the server. - VladDRemoteEndPointcomes to it, and distinguish it by it. - VladD