我是通过路由器和另一个人合用一个Adsl上网的,路由器用的是TP-link R402,我想在外面上网时能登陆到家中的主机,这样必须知道家里计算机上网的IP,但Adsl的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
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox IPAddr;
private System.Windows.Forms.Button GetIP;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void GetIP_Click(object sender, System.EventArgs e)
{
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())
{
if (reader.NodeType != XmlNodeType.Whitespace)
{
writer.WriteNode(reader, true);
}
}
sContent = sw.ToString();
if (sXPath.Length == 0)
{
this.IPAddr.Text = sw.ToString();
}
else
{
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())
{
sb.Append(nodes.Current.Value + "\n");
}
this.IPAddr.Text = sb.ToString();
}
resStream.Close();
sr.Close();
}
}
}
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
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox IPAddr;
private System.Windows.Forms.Button GetIP;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void GetIP_Click(object sender, System.EventArgs e)
{
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())
{
if (reader.NodeType != XmlNodeType.Whitespace)
{
writer.WriteNode(reader, true);
}
}
sContent = sw.ToString();
if (sXPath.Length == 0)
{
this.IPAddr.Text = sw.ToString();
}
else
{
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())
{
sb.Append(nodes.Current.Value + "\n");
}
this.IPAddr.Text = sb.ToString();
}
resStream.Close();
sr.Close();
}
}
}
这里引用了一个组件SGML,是对SGML进行解析的
运行结果如图
接着就是做一服务,每个X分钟检测一次ip是否跟以前相同,如ip改变,自动发邮件到我的邮箱,发送邮件函数在我的文档可以找到