public void StartListening() { try { ClientService ClientTask; // Client Connections Pool ClientConnectionPool ConnectionPool = new ClientConnectionPool(); // Client Task to handle client requests ClientTask = new ClientService(ConnectionPool); ClientTask.Start(); IPAddress serverAddr = IPAddress.Parse(Program.SERVER_ADDRESS); int serverPort = Program.SERVER_PORT; Socket mySocketServerUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint localIpEndPoint = new IPEndPoint(serverAddr, serverPort); try { mySocketServerUdp.Bind(localIpEndPoint); int ClientNbr = 0; // Start listening for connections. while (Program.ServerForm.DoExit == false) { Byte[] received = null; EndPoint remoteEP = (localIpEndPoint); int bytesReceived = 0; try { if (mySocketServerUdp.Available > 0) { int nbToRead = Math.Max(Program.BUFFER_SIZE, mySocketServerUdp.Available); received = new Byte[nbToRead]; bytesReceived = mySocketServerUdp.ReceiveFrom(received, ref remoteEP); } else { Thread.Sleep(10); continue; } } catch (Exception exx) { // Case device has been power off :( log.Error("UdpServer.cs exception"); continue; } // did we received something ? if (bytesReceived > 0) { StringBuilder dataReceived = new StringBuilder(); dataReceived.Append(System.Text.Encoding.ASCII.GetString(received)); // An incoming message needs to be processed. ConnectionPool.Enqueue(new UDPClientHandler(remoteEP, ClientNbr, dataReceived.ToString())); // Send ACK to the client //byte[] cmdBytes = new byte[] { }; //cmdBytes = Encoding.ASCII.GetBytes(requiredCommand); //var sendSackResult = mySocketServerUdp.SendTo(cmdBytes, cmdBytes.Length, SocketFlags.None, remoteEP); } else { // Program.ServerForm.updateNbConnection(ConnectionPool.Count); Thread.Sleep(100); } } // Stop client requests handling ClientTask.Stop(); log.Debug("Closing Listener..."); mySocketServerUdp.Shutdown(SocketShutdown.Both); //mySocketServerUdp.Disconnect(false); mySocketServerUdp.Close(2); mySocketServerUdp = null; } catch (Exception e) { log.Error(e); } } catch (Exception ex) { log.Error(ex); } }