已知计算机ip地址,如何用Socket枚举局域网中活动的计算机名以及当前用户名?

wyfzy521 2003-08-26 09:52:02
以前下载过一个这样的例程,后来不小心删除了,哪位大侠能够提供点这方面的信息?
那个程序好像用了两个线程,然后给局域网一定范围内的机器发送数据,返回的数据中包含该机器的名称和当前的用户名。
...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice90 2003-09-07
  • 打赏
  • 举报
回复
mark
zhbname 2003-09-03
  • 打赏
  • 举报
回复
up
derek 2003-09-02
  • 打赏
  • 举报
回复
当前的用户名! 這個是難題

我也提過類似的問題,見:

http://expert.csdn.net/Expert/topic/2107/2107495.xml?temp=.1838953
jcaomao 2003-09-01
  • 打赏
  • 举报
回复
我记得api里有,你可以查查,这样效率比较高。

如果不是win98的机器,也可以用wmi
kuangren 2003-09-01
  • 打赏
  • 举报
回复
可以从WMI和注册表获取
angxain 2003-08-30
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Threading;

namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 24);
this.button1.TabIndex = 0;
this.button1.Text = "本機IP地址";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 24);
this.button2.TabIndex = 1;
this.button2.Text = "遠端電腦IP地址";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 64);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 112);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(248, 160);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 16);
this.label1.TabIndex = 5;
this.label1.Text = "IP地址:";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// button3
//
this.button3.Location = new System.Drawing.Point(352, 64);
this.button3.Name = "button3";
this.button3.TabIndex = 6;
this.button3.Text = "button3";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(544, 317);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.label1,
this.richTextBox1,
this.textBox2,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
IPHostEntry myHost= new IPHostEntry();
try
{
myHost=Dns.GetHostByName(Dns.GetHostName());

for(int i=0;i<myHost.AddressList.Length;i++)
{
richTextBox1.AppendText("±??uIPμOO·"+myHost.AddressList[i].ToString()+"\r");
}
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}

private void richTextBox1_TextChanged(object sender, System.EventArgs e)
{

}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{

}

private void textBox2_TextChanged(object sender, System.EventArgs e)
{

}

private void button2_Click(object sender, System.EventArgs e)
{
IPHostEntry myDnsToIP = new IPHostEntry();
myDnsToIP = Dns.Resolve(textBox2.Text.ToString());
for(int i=0;i<myDnsToIP.AddressList.Length;i++)
{
richTextBox1.AppendText(textBox2.Text+"μAIPμOO·Ia"+myDnsToIP.AddressList[i].ToString()+"\r");
}



}

private void button3_Click(object sender, System.EventArgs e)
{

Thread thScan = new Thread(new ThreadStart(ScanTarget));
thScan.Start();

}
private void ScanTarget()
{
string strIPAddress= "192.168.100.";
for(int i=1;i<20;i++)
{
string strScanIPAdd = strIPAddress+i.ToString();
IPAddress myScanIP= IPAddress.Parse(strScanIPAdd);
try
{
IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
string strHostName= myScanHost.HostName.ToString();
richTextBox1.AppendText(strScanIPAdd+"->"+strHostName+"\r");

}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}



}

private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
{

}

private void numericUpDown3_ValueChanged(object sender, System.EventArgs e)
{

}

private void numericUpDown2_ValueChanged(object sender, System.EventArgs e)
{

}

private void numericUpDown4_ValueChanged(object sender, System.EventArgs e)
{

}

private void numericUpDown5_ValueChanged(object sender, System.EventArgs e)
{

}
}
}
goodluck001 2003-08-30
  • 打赏
  • 举报
回复
private void HostsInit()
{
IPHostEntry host=new IPHostEntry();
for(int i=1;i<=254;i++)
Dns.BeginGetHostByName("192.168.0."+i,new AsyncCallback(HostsInit),host);
}

private void HostsInit(IAsyncResult ar)
{
try
{
IPHostEntry host=Dns.EndGetHostByName(ar);
IPHostEntry host1=Dns.GetHostByAddress(host.AddressList[0]);

if(host1.HostName.Trim() != Me)//Me是本地计算机
cb_host.Items.Add(host1.HostName);//cb_host 是一个CombBox,用于记录局域网中的计算机
}
catch{}
}
goodluck001 2003-08-30
  • 打赏
  • 举报
回复
private void HostsInit()
{
IPHostEntry host=new IPHostEntry();
for(int i=1;i<=254;i++)
Dns.BeginGetHostByName("192.168.0."+i,new AsyncCallback(HostsInit),host);
}

private void HostsInit(IAsyncResult ar)
{
try
{
IPHostEntry host=Dns.EndGetHostByName(ar);
IPHostEntry host1=Dns.GetHostByAddress(host.AddressList[0]);

if(host1.HostName.Trim() != Me)//Me是本地计算机
cb_host.Items.Add(host1.HostName);//cb_host 是一个CombBox,用于记录局域网中的计算机
}
catch{}
}
jn_sly 2003-08-28
  • 打赏
  • 举报
回复
dns.resolve()
_weiKun_ 2003-08-27
  • 打赏
  • 举报
回复
用远程的Socket中的EndPoint来判断.向客户端发送消息,如连通则发送确认信息,服务器端看是否有返回信息即可.
wzzw 2003-08-26
  • 打赏
  • 举报
回复
刚好最近在作这方面的事情,我用的方法是SOCKET连接地址列表中的每个终端,然后判断异常类型

110,502

社区成员

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

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

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