artifi artifi
关注数: 12 粉丝数: 6 发帖数: 541 关注贴吧数: 14
一段源码,有错误,不会改,求帮忙修改。。。 源码有误,Indexoutofrange。。。求改啊 using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace scanner { class Scanner { string m_port=Console.ReadLine(); int m_host=Convert.ToInt32(Console.ReadLine()); public Scanner(string host,int port) { host=Convert.ToString(m_host); port=Convert.ToInt32(m_port); } public void scan() { TcpClient tc=new TcpClient(); tc.SendTimeout=tc.ReceiveTimeout=2000; try { Console.Write("checking port:{0}...",m_port); tc.Connect(Convert.ToString(m_host),Convert.ToInt32(m_port)); if(tc.Connected) { Console.WriteLine("port{0} is opened",m_port.ToString().PadRight(6)); Program.openedports.Add(Convert.ToInt32(m_port)); } } catch(System.Net.Sockets.SocketException e) { Console.WriteLine("port{0} is closed,{0}",m_port.ToString().PadRight(6),e); } finally { tc.Close(); Program.ScannedCount++; Program.RunningThreadCount--; Console.WriteLine(Program.ScannedCount); } } } 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=10000; static int MaxThread=5; public static void Main(string[] args) { string host=args[0]; string portrange=args[1]; Console.ReadKey(true); 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("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("port:{0} is opened",port.ToString().PadRight(6)); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
1 下一页