博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用c# web编程端口是否开放
阅读量:5110 次
发布时间:2019-06-13

本文共 2914 字,大约阅读时间需要 9 分钟。

使用 输入主机IP或域名,输入开始结束端口。 原理 主要使用了ASP.NET 2.0的System.Net.Sockets组件。 StartPort = Convert.ToInt32(numStart.Text); EndPort = Convert.ToInt32(numEnd.Text); ipAdres = txtIP.Text; Thread[] pool = new Thread[(EndPort - StartPort) + 1]; int i = 0; DateTime start = DateTime.Now; // Loop through the ports between start port and end port for (int CurrPort = StartPort; CurrPort <= EndPort; CurrPort++) {
Thread th = new Thread(new System.Threading.ParameterizedThreadStart(portAc)); //NOTE: better to leave to system. // th.Priority = ThreadPriority.AboveNormal; th.Start(CurrPort); pool[i] = th; i++; } #region thread pool int k = --i; int retryCount = 0; for (; i >= 0; i--) {
if (pool[i].IsAlive) {
i = k; retryCount++; continue; } if (retryCount == 1000) {
break; } } #endregion #region httpfinger if (http) {
// Create a request for the URL. WebRequest request = WebRequest.Create("http://" + txtIP.Text); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. try{
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string serverType = response.Headers["server"]; if (serverType.Contains("IIS")) {
lblServer.Text = "Windows System "; if (serverType.Contains("5.")) {
lblServer.Text += "XP/2000"; } if (serverType.Contains("6.")) {
lblServer.Text += "2003"; } } if (serverType.ToLower().Contains("apache")) {
lblServer.Text += "probably linux"; } lblServer.Text += " " + serverType; } catch(Exception Err){
//sometime which returns 404 and it makes a problem. } } #endregion DateTime end = DateTime.Now; TimeSpan sonuc = end - start; lblzaman.Text = sonuc.TotalSeconds + " total secs";上面是主要的代码,当然我们需要使用线程了,否则,这么多端口的扫描是非常浪费时间的。 public void portAc(object portNoObj) {
int portNo = (int)portNoObj; TcpClient TcpScan = new TcpClient(); try {
// Try to connect TcpScan.Connect(ipAdres, portNo); if (!TcpScan.Connected) return; // If there's no exception, we can say the port is open log += "Port " + portNo + " open\r\n"; //NOTE: We may include more finger tips to here switch (portNo) {
case 80: http = true; break; } try {
DataRow dr = dt.NewRow(); dr[0] = "http://www.portsdb.org/bin/portsdb.cgi?portnumber=" + portNo + "&protocol=ANY&String="; dt.Rows.Add(dr); } // Ends Try catch (Exception Err) {
throw Err; } } catch {
// An exception occured, thus the port is probably closed } } //TcpClient类提供了网络连接、发送和接收的功能,不过这里我们只是使用了它的连接方法。

转载于:https://www.cnblogs.com/nibulu/archive/2011/08/04/2127699.html

你可能感兴趣的文章
【题解】[P4178 Tree]
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
QML学习笔记之一
查看>>
WPF中实现多选ComboBox控件
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
关于TFS2010使用常见问题
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
ionic2+ 基础
查看>>
Screening technology proved cost effective deal
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>