Exemplo n.º 1
0
        /// <summary>
        /// Prints out all tcpconnections and the state of the connection
        /// as well as the process using it.
        /// </summary>
        /// <returns>a string with all the info</returns>
        public static string PrintOutTcpConnections()
        {
            StringBuilder result = new StringBuilder();

            try
            {
                foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                {
                    result.AppendLine(string.Format("  {0,-7}{1,-23}{2, -23}{3,-14}{4}", "TCP", tcpRow.LocalEndPoint, tcpRow.RemoteEndPoint, tcpRow.State, Process.GetProcessById(tcpRow.ProcessId).ProcessName));
                }
            }
            catch (Exception ex) { mainForm.WriteLine(ex.Message, Color.Red); }
            return(result.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a port in use by a process
        /// </summary>
        /// <param name="process">The process you want to check</param>
        /// <returns>the port this process is using, if the process
        /// does not use a port it returns 0</returns>
        public static int FindPortInUseByProcess(Process process)
        {
            int result = 0;

            try
            {
                foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                {
                    if (tcpRow.ProcessId == process.Id)
                    {
                        // the port is everything after : IE: 0.0.0.0:2869
                        string parse = string.Format("{0,-7}", tcpRow.LocalEndPoint).Split(':').Last();
                        int.TryParse(parse.Trim(), out result);
                    }
                }
            }
            catch (Exception ex) { mainForm.WriteLine(ex.Message, Color.Red); }
            return(result);
        }
Exemplo n.º 3
0
        public static Process GetProcessCommunicatingWithRemoteIP(IPAddress address)
        {
            Process result = null;

            try
            {
                foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                {
                    //mainForm.WriteLine(string.Format("Comparing {0} to {1}", tcpRow.RemoteEndPoint.ToString(), address.ToString()));
                    string compare = tcpRow.RemoteEndPoint.Address.ToString();
                    if (compare.Contains(address.ToString()))
                    {
                        result = Process.GetProcessById(tcpRow.ProcessId);
                    }
                }
            }
            catch (Exception ex) { mainForm.WriteLine(ex.Message, Color.Red); }
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a process that is listening on port
        /// </summary>
        /// <param name="port">the port to check </param>
        /// <returns>the process listening on this port, if none returns null</returns>
        public static Process GetProcessByPortInUse(int port)
        {
            Process result  = null;
            int     tryPort = 0;

            try
            {
                foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                {
                    string parse = string.Format("{0,-7}", tcpRow.LocalEndPoint).Split(':').Last().Trim();
                    if (int.TryParse(parse, out tryPort))
                    {
                        if (tryPort == port)
                        {
                            result = Process.GetProcessById(tcpRow.ProcessId);
                        }
                    }
                }
            }
            catch (Exception ex) { mainForm.WriteLine(ex.Message, Color.Red); }
            return(result);
        }