单播
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(); } } }