public static string FindProcessNameByUDpConnection(IPAddress sourceAddress, IPAddress destinationAddress, ushort sourcePort, ushort destinationPort, IPAddress localIP)
        {
            List <UdpRecordPid> udpRecords = null;
            ushort    port;
            IPAddress address;

            if (localIP == sourceAddress)
            {
                port    = sourcePort;
                address = sourceAddress;
            }
            else
            {
                port    = destinationPort;
                address = destinationAddress;
            }

            if ((udpRecords = NetworkStatisticData.GetAllUdpConnections()) != null)
            {
                UdpRecordPid record = udpRecords.Where(r => r.LocalPort == port).SingleOrDefault();
                if (record != null)
                {
                    return(record.PID.ToString());
                }
            }

            return(String.Empty);
        }
        private void ProcessUdpPorts()
        {
            List<UdpRecordPid> udpPortsCollection = null;

            if ((udpPortsCollection = NetworkStatisticData.GetAllUdpConnections()) != null)
            {
                foreach (UdpRecordPid record in udpPortsCollection)
                {
                    if (!(_view.ListOpenPorts.Items.ContainsKey(record.GetHashCode().ToString())))
                        OnAddToPortList(InitUdpItem(record));
                }

                foreach (ListViewItem item in _view.ListOpenPorts.Items)
                {
                    if (item.SubItems[1].Text == "TCP")
                        continue;

                    if (udpPortsCollection.Where(r => r.GetHashCode() == int.Parse(item.Name) && r.Protocol.Equals("UDP")).SingleOrDefault() == null)
                        OnRemoveFromPortList(item);
                }
            }
        }
        public static string FindProcessNameByTcpConnection(IPAddress sourceAddress, IPAddress destinationAddress, ushort sourcePort, ushort destinationPort, IPAddress localIP)
        {
            List <TcpRecordPid> tcpRecords = null;
            ushort    port;
            IPAddress address;

            if (localIP == sourceAddress)
            {
                port    = sourcePort;
                address = sourceAddress;
            }
            else
            {
                port    = destinationPort;
                address = destinationAddress;
            }
            if ((tcpRecords = NetworkStatisticData.GetAllTcpConnections()) != null && tcpRecords.Count > 0)
            {
                TcpRecordPid record = tcpRecords.Where(r => r.LocalPort == port).SingleOrDefault();
                if (record != null)
                {
                    return(record.PID.ToString());

                    //if (record.PID == 0)
                    //    return "System";
                    //else
                    //{
                    //    Process proc = null;
                    //    if ((proc = FindProcessByPid(record.PID)) != null)
                    //        return proc.ProcessName;
                    //    else
                    //        return "N/A";
                    //}
                }
            }

            return(String.Empty);
        }