using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Security.Cryptography.X509Certificates;
using System.Data.SqlTypes;
using System.IO;
using System.Linq.Expressions;
namespace FullConnetScaner
{
public partial class Form1 : Form
{
public Int32 connState = 0;
public int portsum = 0;
public string scanHost;
public string[] portListArray;
public AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void buttonStart_Click(object sender, EventArgs e)
{
if(textIP.Text == "")
{
MessageBox.Show("請輸入一個域名或者IP地址", "埠掃描程式",MessageBoxButtons.OK);
return;
}
scanHost = textIP.Text;
try
{
IPAddress iPAddress = (IPAddress)Dns.Resolve(scanHost).AddressList.GetValue(0);
textIP.Text = iPAddress.ToString();
}
catch
{
textIP.Focus();
MessageBox.Show("請輸入正確的主機地址,該地址無法進行決議", "埠掃描程式", MessageBoxButtons.OK);
return;
}
ThreadPool.SetMaxThreads(Convert.ToInt32(numUpDownThread.Value), Convert.ToInt32(numUpDownThread.Value));
for(Int32 threadNum = Convert.ToInt32(numUpDownPortStart.Value);threadNum<=Convert.ToInt32(numUpDownPortEnd.Value);threadNum++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartScan), threadNum);
}
void StartScan(object state)
{
Int32 port = (Int32)state;
string msg = "";
string getData = "";
connState++;
try
{
TcpClient tcp = new TcpClient();
tcp.SendTimeout = tcp.ReceiveTimeout = 1000;
tcp.Connect(scanHost, port);
portsum++;
msg = port.ToString()+"埠開放";
portListArray[portsum - 1] = msg;
Stream stream = tcp.GetStream();
stream.Write(Encoding.Default.GetBytes(msg.ToCharArray()), 0, msg.Length);
StreamReader streamReader = new StreamReader(tcp.GetStream(), Encoding.Default);
try
{
getData = streamReader.ReadLine();
if(getData.Length!=0)
{
msg = port.ToString() + "埠資料:" + getData.ToString(); ;
portListArray[portsum - 1] = msg;
textShowInfo.Items.Add(msg);
}
}
catch(Exception err)
{
textShowInfo.Items.Add(err.Message);
}
finally
{
streamReader.Close();
stream.Close();
tcp.Close();
Thread.Sleep(1000);
}
}
catch(Exception err)
{
textShowInfo.Items.Add(err.Message);
}
finally
{
Thread.Sleep(1000);
asyncOpsAreDone.Close();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
求助各位大神,為什么我的stream回傳值是NULL,哪里可以修改一下
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/104827.html
