如何通过自己的ADSL使家里的电脑成为服务器呢?像花生壳这样的应用可以帮助你动态解析ip,不过这个程序太庞大了,根本没有必要。
下面介绍我的做法:
条件:
上网方式:ADSL
台式机:linux
中继网页:在某虚拟主机申请一个免费空间,需要支持动态脚本(php、jsp等)
方案:
在本机运行一个Java程序,定时读取本机的外网IP,自动向中继网页用GET方式提交该数据。中继网页保存IP记录在内存中,其他访问者可以通过该页面查看当前的IP。
package
tedeyang;
![None.gif](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.net.InetAddress;
import
java.net.URL;
import
java.net.URLConnection;
import
java.net.UnknownHostException;
import
java.util.Date;
import
java.util.Timer;
import
java.util.TimerTask;
![None.gif](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
class
UpdateIp
{
static String page = " http://xxx.xxx.com/name/link.jsp?ip= " ; // need 'ip='
static String strategy = " ifconfig " ; // or jdk
static long refreshTime = 5L * 60 * 1000 ; // 5 minutes
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* @param args
* @throws IOException
*/
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public static void main( final String[] args) throws IOException
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (args.length <= 1 )
{
System.out.println( " Parameter error ! " );
System.out
.println( " should be followed by three parameters : updatePage ipStrategy refreshMinutes, at least updatePage " );
System.out.println( " updatePage likes 'http://host/xxx.jsp?ip=' " );
System.out.println( " ipStrategy maybe is jdk or ifconfig,default is ifconfig " );
System.out.println( " refreshMinutes 's unit is minute, default is 5 minutes " );
return ;
}
page = args[ 0 ];
if ( ! page.endsWith( " ip= " ))
page += " ?ip= " ; // ?
strategy = args[ 1 ];
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
refreshTime = Integer.parseInt(args[ 2 ]);
refreshTime *= 60 * 1000 ;
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (Exception e)
{
}
// cycle
final Timer t = new Timer();
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
t.schedule( new TimerTask()
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public void run()
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
UpdateIp.run(page, strategy);
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (IOException e)
{
e.printStackTrace();
// t.cancel();
}
} ;
} , 0 , refreshTime);
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
private static void run(String page, String ipStrategy) throws IOException
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
String ip = " no " ;
if (ipStrategy.equals( " jvm " ))
ip = getIpByJavaApi();
else
ip = getIpByLinuxIfconfig();
System.out.print( new Date() + " , this ip is : " + ip);
touchIp(page, ip);
System.out.println( " , touch successed. " );
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
private static void touchIp(String page, String ip) throws IOException
{
URL url = null ;
url = new URL(page + ip);
URLConnection con = null ;
BufferedReader reader = null ;
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
con = url.openConnection();
con.connect();
reader = new BufferedReader( new InputStreamReader(con.getInputStream()));
reader.readLine();
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} finally
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
reader.close();
con = null ;
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (IOException e)
{
e.printStackTrace();
}
}
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
private static String getIpByJavaApi() throws UnknownHostException
{
String ip = " none " ;
InetAddress[] all = InetAddress.getAllByName(InetAddress.getLocalHost()
.getHostName());
// 通过本机主机名,遍历多个ip
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
for ( int i = 0 ; i < all.length; i ++ )
{
String ip2 = all[i].getHostAddress();
if ( ! ip2.startsWith( " 127. " ) && ! ip2.startsWith( " 192. " )
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
&& ! ip2.startsWith( " 10. " ) && ! ip2.startsWith( " 172. " ))
{
ip = ip2;
}
}
return ip;
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
private static String getIpByLinuxIfconfig() throws IOException
{
String ip = " none " ;
Process pre = Runtime.getRuntime().exec(
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
new String[]
{ " sh " , " -c " , " ifconfig ppp0 | grep addr " } );
BufferedReader reader = null ;
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
reader = new BufferedReader( new InputStreamReader(pre
.getInputStream()));
String line = reader.readLine();
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (line != null && line.matches( " .*addr.* " ))
{
int s = line.indexOf( " addr: " ) + 5 ;
int e = line.indexOf( " " , s);
ip = line.substring(s, e).trim();
}
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} finally
{
if (reader != null )
reader.close();
}
return ip;
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
}
<%!
static
String ip
=
"
none
"
;
static
List record
=
new
ArrayList();
%><%
String ipa
=
request.getParameter(
"
ip
"
);
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
if
(record.size()
>
100
)
{
record.clear();
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
if
(ipa
!=
null
)
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (ipa.equals( " ask " ))
{
Iterator it = record.iterator();
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
while (it.hasNext())
{
out.println(it.next() + " </br> " );
}
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
this .ip = ipa;
record.add(ipa + " " + new java.util.Date().getTime());
}
}
out.print(
"
ip=
"
+
ip);
![None.gif](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
%>