关于局域网内IP地址问题,请大家进来看一下~~

PLMM007 2003-06-23 02:03:07
根据下面的代码

public void ScanTarget()
{
//构造IP地址的31-8BIT 位,也就是固定的IP地址的前段
string strIPAddress="192.168.0.";
//开始扫描地址
int nStrat =1;
//终止扫描地址
int nEnd =255;
//扫描的操作
for(int i=nStrat;i<=nEnd;i++)
{
string strScanIPAdd = strIPAddress+i.ToString();
//转换成IP地址
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
try
{
//你可以加入自已的,增强功能
// Dns.GetHostByAddress 方法: 根据 IP 地
//址获取 DNS 主机信息。
IPHostEntry myScanHost =Dns.GetHostByAddress(myScanIP);
//获取主机的名
string strHostName =myScanHost.HostName.ToString();
richTextBox2.AppendText(strScanIPAdd+"->"+strHostName+"\r");
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}//for
}//private



如果在192.168.0.1-192.168.0.255之间没有存在的IP的话,它就提示请求的名称有效并在数据库中存在,但它没有相关的正确数据的错误提示,我想如果不存在就不要提示,我直接能得到192.168.0.1-192.168.0.255之间我的局域网存在的IP就行了,怎么改?谢谢!
...全文
96 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
skolay 2003-07-03
  • 打赏
  • 举报
回复
up
ArLi2003 2003-06-30
  • 打赏
  • 举报
回复
这是我以前写的一个简易端口扫描,你改改用吧,为了节省空间我将所有控制定义去掉了,你瞧一下程序部分吧

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;
using System.Net;
using System.Net.Sockets;


[STAThread]
static void Main()
{
Application.Run(new fm_Main());
}

private void IPBox(){
ArLi.CommonPrj.MsgBox.Warning(this,"Invalid IP Format\nIP Format:\n\nNumber.Number.Number.*");
}

public ArrayList threadList = new ArrayList();

private void GoScan(string ipAddr,int ServerPort) {
Poc clsPoc = new Poc();
clsPoc.myIPAddr = ipAddr;
clsPoc.ServerPort = ServerPort;
clsPoc.sForm = this;
ThreadStart tsTmp = new ThreadStart(clsPoc.PocScanOf);
Thread tdTmp = new Thread(tsTmp);

lock (this.lstBox_Job) {
this.lstBox_Job.Items.Add(ipAddr);
this.stBar_Job.Text = this.lstBox_Job.Items.Count.ToString();
}
lock (this.threadList) {
this.threadList.Add(tdTmp);
}

Application.DoEvents();
tdTmp.Start();
}

private void btn_Start_Click(object sender, System.EventArgs e) {
this.threadList = new ArrayList();
this.lstBox_Job.Items.Clear();
this.lstBox_ok.Items.Clear();

this.txt_Port.Text = this.txt_Port.Text.Trim();
this.txt_IP.Text = this.txt_IP.Text.Trim();

if (this.txt_IP.Text == "" || this.txt_Port.Text == "") {
ArLi.CommonPrj.MsgBox.Warning(this,"IP or Port Empty!");
return;
}

try {
int tmp = Int32.Parse(this.txt_Port.Text);
}
catch {
ArLi.CommonPrj.MsgBox.Warning(this,"Port Must Number");
}

string[] myIP = this.txt_IP.Text.Trim().Split('.');

if (myIP.Length<4){
this.IPBox();
return;
}

try {
int tmp;
for (int i=0; i<=2; i++) {
tmp = Int32.Parse(myIP[i]);
if (tmp <0 || tmp >254){
this.IPBox();
return;
}
}
}
catch {
this.IPBox();
return;
}

try {
int tmp = Int32.Parse(myIP[3]);
this.gBox_Target.Enabled = false;
this.GoScan(String.Join(".",myIP),Int32.Parse(this.txt_Port.Text.Trim()));
}
catch {
if (myIP[3] == "*") {
this.gBox_Target.Enabled = false;
for (int i=1; i<=254; i++) {
this.GoScan(myIP[0] + "." + myIP[1] + "." + myIP[2] + "." + i.ToString(),Int32.Parse(this.txt_Port.Text.Trim()));
}
}
else {
this.IPBox();
return;
}
}
}

private void fm_Main_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = ! this.gBox_Target.Enabled;
if (! this.gBox_Target.Enabled) {
ArLi.CommonPrj.MsgBox.Warning(this,"wait Socket connect Close");
}
}

private void gBox_Target_EnabledChanged(object sender, System.EventArgs e) {
this.picBox_Wait.Visible = (! this.gBox_Target.Enabled);
}

