//--------------------------Private methods------------------------------------------------------ void receive() { var _receiveBuffer = new byte[BUFFER_SIZE]; uint _ctxIndex = 0; //-- This value is not realy used. Remote end point is passed as reference, but we only care //-- about the returned value, that represents the end point that sent the datagram/command EndPoint _remoteEP = new IPEndPoint(/*IPAddress.Parse("127.0.0.1")*/ IPAddress.Any, 0); stopped = false; while (! stop) { try { int _numberOfBytesReceived; try { _numberOfBytesReceived = localHost.ReceiveFrom(_receiveBuffer, ref _remoteEP); } catch (SocketException _se) { var _errorCode = (SocketError) _se.ErrorCode; if (_errorCode != SocketError.TimedOut) { log(LogSeverity.Critical, UDP_SERVER_RECEIVE_LABEL, string.Format("Socket error:\r\n{0}", _se)); } continue; } //-- Convert raw bytes to string. var _dataReceived = Encoding.ASCII.GetString(_receiveBuffer, 0, _numberOfBytesReceived); _ctxIndex++; CmdContextCache.Save(_ctxIndex, new CmdContext(_dataReceived, _remoteEP)); threadPool.PostEvent(_ctxIndex); } catch (Exception _ex) { log(LogSeverity.Critical, UDP_SERVER_RECEIVE_LABEL, string.Format("Exception:\r\n{0}", _ex)); } } stopped = true; }