URL----网络资源定位指针
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; import org.junit.Test; public class UrlDemo { @Test//URL public void t1(){ try { URL url=new URL("http://www.hncu.net"); InputStream in=url.openStream(); BufferedReader bf=new BufferedReader(new InputStreamReader(in)); String str=""; while((str=bf.readLine())!=null){ System.out.println(str); } bf.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test//URLConnection ---响应头信息 public void t2(){ try { URL url=new URL("http://www.hncu.net"); URLConnection con=url.openConnection(); String code=con.getContentEncoding(); System.out.println("code:"+code); int len=con.getContentLength(); System.out.println("len:"+len); Date d=new Date(con.getLastModified()); System.out.println(d.toString()); // con.setDoOutput(true); // OutputStream out = con.getOutputStream(); // for(int i=0;i<10000;i++){ // out.write(i); // } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test//IP地址的封装类:InetAddress public void t3() throws IOException{ InetAddress ip=InetAddress.getByName("www.hncu.net"); System.out.println(ip.getHostAddress()); System.out.println(ip.getHostName()); } }
package cn.hncu.udp.hello; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UdpSendDemo { /* * UDP编程就两个类:DatagramPacket,DatagramSocket 发送: 创建一个数据包 DatagramPacket dp = * new DatagramPacket(数据+IP+端口); * * 创建一个UDP的socket //可以指定端口,也可由系统随机分配 DatagramSocket s = new * DatagramSocket(); * * s.send(dp);//发送出去 ※※※入口※※※ * * 数据包DatagramPacket类的构造方法分成两类: 1)有地址参数(通常是Ip地址)的就是用于发送的 2)没地址参数的就是用于接收的 */ public static void main(String[] args) throws IOException { byte buf[] = "hello,world!".getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10001); DatagramSocket s = new DatagramSocket();//发送时可以不指定端口号 s.send(dp);// 入口 s.close(); } }
package cn.hncu.udp.hello; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpReceiveDemo { /* * UDP编程就两个类:DatagramPacket,DatagramSocket * 接收: * 创建一个数据包 * DatagramPacket dp = new DatagramPacket(缓存); * * 创建一个UDP的socket //必须指定发送方数据包中给的端口号 * DatagramSocket ds = new DatagramSocket(发送方数据包中指定的端口号); * * ds.receive(dp);//数据收到dp中 //※※※※ * 从dp中把真正的数据解析出来 //※※※※ */ public static void main(String[] args) throws IOException { byte buf[]=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); DatagramSocket ds=new DatagramSocket(10001); ds.receive(dp); //从dp中解析数据 String ip=dp.getAddress().getHostAddress(); int port=dp.getPort(); byte data[]=dp.getData(); String text=new String(data,0,dp.getLength()); System.out.println(ip+":"+port+"--->"+text); ds.close(); } }
package cn.hncu.udp.chat.v1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPChat1 { public static void main(String[] args) { DatagramSocket sendDs = null; DatagramSocket receiveDs = null; try { sendDs = new DatagramSocket(10002); receiveDs = new DatagramSocket(10003); new Thread(new Send(sendDs)).start();// 发 new Thread(new Receive(receiveDs)).start();// 收 } catch (SocketException e) { e.printStackTrace(); } } } class Send implements Runnable { private DatagramSocket ds; public Send(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String str = ""; while ((str = br.readLine()) != null) { byte buf[] = str.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10001); ds.send(dp); } ds.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Receive implements Runnable { private DatagramSocket ds; public Receive(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { byte buf[] = new byte[1024]; while (true) { DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); // 解析数据 String ip = dp.getAddress().getHostAddress(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":" + text); if ("over".equals(text)) { break; } } System.out.println("离线了....."); ds.close(); } catch (IOException e) { e.printStackTrace(); } } }
package cn.hncu.udp.chat.v2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPChat2 { public static void main(String[] args) { try { DatagramSocket sendDs = new DatagramSocket(10000); DatagramSocket receiveDs = new DatagramSocket(10001); new Thread(new Send(sendDs)).start();// 发 new Thread(new Receive(receiveDs)).start();// 收 } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Send implements Runnable { private DatagramSocket ds; public Send(DatagramSocket ds) { this.ds = ds; } @Override public void run() { // System.in字节流 --> 转换流 --> 字符流 --> 缓存流 try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String str = ""; while ((str = br.readLine()) != null) { byte buf[] = str.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10003); ds.send(dp); } ds.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Receive implements Runnable { private DatagramSocket ds; public Receive(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { while (true) { byte buf[] = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); // 从dp中解析数据 String ip = dp.getAddress().getHostAddress(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":" + text); if ("over".equals(text)) { break; } } System.out.println("离线了。。。。"); ds.close(); } catch (IOException e) { e.printStackTrace(); } } }