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();
}
}
}
本文来自博客园,作者:HumorChen99,转载请注明原文链接:https://www.cnblogs.com/HumorChen/p/18039599
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律