如何通过自己的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);
%>