private void fm_Main_Load(object sender, System.EventArgs e) {
this.Text = Application.ProductName;
ArLi.CommonPrj.getResPrj ObjTmp = new ArLi.CommonPrj.getResPrj();
this.Icon = (Icon)ObjTmp.GetResOf("PortScanEx.Image","ico_Main");
this.picBox_Wait.Image = (Image)ObjTmp.GetResOf("PortScanEx.Image","wiz");
// this.picBox_Wait.Location = this.gBox_Target.Location;
// this.picBox_Wait.Size = this.gBox_Target.Size;
}

private void lstBox_ok_SelectedIndexChanged(object sender, System.EventArgs e) {
this.cMnu_Ok_Copy.Enabled = (this.lstBox_ok.SelectedItem.ToString() != "");
}

private void cMnu_Ok_Copy_IP_Click(object sender, System.EventArgs e) {
Clipboard.SetDataObject(this.lstBox_ok.SelectedItem.ToString(),true);
}

private void cMnu_Ok_Copy_IP_Port_Click(object sender, System.EventArgs e) {
Clipboard.SetDataObject(this.lstBox_ok.SelectedItem.ToString() + ":" + this.txt_Port.Text,true);
}
}

public class Poc{
public string myIPAddr = "";
public int ServerPort = 0;
public fm_Main sForm = null;

public void PocScanOf() {
IPAddress ipAddress = IPAddress.Parse(this.myIPAddr);
IPEndPoint HostEP = new IPEndPoint(ipAddress,this.ServerPort);

Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,
60 * 1000
);

sock.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendTimeout,
60 * 1000
);

try {
sock.Connect(HostEP);

byte[] bytesSendStr = new byte[1]{0};
sock.Send(bytesSendStr,bytesSendStr.Length,0);

lock (sForm.lstBox_ok) {
sForm.lstBox_ok.Items.Add(this.myIPAddr);
sForm.stBar_Ok.Text = sForm.lstBox_ok.Items.Count.ToString();
}
}
catch (Exception e){
sForm.stBar_Stats.Text = e.Message;
}
finally {
lock (sForm.lstBox_Job) {
sForm.lstBox_Job.Items.Remove(this.myIPAddr);
sForm.stBar_Job.Text = sForm.lstBox_Job.Items.Count.ToString();
if (sForm.lstBox_Job.Items.Count == 0) {
sForm.gBox_Target.Enabled = true;
}
}
try {
sock.Shutdown(SocketShutdown.Both);
}catch{}
sock.Close();
sock = null;
}
}
ArLi2003 2003-06-30
  • 打赏
  • 举报
回复
你怎么连续这么多问题?搞的好象要写一个pcanywhere 一样。。。

我上面说的:

使用254 条并行线程
各线程只要用socket 连接 139 端口(通则存在),比解析主机名快

这个难道你不会?留言给我又要我帮你写程序吗?晕。。
GreenSpring 2003-06-30
  • 打赏
  • 举报
回复
up
lovered 2003-06-30
  • 打赏
  • 举报
回复
[v]
CSTerry 2003-06-30
  • 打赏
  • 举报
回复
using System;
using System.DirectoryServices;
using System.Net;

class TestClass
{
static void Main (string[] args)
{
ShowComputers();
}
public static void ShowComputers()
{
//or use "WinNT://your_domain_name"
DirectoryEntry root = new DirectoryEntry("WinNT:"); DirectoryEntries domains = root.Children;
domains.SchemaFilter.Add("domain");
foreach (DirectoryEntry domain in domains)
{
Console.WriteLine(domain.Name);
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach (DirectoryEntry computer in computers)
{
Console.WriteLine("\t" + computer.Name);
IPHostEntry iphe = Dns.GetHostByName(computer.Name);
foreach (IPAddress ip in iphe.AddressList)
{
Console.WriteLine("\t\t" + ip);
}
DirectoryEntries users = computer.Children;
}
}
}

}
PLMM007 2003-06-30
  • 打赏
  • 举报
回复
up
ArLi2003 2003-06-29
  • 打赏
  • 举报
回复
使用254 条并行线程

各线程只要用socket 连接 139 端口(通则存在),比解析主机名快
PLMM007 2003-06-24
  • 打赏
  • 举报
回复
up
PLMM007 2003-06-23
  • 打赏
  • 举报
回复
我增加了一个线程,好象速度也慢呀
PLMM007 2003-06-23
  • 打赏
  • 举报
回复
up
conquersky 2003-06-23
  • 打赏
  • 举报
回复
能具体而简洁的说一下吗? 谢谢
wudixiaocaoren 2003-06-23
  • 打赏
  • 举报
回复
也只能用多线程了,没什么更好的办法
PLMM007 2003-06-23
  • 打赏
  • 举报
回复
楼上说的是,怎么实现?
allanyan 2003-06-23
  • 打赏
  • 举报
回复
采用并行机制了!用多线程吧!
至于想预先判断IP是否存在,就只好先ping一下了!
PLMM007 2003-06-23
  • 打赏
  • 举报
回复
还有没有速度更快的遍历方法,这个速度很慢的

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