我是通过路由器和另一个人合用一个Adsl上网的,路由器用的是TP-link R402,我想在外面上网时能登陆到家中的主机,这样必须知道家里计算机上网的IP,但Adsl的IP地址是动态分配的,如何得到家中主机的IP呢?
我的解决方法:路由器的管理页面中有当前的主机IP地址 ,我写了一个程序,解析得到ip
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using Sgml;
using System.Xml;
namespace GetIPAddress
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.TextBox IPAddr;
private System.Windows.Forms.Button GetIP;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.IPAddr = new System.Windows.Forms.TextBox();
this.GetIP = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// IPAddr
//
this.IPAddr.Enabled = false;
this.IPAddr.Location = new System.Drawing.Point(32, 56);
this.IPAddr.Name = "IPAddr";
this.IPAddr.Size = new System.Drawing.Size(224, 21);
this.IPAddr.TabIndex = 0;
this.IPAddr.Text = "";
//
// GetIP
//
this.GetIP.Location = new System.Drawing.Point(96, 112);
this.GetIP.Name = "GetIP";
this.GetIP.Size = new System.Drawing.Size(88, 23);
this.GetIP.TabIndex = 1;
this.GetIP.Text = "WebRequest";
this.GetIP.Click += new System.EventHandler(this.GetIP_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 197);
this.Controls.Add(this.GetIP);
this.Controls.Add(this.IPAddr);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
private void GetIP_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sPageUrl="http://192.168.1.1/userRpm/StatusRpm.htm";
string sXPath="/html/body/center/form/table[2]/tr[2]/td/table/tr/td[2]/table/tr[2]";
string sContent="";
WebRequest request = WebRequest.Create(sPageUrl);
request.Credentials=new NetworkCredential("admin","admin");
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
SgmlReader reader = new SgmlReader();
reader.DocType = "HTML";
reader.InputStream = new StringReader(sr.ReadToEnd());
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.Indented;
while (reader.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (reader.NodeType != XmlNodeType.Whitespace)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
writer.WriteNode(reader, true);
}
}
sContent = sw.ToString();
if (sXPath.Length == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.IPAddr.Text = sw.ToString();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument(new StringReader(sw.ToString()));
System.Xml.XPath.XPathNavigator nav = doc.CreateNavigator();
System.Xml.XPath.XPathNodeIterator nodes = nav.Select(sXPath);
while (nodes.MoveNext())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append(nodes.Current.Value + "\n");
}
this.IPAddr.Text = sb.ToString();
}
resStream.Close();
sr.Close();
}
}
}
这里引用了一个组件SGML,是对SGML进行解析的
运行结果如图
这里引用了一个组件SGML,是对SGML进行解析的 运行结果如图
接着就是做一服务,每个X分钟检测一次ip是否跟以前相同,如ip改变,自动发邮件到我的邮箱,发送邮件函数在我的文档可以找到