Java Udp协议三种通讯方式:单播、组播、广播示范代码
2021/6/22 14:56:43
本文主要是介绍Java Udp协议三种通讯方式:单播、组播、广播示范代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
单播
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * Udp 单播方式示范 */ public class UdpDemo { public static void main(String[] args) { new Thread(()->{ UdpDemoServer server = null; try { server = new UdpDemoServer("127.0.0.1",5001); System.out.println("服务器启动成功,接收数据..."); byte[] data = server.receive(1024); System.out.println("服务器收到数据:"); System.out.println(new String(data)); }catch (Exception e){ e.printStackTrace(); }finally { if (server != null){ server.close(); } } }).start(); new Thread(()->{ UdpDemoClient client = null; try { Thread.sleep(1500); client = new UdpDemoClient("127.0.0.1",5001); System.out.println("客户端启动成功"); client.send("你好,我是客户端"); System.out.println("客户成功发送数据"); }catch (Exception e){ e.printStackTrace(); }finally { if (client != null){ client.close(); } } }).start(); } } class UdpDemoServer{ private DatagramSocket socket; private InetAddress inetAddress; private int port; public UdpDemoServer(String ip,int port) throws IOException { this.inetAddress = InetAddress.getByName(ip); this.port = port; this.socket = new DatagramSocket(port); } public byte[] receive(int length)throws IOException{ DatagramPacket packet = new DatagramPacket(new byte[length],length); this.socket.receive(packet); return packet.getData(); } public void close(){ if (this.socket != null && !this.socket.isClosed()){ this.socket.close(); } } } class UdpDemoClient{ private DatagramSocket socket; private InetAddress inetAddress; private int port; public UdpDemoClient(String ip,int port) throws IOException { this.inetAddress = InetAddress.getByName(ip); this.port = port; this.socket = new DatagramSocket(); } public void send(String msg)throws IOException{ byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data,data.length,this.inetAddress,this.port); this.socket.send(packet); } public void close(){ if (this.socket != null && !this.socket.isClosed()){ this.socket.close(); } } }
组播
224.0.0.0~224.0.0.255为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其它地址供路由协议使用;
224.0.1.0~224.0.1.255是公用组播地址,可以用于Internet;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; /** * Udp 组播方式示范 */ public class UdpMulticastDemo { public static void main(String[] args) { new Thread(()->{ UdpMulticastServer server = null; try { server = new UdpMulticastServer("224.0.1.0",5000); System.out.println("服务器启动成功,接收数据..."); byte[] data = server.receive(1024); System.out.println("服务器收到数据:"); System.out.println(new String(data)); }catch (Exception e){ e.printStackTrace(); }finally { if (server != null){ server.close(); } } }).start(); new Thread(()->{ UdpMulticastClient client = null; try { Thread.sleep(1500); client = new UdpMulticastClient("224.0.1.0",5000); System.out.println("客户端启动成功"); client.send("你好,我是客户端"); System.out.println("客户成功发送数据"); }catch (Exception e){ e.printStackTrace(); }finally { if (client != null){ client.close(); } } }).start(); } } class UdpMulticastServer { private MulticastSocket multicastSocket; private InetAddress inetAddress; private int port; public UdpMulticastServer(String address, int port) throws IOException { inetAddress = InetAddress.getByName(address); this.port = port; multicastSocket = new MulticastSocket(port); multicastSocket.joinGroup(this.inetAddress); } public byte[] receive(int length) throws IOException { DatagramPacket packet = new DatagramPacket(new byte[length],length); this.multicastSocket.receive(packet); return packet.getData(); } public void close(){ if (this.multicastSocket != null && !this.multicastSocket.isClosed()){ this.multicastSocket.close(); } } } class UdpMulticastClient { private DatagramSocket socket; private InetAddress inetAddress; private int port; public UdpMulticastClient(String address, int port) throws IOException { inetAddress = InetAddress.getByName(address); this.port = port; socket = new MulticastSocket(); } public void send(String msg) throws IOException { byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data,data.length,this.inetAddress,port); this.socket.send(packet); } public void close(){ if (this.socket != null && !this.socket.isClosed()){ this.socket.close(); } } }
广播
广播地址 255.255.255.255
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * Udp 广播方式示范 */ public class UdpBroadcastDemo { public static void main(String[] args) { new Thread(()->{ UdpBroadcastDemoServer server = null; try { server = new UdpBroadcastDemoServer("255.255.255.255",5001); System.out.println("服务器1启动成功,接收数据..."); byte[] data = server.receive(1024); System.out.println("服务器1收到数据:"); System.out.println(new String(data)); }catch (Exception e){ e.printStackTrace(); }finally { if (server != null){ server.close(); } } }).start(); new Thread(()->{ UdpBroadcastDemoClient client = null; try { Thread.sleep(1500); client = new UdpBroadcastDemoClient("255.255.255.255",5001); System.out.println("客户端启动成功"); client.send("你好,我是客户端"); System.out.println("客户成功发送数据"); }catch (Exception e){ e.printStackTrace(); }finally { if (client != null){ client.close(); } } }).start(); } } class UdpBroadcastDemoServer{ private DatagramSocket socket; private InetAddress inetAddress; private int port; public UdpBroadcastDemoServer(String ip,int port) throws IOException { this.inetAddress = InetAddress.getByName(ip); this.port = port; this.socket = new DatagramSocket(port); } public byte[] receive(int length)throws IOException{ DatagramPacket packet = new DatagramPacket(new byte[length],length); this.socket.receive(packet); return packet.getData(); } public void close(){ if (this.socket != null && !this.socket.isClosed()){ this.socket.close(); } } } class UdpBroadcastDemoClient{ private DatagramSocket socket; private InetAddress inetAddress; private int port; public UdpBroadcastDemoClient(String ip,int port) throws IOException { this.inetAddress = InetAddress.getByName(ip); this.port = port; this.socket = new DatagramSocket(); } public void send(String msg)throws IOException{ byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data,data.length,this.inetAddress,this.port); this.socket.send(packet); } public void close(){ if (this.socket != null && !this.socket.isClosed()){ this.socket.close(); } } }
这篇关于Java Udp协议三种通讯方式:单播、组播、广播示范代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求