已知计算机ip地址,如何用Socket枚举局域网中活动的计算机名以及当前用户名? 刚好最近在作这方面的事情,我用的方法是SOCKET连接地址列表中的每个终端,然后判断异常类型 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用远程的Socket中的EndPoint来判断.向客户端发送消息,如连通则发送确认信息,服务器端看是否有返回信息即可. 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{} } 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{} } 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) { } }} 我记得api里有,你可以查查,这样效率比较高。如果不是win98的机器,也可以用wmi 当前的用户名! 這個是難題我也提過類似的問題,見: http://expert.csdn.net/Expert/topic/2107/2107495.xml?temp=.1838953 新手求教,在webService中使用反射的问题。 CDO发送邮件时,如果把姓名显示出来 C# 把十六进制转成字符串 做一个抓网页的WinForm程序,如何模拟ASPX的__doPostBack? C# 可以通过Process调用ftp.exe吗? 如何做一个水晶报表报表编辑器 动态的创建属性及编辑的问题 关于字符串的简单问题,结贴给分,在线等! TextBox寬度問題 windows form 程序对运行环境有何要求? web窗体中Table控件的问题 树的问题
{
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{}
}
{
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{}
}
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)
{
}
}
}