//线程池要委托的方法 void ThreadScan(object obj) { TcpConnect端口 myTcpConnect端口 = (TcpConnect端口)obj;//把发送的结构转换成自己自定义的结构/拆箱 try { myTcpConnect端口.myTcpClient.Connect(myTcpConnect端口.hostname, myTcpConnect端口.x); SetText("目标端口: " + myTcpConnect端口.x.ToString() + " 目前打开!!"); //输出当前打开的端口 myTcpConnect端口.myTcpClient.Close(); //关闭TCP客户端实例 } catch { SetText("目标端口: " + myTcpConnect端口.x.ToString() + " 关闭!!");//输出连接失败的原因 } }
private void Port_Click(object sender, EventArgs e) { //获取你输入的IP地址或者主机名称 string pHostName = text1.Text.Trim().Replace("。", "."); int a = 0; int b = 255; if (text3.Text != "" && text4.Text != "") { try { a = Convert.ToInt32(text3.Text); b = Convert.ToInt32(text4.Text); } catch { return; } } if (a > b)//防止a大于b { int k = a; a = b; b = k; } panel.Visible = false; text2.Text = ""; for (int i = a; i <= b; i++) //循环连接输入范围内的端口 { TcpClient myTcpClient = new TcpClient(); //实例化一个Tcp客户端 //多线程池扫描 TcpConnect端口 myTcpConnect端口 = new TcpConnect端口(pHostName, i, myTcpClient); //封装要发送的参数 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadScan), myTcpConnect端口); } //PortPanel.Text = "导出"; }