扫描指定ip的端口(C#)
class PingExam
{
public static void Main()
{
Ping ping = new Ping();
string ip = "192.168.1.43"; // 目标ip
int[] ports = { , , , , , }; // 需要扫描的端口
scanPort(IPAddress.Parse(ip), ports);
} private static void scanPort(IPAddress address, int startPort, int endPort)
{
int[] ports = new int[endPort - startPort + ];
for (int i = ; i < endPort-startPort+; i++) {
ports[i] = startPort + i;
}
scanPort(address, ports);
} private static void scanPort(IPAddress address, int[] ports)
{
try {
int count = ports.Length;
AutoResetEvent[] arEvents = new AutoResetEvent[count]; // 同步对象
for (int i = ; i < count; i++) {
arEvents[i] = new AutoResetEvent(false); // 同步对象, 初始未触发
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Any, ));
socket.BeginConnect(new IPEndPoint(address, ports[i]),
callback,
new ArrayList() { socket, ports[i], arEvents[i]} // 传递3个参数: 当前socket, 端口, 同步对象
);
} WaitHandle.WaitAll(arEvents); // 等待所有扫描工作完成
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
} private static void callback(IAsyncResult ar) // 扫描完成后的回调方法
{
ArrayList list = (ArrayList)ar.AsyncState; // 获取传递的参数
Socket socket = (Socket)list[];
int port = (int)list[];
AutoResetEvent arevent = (AutoResetEvent)list[]; if (ar.IsCompleted && socket.Connected) {
Console.WriteLine("port: {0} open.", port); // 检查 connected属性, 如果为true 则表示是开放的
}
else {
Console.WriteLine("port: {0} closed.", port);
}
try {
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch {
}
arevent.Set(); // 完成后触发
}
}
扫描指定ip的端口(C#)的更多相关文章
- CentOS6.5 配置防火墙+允许指定ip访问端口
参考博文: iptables防火墙只允许指定ip连接指定端口.访问指定网站 一.配置防火墙 打开配置文件 [root@localhost ~]# vi /etc/sysconfig/iptables ...
- centos7 firewall指定IP与端口访问(常用)
1.启动防火墙 systemctl start firewalld.service 2.指定IP与端口 firewall-cmd --permanent --add-rich-rule="r ...
- pycharm中指定ip和端口
pycharm中指定ip和端口 环境: 系统:win7 本机ip:192.168.0.100 1.建立工程请参照:https://www.cnblogs.com/effortsing/p/103945 ...
- centos7 firewall指定IP与端口、端段访问(常用)
https://blog.csdn.net/yipianfuyunsm/article/details/99998332 https://www.cnblogs.com/co10rway/p/8268 ...
- MySQL指定ip和端口连接数据库,并修改数据库密码
一.指定ip和端口连接数据库 命令 mysql -u root -h (ip) -P (端口)-p 假设ip是:127.0.0.1:端口是:13326,连接的命令: mysql -u root -h ...
- python - socket - client端指定ip和端口
问题描述: 在设备中有3个NI, ip分别为192.168.1.5/6/7.其中本端192.168.1.6同对端192.168.1.10建立了一个tunnel. 我希望测试tunnel连通性, 对端起 ...
- 运行Django项目指定IP和端口
默认IP和端口 python manage.py runserver 指定端口: python manage.py runserver 192.168.12.12:8080 此时会报错,我们需要修改配 ...
- tcpdump指定IP和端口抓包
如下指定抓www.baidu.com 并且80端口的包 保存到test.cap 可以在Windows下面用wireshark打开 tcpdump 'port 80 and host www.baidu ...
- pycharm开发flask指定ip、端口无效
原因分析 是因为使用了pycharm的版本的问题.并不是flask框架本身的问题(不管你是如何设置的flask配置,通过加载config也好,还是通过run的时候传入形参也好,均不影响) 可以很明显的 ...
随机推荐
- python渗透测试工具包
网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库.pypcap, Pcapy, pylibpcap: 几个不同 libpcap 捆绑的py ...
- lambda架构简介
1.Lambda架构背景介绍 Lambda架构是由Storm的作者Nathan Marz提出的一个实时大数据处理框架.Marz在Twitter工作期间开发了著名的实时大数据处理框架Storm,Lamb ...
- C语言指针的一些题目
1.将从键盘输入的每个单词的第一个字母转换成大写字母输入时各单词以空格隔开,用“.”结束输入 解体思路: 把输入的字符存入字符数组中直到输入".",然后调用函数,把字符串的第一个 ...
- [译]作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的
前日在cnblogs上看到一遍文章<每个程序员都必读的12篇文章>,其中大多数是E文的. 先译其中一篇web相关的”每个程序员必知之WEB开发”. 原文: http://programme ...
- GridEh Lookup
Flexible adjustment of a lookup inplace editor 没有输入拼音码搜索功能. Drop-Down Forms 这个比较符合中国人的习惯,搜索框,不错,点下来箭 ...
- 菜单与内容下拉jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spark报错:Failed to locate the winutils binary in the hadoop binary path
之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报 Failed to ...
- tomcat加固
tomcat安全加固和规范 tomcat是一个开源Web服务器,基于Tomcat的Web运行效率高,可以在一般的硬件平台上流畅运行,因此,颇受Web站长的青睐.不过,在默认配置下其存在一定的安全隐患, ...
- 在eclipse中的maven工程中执行maven命令的步骤
执行maven命令的步骤: 1.找到maven工程的pom.xml文件,点中右键 2.在弹出的对话框中选择run as 3.在弹出的对话框中输入compile 再执行即可
- 49. Group Anagrams (string, HashTable)
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...