如何通過自己的ADSL使家里的電腦成為服務器呢?像花生殼這樣的應用可以幫助你動態解析ip,不過這個程序太龐大了,根本沒有必要。
下面介紹我的做法:
條件:
? 上網方式:ADSL
? 臺式機:linux
? 中繼網頁:在某虛擬主機申請一個免費空間,需要支持動態腳本(php、jsp等)
方案:
??在本機運行一個Java程序,定時讀取本機的外網IP,自動向中繼網頁用GET方式提交該數據。中繼網頁保存IP記錄在內存中,其他訪問者可以通過該頁面查看當前的IP。
package
?tedeyang;

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;


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
????
/**?*/
/**
?????*?
@param
?args
?????*?
@throws
?IOException
?????
*/
????
public
?
static
?
void
?main(
final
?String[]?args)?
throws
?IOException?
{

????????
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
];

????????
try
?
{
????????????refreshTime?
=
?Integer.parseInt(args[
2
]);
????????????refreshTime?
*=
?
60
?
*
?
1000
;

????????}
?
catch
?(Exception?e)?
{
????????}
????????
//
cycle
????????
final
?Timer?t?
=
?
new
?Timer();

????????t.schedule(
new
?TimerTask()?
{

????????????
public
?
void
?run()?
{

????????????????
try
?
{
????????????????????UpdateIp.run(page,?strategy);

????????????????}
?
catch
?(IOException?e)?
{
????????????????????e.printStackTrace();
????????????????????
//
?t.cancel();
????????????????}
????????????}
;
????????}
,?
0
,?refreshTime);
????}
????
private
?
static
?
void
?run(String?page,?String?ipStrategy)?
throws
?IOException?
{

????????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.
"
);
????}
????
private
?
static
?
void
?touchIp(String?page,?String?ip)?
throws
?IOException?
{
????????URL?url?
=
?
null
;
????????url?
=
?
new
?URL(page?
+
?ip);
????????URLConnection?con?
=
?
null
;
????????BufferedReader?reader?
=
?
null
;

????????
try
?
{
????????????con?
=
?url.openConnection();
????????????con.connect();
????????????reader?
=
?
new
?BufferedReader(
new
?InputStreamReader(con.getInputStream()));
????????????reader.readLine();

????????}
?
finally
?
{

????????????
try
?
{
????????????????reader.close();
????????????????con?
=
?
null
;

????????????}
?
catch
?(IOException?e)?
{
????????????????e.printStackTrace();
????????????}
????????}
????}
????
private
?
static
?String?getIpByJavaApi()?
throws
?UnknownHostException?
{
????????String?ip?
=
?
"
none
"
;
????????InetAddress[]?all?
=
?InetAddress.getAllByName(InetAddress.getLocalHost()
????????????????.getHostName());
????????
//
?通過本機主機名,遍歷多個ip
????????
for
?(
int
?i?
=
?
0
;?i?
<
?all.length;?i
++
)?
{
????????????String?ip2?
=
?all[i].getHostAddress();
????????????
if
?(
!
ip2.startsWith(
"
127.
"
)?
&&
?
!
ip2.startsWith(
"
192.
"
)

????????????????????
&&
?
!
ip2.startsWith(
"
10.
"
)?
&&
?
!
ip2.startsWith(
"
172.
"
))?
{
????????????????ip?
=
?ip2;
????????????}
????????}
????????
return
?ip;
????}
????
private
?
static
?String?getIpByLinuxIfconfig()?
throws
?IOException?
{
????????String?ip?
=
?
"
none
"
;
????????Process?pre?
=
?Runtime.getRuntime().exec(

????????????????
new
?String[]?
{?
"
sh
"
,?
"
-c
"
,?
"
ifconfig?ppp0?|?grep?addr
"
?}
);
????????BufferedReader?reader?
=
?
null
;

????????
try
?
{
????????????reader?
=
?
new
?BufferedReader(
new
?InputStreamReader(pre
????????????????????.getInputStream()));
????????????String?line?
=
?reader.readLine();

????????????
if
?(line?
!=
?
null
?
&&
?line.matches(
"
.*addr.*
"
))?
{
????????????????
int
?s?
=
?line.indexOf(
"
addr:
"
)?
+
?
5
;
????????????????
int
?e?
=
?line.indexOf(
"
?
"
,?s);
????????????????ip?
=
?line.substring(s,?e).trim();
????????????}
????????}
?
finally
?
{
????????????
if
?(reader?
!=
?
null
)
????????????????reader.close();
????????}
????????
return
?ip;
????}
}
?
<%!
static
?String?ip?
=
?
"
none
"
;
static
?List?record?
=
?
new
?ArrayList();
%><%
String?ipa
=
?request.getParameter(
"
ip
"
);

if
(record.size()
>
100
)
{
?record.clear();
}
if
(ipa?
!=
?
null
)
{

?
if
(ipa.equals(
"
ask
"
))
{
?Iterator?it?
=
?record.iterator();

while
(it.hasNext())
{
out.println(it.next()
+
"
</br>
"
);
}
}
else
{
?
this
.ip?
=
?ipa;
?record.add(ipa
+
"
?
"
+
new
?java.util.Date().getTime());
}
}
out.print(
"
ip=
"
+
ip);

%>