个人电脑做服务器(二)

时间:2022-06-18
本文章向大家介绍个人电脑做服务器(二),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

铁通用户获取准确外网ip

  本来通过花生壳可以实现外网访问我电脑上的网站,但是就像上篇文章中所说,在花生壳上需要选择服务器线路,然后铁通的线路只能试用一天,想要再次使用的话,你懂的(囧)

  所以又下载了另外一个动态域名映射工具:易联:

  这个工具最大的优点就是支持自定义的外网ip获取来源,如图:

  这个是我本地的一个jsp网站,我在网站首页写了一段代码通过ip138获取到自己的外网ip,然后输出。这样易联软件就可以获得我外网的ip进行域名映射了。

  Jsp代码如下:

<%@ page import="java.io.*" contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.net.*,java.util.regex.*"%>
<% 
/**
 *   code by the5fire
 *   blog:http://www.the5fire.com
**/
        String htmpath=null; 
        BufferedReader in = null; 
        InputStreamReader isr = null; 
        InputStream is = null; 
        HttpURLConnection huc = null; 
		String ipFilter = "\d+\.\d+\.\d+\.\d+"; 
		String host = "";
        try{ 
          	 URL url = new URL("http://www.ip138.com/ip2city.asp"); //创建 URL 
              huc = (HttpURLConnection)url.openConnection(); 
              is = huc.getInputStream(); 
              isr = new InputStreamReader(is); 
              in = new BufferedReader(isr); 
              String line = null; 
			  Pattern p = Pattern.compile(ipFilter);
              while(((line = in.readLine()) != null)) { 
                Matcher m = p.matcher(line); 
                if (m.find()) { 
                   host = m.group();//IP地址 
                } 
                 if(line.length()==0) 
                   continue; 
               } 
         } 
        catch (Exception e) { 
            System.err.println(e); 
        } 
         finally {  //无论如何都要关闭流 
            try {  is.close(); isr.close();in.close();huc.disconnect();    
            } catch (Exception e) {} 
        } 
%>
<%=host %>

  不过令我比较郁闷的是,目前我自己可以通过外网ip访问,但是别人好像不行,目前原因不明,待深究。哪位大侠要是知道不妨告知一声。