七、UDP
- 发短信:不用连接,需要知道对方的地址
1.发送消息
发送方
package com.wang.Web.Demo03; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; // 不需要连接服务器 public class UDPClientDemo01 { public static void main(String[] args) throws Exception{ // 1.建立一个Socket DatagramSocket socket = new DatagramSocket(); // 2.建个包 String msg = "服务器,你好!"; InetAddress localhost = InetAddress.getByName("localhost"); int port = 9090; // 数据,数据的长度起始,要发送给谁 DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0,msg.getBytes().length,localhost,port); // 3.发送包 socket.send(packet); // 4.关闭流 socket.close(); } }
接收方
package com.wang.Web.Demo03; import java.net.DatagramPacket; import java.net.DatagramSocket; // 还是需要客户端的链接 public class UDPServerDemo01 { public static void main(String[] args) throws Exception{ // 1.开放端口 DatagramSocket socket = new DatagramSocket(9090); // 2.接收数据包 byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); // 阻塞接受 System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData())); // 3.关闭连接 socket.close(); } }
2.循环发送消息
发送方
package com.wang.Web.Demo03; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class UDPSenderDemo01 { public static void main(String[] args) throws Exception{ DatagramSocket socket = new DatagramSocket(8888); // 准备数据:控制台读取 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while(true) { String data = reader.readLine(); byte[] buffer = data.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length, new InetSocketAddress("localhost", 9090)); // 发送包 socket.send(packet); if (data.equals("bye")) { break; } } // 关闭socket socket.close(); } }
接收方
package com.wang.Web.Demo03; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo01 { public static void main(String[] args) throws Exception{ // 开放端口 DatagramSocket socket = new DatagramSocket(6666); while (true) { // 准备接收包裹 byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container, 0, container.length); socket.receive(packet); byte[] data = packet.getData(); String receiveData = new String(data); System.out.println(receiveData); if (receiveData.equals("bye")) { break; } } // 关闭socket socket.close(); } }
3.在线咨询
- 情形描述: 两个人都可以是发送方,也可以是接收方(配合多线程)
发信端类
package com.wang.Web.Demo04; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class TalkSend implements Runnable { DatagramSocket socket = null; BufferedReader reader = null; private int formPort; private String hostname; private int toPort; public TalkSend(int formPort, String hostname, int toPort) { this.formPort = formPort; this.hostname = hostname; this.toPort = toPort; try { socket = new DatagramSocket(formPort); reader = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { String s = reader.readLine(); byte[] buffer = s.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length, new InetSocketAddress(hostname, toPort)); // 发送包 socket.send(packet); if (s.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } } // 关闭socket socket.close(); } }
接收端类
package com.wang.Web.Demo04; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class TalkReceive implements Runnable{ DatagramSocket socket = null; private int port; public TalkReceive(int port) { this.port = port; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { //开放端口 // 接收包 while (true){ try { byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container,0,container.length); // 接收 socket.receive(packet); byte[] data = packet.getData(); String receiveData = new String(data); System.out.println(Thread.currentThread().getName() + ":" + receiveData); if (receiveData.equals("bye")){ break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
教师端
package com.wang.Web.Demo04; public class StudentClient { public static void main(String[] args) { new Thread(new TalkSend(7777,"localhost",8900)).start(); new Thread(new TalkReceive(9999),"老师").start(); } }
学生端
package com.wang.Web.Demo04; public class TeacherClient { public static void main(String[] args) { new Thread(new TalkSend(5555,"localhost",9999)).start(); new Thread(new TalkReceive(8900),"学生").start(); } }
八、URL下载网络资源
1.URL介绍
统一资源定位符(URL):定位互联网上的某一个资源,如https://www.baidu.com
DNS域名解析将URL如www.baidu.com 解析为具体的IP地址 xxx.xxx.xxxx.xxx…xxx,计算机是通过IP地址定位具体的主机的
URL的格式:“ 协议:// ip地址:端口号/项目名/资源 ”
Java中使用URL示例:
package com.wang.Web.Demo05; import java.net.MalformedURLException; import java.net.URL; public class URLDemo01 { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://localhost:8080/helloworld/insex" + ".jsp?username=subeily&password=123"); System.out.println(url.getProtocol()); // 协议 System.out.println(url.getHost()); // 主机IP System.out.println(url.getPort()); // 端口 System.out.println(url.getPath()); // 路径 System.out.println(url.getFile()); // 文件名 System.out.println(url.getQuery()); // 参数 } }
2.使用URL下载网络资源
代码示例:
package com.wang.Web.Demo05; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class URLDemo02 { public static void main(String[] args) throws Exception{ // 1.下载网络资源的地址 URL url = new URL("http://localhost:8080/subei/hello.html"); // 2.连接到这个资源HTTP HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream is = urlConnection.getInputStream(); FileOutputStream stream = new FileOutputStream(new File("same.txt")); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1){ stream.write(bytes,0,len); } stream.close(); is.close(); urlConnection.disconnect(); } }
后记
Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~