最简单的网络编程代码,ip、url、UDP

  1. package com.cust.util;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.Writer;
  7. import java.net.DatagramSocket;
  8. import java.net.InetAddress;
  9. import java.net.MalformedURLException;
  10. import java.net.SocketException;
  11. import java.net.URL;
  12. import java.net.URLConnection;
  13. import java.net.UnknownHostException;
  14. /**
  15.  * java net demo
  16.  * @author lee
  17.  *
  18.  */
  19. public class IpTest {
  20.     
  21.     String url = "http://www.163.com";//读取的网址
  22.     String path = "D:/code.html";//网页代码保存到本地路径、名称
  23.     /**
  24.      * 读取本机ip信息
  25.      */
  26.     public void ipconfig() {
  27.         try {
  28.             InetAddress inet = InetAddress.getLocalHost();
  29.             System.out.println("ip地址:"+inet.getHostAddress()+"/"+"名称" + inet.getHostName());
  30.         } catch (UnknownHostException e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.     /**
  35.      * 读取网页源码,保存到本地
  36.      */
  37.     public void UrlTest() {
  38.         try {
  39.             System.out.println("starting.....");
  40.             int c;
  41.             URL u = new URL(url);//获取地址
  42.             URLConnection urlc = u.openConnection();//建立连接
  43.             InputStreamReader ist = new InputStreamReader(
  44.                     urlc.getInputStream(), "GB2312");//获得流
  45.             FileOutputStream fw = new FileOutputStream(path);//输出文件流
  46.             Writer w = new OutputStreamWriter(fw, "GB2312");
  47.             System.out.println(urlc.getContentEncoding());
  48.             System.out.println(urlc.getContentType());
  49.             while ((c = ist.read()) != -1) {
  50.                 w.write((char) c);//保存到本地
  51.                 System.out.print((char) c);//控制台输出
  52.             }
  53.             //关闭流
  54.             fw.close();
  55.             ist.close();
  56.                 
  57.         } catch (MalformedURLException e) {
  58.             e.printStackTrace();
  59.         } catch (IOException e) {
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.    
  64.     /**
  65.      * UDP,通过DatagramSocket的异常查看被占用的端口
  66.      */
  67.     public void UDPTest() {
  68.         System.out.println("查看中......");
  69.         for (int port = 0; port <= 65535; port++) {
  70.             try {
  71.                 DatagramSocket ds = new DatagramSocket(port);
  72.                 // System.out.println("端口" + port + "未被占用");
  73.                 ds.close();
  74.             } catch (SocketException e) {
  75.                 System.out.println("端口" + port + "被占用");
  76.             }
  77.         }
  78.     }
  79.     /**
  80.      * @param args
  81.      */
  82.     public static void main(String[] args) {
  83.         IpTest ip = new IpTest();
  84.         ip.ipconfig();
  85.         ip.UrlTest();
  86.         ip.UDPTest();
  87.     }
  88. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值