C# 判断端口是否打开
展开全部
TCP还是UDP?创建对应的组件,并绑定到5000端口上,如果创建成功就是没打开,如果创建失败了就是已经打开了(因为不能重复打开)。最后记得如果打开成功了再关闭即可。
绑定5000端口,失败基本就代表端口已经在使用了。。。
//===========================================================
// C# 实现端口扫描
//===========================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConApp
{
class Program
{
//已扫描端口数目
internal static int scannedCount = 0;
internal static int runningThreadCount = 0;
internal static List<int> openedPorts = new List<int>();
static int startPort = 1;
static int endPort = 500;
static int maxThread = 100;
static void Main(string[] args)
{
//简单提示
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("// Writer;Feeling");
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("请输入要扫描的主机;");
string host = Console.ReadLine();
Console.WriteLine("请输入扫描的端口 例如:1-800");
string portRange = Console.ReadLine();
startPort = int.Parse(portRange.Split('-')[0].Trim());
endPort = int.Parse(portRange.Split('-')[1].Trim());
for (int port = startPort; port < endPort; port++)
{
Scanner scanner = new Scanner(host, port);
Thread thread = new Thread(new ThreadStart(scanner.Scan));
thread.Name = port.ToString();
thread.IsBackground = true;
thread.Start();
runningThreadCount++;
Thread.Sleep(10);
//循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
while (runningThreadCount >= maxThread) ;
}
//空循环,直到所有端口扫描完毕
while (scannedCount + 1 < (endPort - startPort)) ;
Console.WriteLine();
Console.WriteLine();
//输出结果
Console.WriteLine("Scan for host:{0} has been completed, \n total {1} ports scanned, \n opened ports:{2}", host, (endPort - startPort), openedPorts.Count);
foreach (int port in openedPorts)
{
Console.WriteLine("\tport: {0} is open", port.ToString().PadLeft(6));
}
Console.ReadLine();
}
}
class Scanner
{
string m_host;
int m_port;
public Scanner(string host, int port)
{
m_host = host;
m_port = port;
}
public void Scan()
{
TcpClient tc = new TcpClient();
tc.SendTimeout = tc.ReceiveTimeout = 2000;
try
{
tc.Connect(m_host, m_port);
if (tc.Connected)
{
Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
Program.openedPorts.Add(m_port);
}
}
catch
{
Console.WriteLine("Port {0} is Closed", m_port.ToString().PadRight(6));
}
finally
{
tc.Close();
tc = null;
Program.scannedCount++;
Program.runningThreadCount--;
}
}
}
}
绑定5000端口,失败基本就代表端口已经在使用了。。。
//===========================================================
// C# 实现端口扫描
//===========================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConApp
{
class Program
{
//已扫描端口数目
internal static int scannedCount = 0;
internal static int runningThreadCount = 0;
internal static List<int> openedPorts = new List<int>();
static int startPort = 1;
static int endPort = 500;
static int maxThread = 100;
static void Main(string[] args)
{
//简单提示
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("// Writer;Feeling");
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("请输入要扫描的主机;");
string host = Console.ReadLine();
Console.WriteLine("请输入扫描的端口 例如:1-800");
string portRange = Console.ReadLine();
startPort = int.Parse(portRange.Split('-')[0].Trim());
endPort = int.Parse(portRange.Split('-')[1].Trim());
for (int port = startPort; port < endPort; port++)
{
Scanner scanner = new Scanner(host, port);
Thread thread = new Thread(new ThreadStart(scanner.Scan));
thread.Name = port.ToString();
thread.IsBackground = true;
thread.Start();
runningThreadCount++;
Thread.Sleep(10);
//循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
while (runningThreadCount >= maxThread) ;
}
//空循环,直到所有端口扫描完毕
while (scannedCount + 1 < (endPort - startPort)) ;
Console.WriteLine();
Console.WriteLine();
//输出结果
Console.WriteLine("Scan for host:{0} has been completed, \n total {1} ports scanned, \n opened ports:{2}", host, (endPort - startPort), openedPorts.Count);
foreach (int port in openedPorts)
{
Console.WriteLine("\tport: {0} is open", port.ToString().PadLeft(6));
}
Console.ReadLine();
}
}
class Scanner
{
string m_host;
int m_port;
public Scanner(string host, int port)
{
m_host = host;
m_port = port;
}
public void Scan()
{
TcpClient tc = new TcpClient();
tc.SendTimeout = tc.ReceiveTimeout = 2000;
try
{
tc.Connect(m_host, m_port);
if (tc.Connected)
{
Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
Program.openedPorts.Add(m_port);
}
}
catch
{
Console.WriteLine("Port {0} is Closed", m_port.ToString().PadRight(6));
}
finally
{
tc.Close();
tc = null;
Program.scannedCount++;
Program.runningThreadCount--;
}
}
}
}
展开全部
TCP还是UDP?创建对应的组件,并绑定到5000端口上,如果创建成功就是没打开,如果创建失败了就是已经打开了(因为不能重复打开)。最后记得如果打开成功了再关闭即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
绑定5000端口,失败基本就代表端口已经在使用了。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//===========================================================
// C# 实现端口扫描
//===========================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConApp
{
class Program
{
//已扫描端口数目
internal static int scannedCount = 0;
internal static int runningThreadCount = 0;
internal static List<int> openedPorts = new List<int>();
static int startPort = 1;
static int endPort = 500;
static int maxThread = 100;
static void Main(string[] args)
{
//简单提示
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("// Writer;Feeling");
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("请输入要扫描的主机;");
string host = Console.ReadLine();
Console.WriteLine("请输入扫描的端口 例如:1-800");
string portRange = Console.ReadLine();
startPort = int.Parse(portRange.Split('-')[0].Trim());
endPort = int.Parse(portRange.Split('-')[1].Trim());
for (int port = startPort; port < endPort; port++)
{
Scanner scanner = new Scanner(host, port);
Thread thread = new Thread(new ThreadStart(scanner.Scan));
thread.Name = port.ToString();
thread.IsBackground = true;
thread.Start();
runningThreadCount++;
Thread.Sleep(10);
//循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
while (runningThreadCount >= maxThread) ;
}
//空循环,直到所有端口扫描完毕
while (scannedCount + 1 < (endPort - startPort)) ;
Console.WriteLine();
Console.WriteLine();
//输出结果
Console.WriteLine("Scan for host:{0} has been completed, \n total {1} ports scanned, \n opened ports:{2}", host, (endPort - startPort), openedPorts.Count);
foreach (int port in openedPorts)
{
Console.WriteLine("\tport: {0} is open", port.ToString().PadLeft(6));
}
Console.ReadLine();
}
}
class Scanner
{
string m_host;
int m_port;
public Scanner(string host, int port)
{
m_host = host;
m_port = port;
}
public void Scan()
{
TcpClient tc = new TcpClient();
tc.SendTimeout = tc.ReceiveTimeout = 2000;
try
{
tc.Connect(m_host, m_port);
if (tc.Connected)
{
Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
Program.openedPorts.Add(m_port);
}
}
catch
{
Console.WriteLine("Port {0} is Closed", m_port.ToString().PadRight(6));
}
finally
{
tc.Close();
tc = null;
Program.scannedCount++;
Program.runningThreadCount--;
}
}
}
}
// C# 实现端口扫描
//===========================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConApp
{
class Program
{
//已扫描端口数目
internal static int scannedCount = 0;
internal static int runningThreadCount = 0;
internal static List<int> openedPorts = new List<int>();
static int startPort = 1;
static int endPort = 500;
static int maxThread = 100;
static void Main(string[] args)
{
//简单提示
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("// Writer;Feeling");
Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
Console.WriteLine("请输入要扫描的主机;");
string host = Console.ReadLine();
Console.WriteLine("请输入扫描的端口 例如:1-800");
string portRange = Console.ReadLine();
startPort = int.Parse(portRange.Split('-')[0].Trim());
endPort = int.Parse(portRange.Split('-')[1].Trim());
for (int port = startPort; port < endPort; port++)
{
Scanner scanner = new Scanner(host, port);
Thread thread = new Thread(new ThreadStart(scanner.Scan));
thread.Name = port.ToString();
thread.IsBackground = true;
thread.Start();
runningThreadCount++;
Thread.Sleep(10);
//循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
while (runningThreadCount >= maxThread) ;
}
//空循环,直到所有端口扫描完毕
while (scannedCount + 1 < (endPort - startPort)) ;
Console.WriteLine();
Console.WriteLine();
//输出结果
Console.WriteLine("Scan for host:{0} has been completed, \n total {1} ports scanned, \n opened ports:{2}", host, (endPort - startPort), openedPorts.Count);
foreach (int port in openedPorts)
{
Console.WriteLine("\tport: {0} is open", port.ToString().PadLeft(6));
}
Console.ReadLine();
}
}
class Scanner
{
string m_host;
int m_port;
public Scanner(string host, int port)
{
m_host = host;
m_port = port;
}
public void Scan()
{
TcpClient tc = new TcpClient();
tc.SendTimeout = tc.ReceiveTimeout = 2000;
try
{
tc.Connect(m_host, m_port);
if (tc.Connected)
{
Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
Program.openedPorts.Add(m_port);
}
}
catch
{
Console.WriteLine("Port {0} is Closed", m_port.ToString().PadRight(6));
}
finally
{
tc.Close();
tc = null;
Program.scannedCount++;
Program.runningThreadCount--;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
bool tcpListen=false;
bool udpListen=false;//设定端口状态标识位
System.Net.IPAddress myIpAddress=IPAddress.Parse("202.96.170.165");
System.Net.IPEndPoint myIpEndPoint=newIPEndPoint(myIpAddress,8000);
try
{
System.Net.Sockets.TcpClient tcpClient=newTcpClient();
tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
tcpListen=true;
}
catch{}
try
{
System.Net.Sockets.UdpClient udpClient=newUdpClient();
udpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出UDP连接请求
udpListen=true;
}
catch{}
if(tcpListen==false&&udpListen==false)
{
MessageBox.Show("8000端口关闭!","提示");
}
else
MessageBox.Show("8000端口打开!","提示");}
bool udpListen=false;//设定端口状态标识位
System.Net.IPAddress myIpAddress=IPAddress.Parse("202.96.170.165");
System.Net.IPEndPoint myIpEndPoint=newIPEndPoint(myIpAddress,8000);
try
{
System.Net.Sockets.TcpClient tcpClient=newTcpClient();
tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
tcpListen=true;
}
catch{}
try
{
System.Net.Sockets.UdpClient udpClient=newUdpClient();
udpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出UDP连接请求
udpListen=true;
}
catch{}
if(tcpListen==false&&udpListen==false)
{
MessageBox.Show("8000端口关闭!","提示");
}
else
MessageBox.Show("8000端口打开!","提示");}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询