private void ReceiveLoop() { if (string.IsNullOrEmpty(this.m_CurrentIP)) { this.m_LocalEndPoint = new IPEndPoint(IPAddress.Any, this.m_CurrentPort); } else { this.m_LocalEndPoint = new IPEndPoint(IPAddress.Parse(this.m_CurrentIP), this.m_CurrentPort); } try { this.m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.m_Socket.Bind(this.m_LocalEndPoint); this.m_iState = Constants.UDP_STATE_LISTENING; Action <int> action = (i) => { byte[] array = new byte[Constants.MAX_COMMAND_LEN]; EndPoint endPoint = new IPEndPoint(this.m_LocalEndPoint.Address, this.m_LocalEndPoint.Port); int num = this.m_Socket.ReceiveFrom(array, ref endPoint); IPEndPoint clientIP = (IPEndPoint)endPoint; UdpConnection udpConnection = this.NewConnection(clientIP); byte[] array2 = new byte[num]; Array.Copy(array, array2, num); if (this.OnDataReceived != null) { this.OnDataReceived(udpConnection.ID, new ReceivedEventArgs((IPEndPoint)endPoint, array2)); } }; try { while (true) { action(0); } } catch (SocketException ex) { if (this.OnSocketError != null) { this.OnSocketError(0, new SocketEventArgs(ex.ErrorCode, ex.Message)); } action(0); } catch (Exception) { action(0); } } catch { } }
private UdpConnection NewConnection(IPEndPoint clientIP) { string text = clientIP.ToString(); UdpConnection udpConnection = (UdpConnection)this.ConnectionMap[text]; if (udpConnection != null) { udpConnection.OnlineDate = DateTime.Now; return(udpConnection); } UdpConnection udpConnection2 = new UdpConnection(); udpConnection2.ClientIP = clientIP; this.ConnectionMap[text] = udpConnection2; udpConnection2.ID = text; this.ConnectionList.Add(udpConnection2); return(udpConnection2); }
private UdpConnection NewConnection(IPEndPoint clientIP) { string strKey = clientIP.ToString(); UdpConnection ic = (UdpConnection)ConnectionMap[strKey]; if (ic != null) { ic.OnlineDate = DateTime.Now; return(ic); } UdpConnection uc = new UdpConnection(); uc.ClientIP = clientIP; ConnectionMap[strKey] = uc; uc.ID = strKey; ConnectionList.Add(uc); return(uc); }
private UdpConnection NewConnection(IPEndPoint clientIP) { string strKey = clientIP.ToString(); UdpConnection ic = (UdpConnection)ConnectionMap[strKey]; if(ic != null) { ic.OnlineDate = DateTime.Now; return ic; } UdpConnection uc = new UdpConnection(); uc.ClientIP = clientIP; ConnectionMap[strKey] = uc; uc.ID = strKey; ConnectionList.Add(uc); return uc; }
private void ReceiveLoop() { int iExCount = 0; if (string.IsNullOrEmpty(m_CurrentIP)) { m_LocalEndPoint = new IPEndPoint(IPAddress.Any, m_CurrentPort); } else { m_LocalEndPoint = new IPEndPoint(IPAddress.Parse(m_CurrentIP), m_CurrentPort); } try { m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_Socket.Bind(m_LocalEndPoint); m_iState = Constants.UDP_STATE_LISTENING; //if (OnListenStateChanged != null) //OnListenStateChanged(null, new ListenEventArgs(true)); while (true) { try { //Make space to store the data from the socket Byte[] received = new Byte[Constants.MAX_COMMAND_LEN]; //Create an end point, just give it temporary values EndPoint remoteEP = new IPEndPoint(m_LocalEndPoint.Address, m_LocalEndPoint.Port); //Read bytes from the socket int bytesReceived = m_Socket.ReceiveFrom(received, ref remoteEP); IPEndPoint remoteIPEP = (IPEndPoint)remoteEP; UdpConnection uc = NewConnection(remoteIPEP); //string str_received = Encoding.UTF8.GetString(received); //string str_received = null;//Util.BytesToString(received); //str_received = str_received.Substring(0, bytesReceived); //Fire the received event if it is being used (allowing raw data to be caught) byte[] data = new byte[bytesReceived]; Array.Copy(received, data, bytesReceived); if (OnDataReceived != null) { OnDataReceived(uc.ID, new ReceivedEventArgs((IPEndPoint)remoteEP, data)); } } catch (SocketException ex) { if (OnSocketError != null) { OnSocketError(0, new SocketEventArgs((int)ex.ErrorCode, ex.Message)); } } catch (Exception ex) { } } } catch { } }