IP:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.loc; import java.net.InetAddress; import java.net.UnknownHostException; /** * IP:定位一个节点:计算机、路由、通讯设备等 * InetAddress: 多个静态方法 * 1、getLocalHost:本机 * 2、getByName:根据域名DNS | IP地址 -->IP * * 两个成员方法 * 1、getHostAddress: 返回地址 * 2、getHostName:返回计算机名 * * */ public class IPTest { public static void main(String[] args) throws UnknownHostException { //使用getLocalHost方法创建InetAddress对象 本机 InetAddress addr = InetAddress.getLocalHost(); System.out.println(addr.getHostAddress()); //返回:192.168.1.110 System.out.println(addr.getHostName()); //输出计算机名 //根据域名得到InetAddress对象 addr = InetAddress.getByName("www.baidu.com"); System.out.println(addr.getHostAddress()); //返回 baidu服务器的ip: System.out.println(addr.getHostName()); //输出:www.shsxt.com //根据ip得到InetAddress对象 addr = InetAddress.getByName("61.135.169.121"); System.out.println(addr.getHostAddress()); //返回 baidu的ip:61.135.169.121 System.out.println(addr.getHostName()); //输出ip而不是域名。如果这个IP地 址不存在或DNS服务器不允许进行IP地址和域名的映射, } }
端口:
查看所有端口: netstat -ano
查看指定端口: netstat -ano|findstr "808"
查看指定进程: tasklist|finder "808"
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.loc; import java.net.InetSocketAddress; /** * 端口 * 1、区分软件 * 2、2个字节 0-65535 UDP TCP * 3、同一个协议端口不能冲突 * 4、定义端口越大越好 * InetSocketAddress * 1、构造器 * new InetSocketAddress(地址|域名,端口); * 2、方法 * getAddress() * getPort() * getHostName() * @author 裴新 QQ:3401997271 * */ public class PortTest { public static void main(String[] args) { //包含端口 InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",9000); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress2.getAddress()); System.out.println(socketAddress2.getPort()); } } /* * 192.168.60.255 admindeMacBook-Pro.local 61.135.169.121 www.baidu.com 61.135.169.121 61.135.169.121 */
URL:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.loc; import java.net.MalformedURLException; import java.net.URL; /** * URL: 统一资源定位器,互联网三大基石之一(html http),区分资源 * 1、协议 * 2、域名、计算机 * 3、端口: 默认80 * 4、请求资源 * http://www.baidu.com:80/index.html?uname=shsxt&age=18#a * * @author * */ public class URLTest01 { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://www.baidu.com:80/index.html?uname=shsxt&age=18#a"); //获取四个值 System.out.println("协议:"+url.getProtocol()); System.out.println("域名|ip:"+url.getHost()); System.out.println("端口:"+url.getPort()); System.out.println("请求资源1:"+url.getFile()); System.out.println("请求资源2:"+url.getPath()); //参数 System.out.println("参数:"+url.getQuery()); //锚点 System.out.println("锚点:"+url.getRef()); } } /** * 协议:http 域名|ip:www.baidu.com 端口:80 请求资源1:/index.html?uname=shsxt&age=18 请求资源2:/index.html 参数:uname=shsxt&age=18 锚点:a * * */
抓取数据:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.loc; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; /** * 网络爬虫的原理 * * * */ public class SpiderTest01 { public static void main(String[] args) throws Exception { //获取URL URL url =new URL("https://www.jd.com"); //URL url =new URL("https://www.dianping.com"); //下载资源 InputStream is = url.openStream(); BufferedReader br =new BufferedReader(new InputStreamReader(is,"UTF-8")); String msg =null; while(null!=(msg=br.readLine())) { System.out.println(msg); } br.close(); //分析 //处理。。。。 } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.loc; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /** * 网络爬虫的原理 +模拟浏览器 * * * */ public class SpiderTest02 { public static void main(String[] args) throws Exception { //获取URL URL url =new URL("https://www.dianping.com"); //下载资源 HttpURLConnection conn =(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); BufferedReader br =new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); String msg =null; while(null!=(msg=br.readLine())) { System.out.println(msg); } br.close(); //分析 //处理。。。。 } }
UDP:
客户端和服务端发送接收数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * 基本流程: 接收端 * Address already in use: Cannot bind 同一个协议下端口不允许冲突 * 1、使用DatagramSocket 指定端口 创建接收端 * 2、准备容器 封装成DatagramPacket 包裹 * 3、阻塞式接收包裹receive(DatagramPacket p) * 4、分析数据 * byte[] getData() * getLength() * 5、释放资源 * * */ public class UdpServer { public static void main(String[] args) throws Exception { System.out.println("接收方启动中....."); // 1、使用DatagramSocket 指定端口 创建接收端 DatagramSocket server =new DatagramSocket(6666); // 2、准备容器 封装成DatagramPacket 包裹 byte[] container =new byte[1024*60]; DatagramPacket packet = new DatagramPacket(container,0,container.length); // 3、阻塞式接收包裹receive(DatagramPacket p) server.receive(packet); //阻塞式 // 4、分析数据 // byte[] getData() // getLength() byte[] datas =packet.getData(); int len = packet.getLength(); System.out.println(new String(datas,0,len)); // 5、释放资源 server.close(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sxt.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; /** * 基本流程: 发送端 * 1、使用DatagramSocket 指定端口 创建发送端 * 2、准备数据 一定转成字节数组 * 3、 封装成DatagramPacket 包裹,需要指定目的地 * 4、发送包裹send(DatagramPacket p) * * 5、释放资源 * * */ public class UdpClient { public static void main(String[] args) throws Exception { System.out.println("发送方启动中....."); // 1、使用DatagramSocket 指定端口 创建发送端 DatagramSocket client =new DatagramSocket(8888); //2、准备数据 一定转成字节数组 String data = "上海尚学堂"; byte[] datas = data.getBytes(); //3、 封装成DatagramPacket 包裹,需要指定目的地 DatagramPacket packet =new DatagramPacket(datas,0,datas.length, new InetSocketAddress("localhost",6666)); //4、发送包裹send(DatagramPacket p) * client.send(packet); // 5、释放资源 client.close(); } }