private void CheckForIPChange() { uint newLocalAddress = 0; if (string.IsNullOrWhiteSpace(LocalIP)) { // pick the first IP address by default if (_connections.Count > 0) { newLocalAddress = _connections[0].LocalIP; } // if we happened to have picked localhost, which may be used by some VPN/proxy software, look for any other ip other than localhost and pick it instead. if (newLocalAddress == 0x100007F) { for (int i = 0; i < _connections.Count; i++) { if (_connections[i].LocalIP != 0x100007f) { newLocalAddress = _connections[i].LocalIP; break; } } } } else { newLocalAddress = (uint)IPAddress.Parse(LocalIP).Address; } if (_localAddress != newLocalAddress) { Trace.WriteLine("TCPNetworkMonitor: " + ((MonitorType == NetworkMonitorType.WinPCap) ? "WinPCap " : "") + "listening on IP: " + new IPAddress(newLocalAddress).ToString()); _localAddress = newLocalAddress; if (MonitorType == NetworkMonitorType.WinPCap) { if (_winpcap != null) { _winpcap.Destroy(); } _winpcap = new RawPCap(); _winpcap.Create(_localAddress); } else { if (_socket != null) { _socket.Destroy(); } _socket = new RawSocket(); _socket.Create(_localAddress); } } }
private void Cleanup() { if (_socket != null) { _socket.Destroy(); _socket = null; } if (_winpcap != null) { _winpcap.Destroy(); _winpcap = null; } _connections.Clear(); _localAddress = 0; }