Socket网络编程学习笔记 UDP案例-消息接收回送与局域网广播回送
Posted 鮀城小帅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket网络编程学习笔记 UDP案例-消息接收回送与局域网广播回送相关的知识,希望对你有一定的参考价值。
1. 实现UDP接收消息并回送功能
UDPProvider.java -- 消息的接收者
package UDP;
/**
* @ClassName UDPProvider
* @Description TODO
* @Author wushaopei
* @Date 2022/2/16 22:01
* @Version 1.0
*/
public class UDPProvider
public static void main(String[] args) throws IOException
System.out.println("UDPProvider Started.");
// 作为接收者,指定一个端口用于数据接收
DatagramSocket ds = new DatagramSocket(20000);
// 构建接收实体
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息与发送者的信息
// 发送者的 IP 地址
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPProvider receive from ip:" + ip + "\\tport:" + port + "\\tdata:"+data);
// 构建一份回送数据
String responseData = "Receive data with len:" + dataLen;
byte[] responseDataBytes = responseData.getBytes();
// 直接根据发送者构建一份回送信息
DatagramPacket responsePacket = new DatagramPacket(responseDataBytes, responseDataBytes.length,receivePack.getAddress(),receivePack.getPort());
ds.send(responsePacket);
// 完成
System.out.println("UDPProvider Finished.");
ds.close();
UDPSearcher.java -- 消息的发送者
/**
* @ClassName UDPSearcher
* @Description TODO
* @Author wushaopei
* @Date 2022/2/16 22:01
* @Version 1.0
*/
public class UDPSearcher
public static void main(String[] args) throws IOException
System.out.println("UDPSearcher Started.");
// 作为搜索方,不需要指定端口,由系统自动分配端口
DatagramSocket ds = new DatagramSocket();
// 构建一份回送数据
String requestData = "HelloWorld";
byte[] requestDataBytes = requestData.getBytes();
// 直接根据发送者构建一份发送信息
DatagramPacket requestPack = new DatagramPacket(requestDataBytes,requestDataBytes.length);
requestPack.setAddress(InetAddress.getLocalHost());
requestPack.setPort(20000);
// 发送
ds.send(requestPack);
// 构建接收实体
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息与发送者的信息
// 发送者的 IP 地址
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPSearcher receive from ip:" + ip + "\\tport:" + port + "\\tdata:"+data);
// 完成
System.out.println("UDPSearcher Finished.");
ds.close();
UDP消息的交互日志:
2. UDP案例-向局域网内所有设备发送信息
- UDP局域网广播发送的实现
- UDP局域网回送消息的实现
- A作为提供者,B作为接收者
UDP提供者,用于提供服务
/**
* @ClassName UDPProvider
* @Description TODO
* @Author wushaopei
* @Date 2022/2/16 22:01
* @Version 1.0
*/
public class UDPProvider
public static void main(String[] args) throws IOException
// 生成唯一标识
String sn = UUID.randomUUID().toString();
Provider provider = new Provider(sn);
provider.start();
// 读取任意键盘信息后可以退出
System.in.read();
provider.exit();
private static class Provider extends Thread
private final String sn;
private boolean done = false;
private DatagramSocket ds = null;
public Provider(String sn)
super();
this.sn = sn;
@Override
public void run()
super.run();
System.out.println("UDPProvider Started.");
// 作为接收者,指定一个端口用于数据接收
try
// 构建监听,监听20000端口
ds = new DatagramSocket(20000);
while(!done)
// 构建接收实体
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息与发送者的信息
// 发送者的 IP 地址
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPProvider receive from ip:" + ip + "\\tport:" + port + "\\tdata:"+data);
// 解析端口
int responsePort = MessageCreator.parsePort(data);
if(responsePort != -1)
// 构建一份回送数据
String responseData = MessageCreator.buildWithSn(sn);
byte[] responseDataBytes = responseData.getBytes();
// 直接根据发送者构建一份回送信息
DatagramPacket responsePacket = new DatagramPacket(responseDataBytes, responseDataBytes.length,receivePack.getAddress(),responsePort);
ds.send(responsePacket);
catch (SocketException e)
// e.printStackTrace();
catch (IOException e)
// e.printStackTrace();
finally
close();
// 完成
System.out.println("UDPProvider Finished.");
private void close()
if( ds != null)
ds.close();
ds = null;
/**
* 提供结束
*/
void exit()
done = true;
close();
消息创建者,具备两个口令,一个搜索口令,一个回送口令
/**
* @ClassName MessageCreator
* @Description TODO
* @Author wushaopei
* @Date 2022/2/16 22:37
* @Version 1.0
*/
public class MessageCreator
private static final String SN_HEADER = "收到暗号,我是(SN):";
private static final String PORT_HEADER = "这是暗号,请回电端口(Port):";
public static String buildWithPort(int port)
return PORT_HEADER + port;
public static int parsePort(String data)
if(data.startsWith(PORT_HEADER))
return Integer.parseInt(data.substring(PORT_HEADER.length()));
return -1;
public static String buildWithSn(String sn)
return SN_HEADER + sn;
public static String parseSn(String data)
if (data.startsWith(SN_HEADER))
return data.substring(SN_HEADER.length());
return null;
UDP搜索者用于搜索支持方
/**
* @ClassName UDPSearcher
* @Description TODO
* @Author wushaopei
* @Date 2022/2/16 22:01
* @Version 1.0
*/
public class UDPSearcher
private static final int LISTEN_PORT = 30000;
public static void main(String[] args) throws IOException, InterruptedException
System.out.println("UDPSearcher Started.");
Listener listener = listen();
sendBroadcast();
// 读取任意键盘信息后可以退出
System.in.read();
List<Device> devices = listener.getDevicesAndClose();
for(Device device : devices)
System.out.println("Device:" + device.toString());
// 完成
System.out.println("UDPSearcher Finished.");
private static class Device
final int port;
final String ip;
final String sn;
private Device(int port, String ip, String sn)
this.port = port;
this.ip = ip;
this.sn = sn;
@Override
public String toString()
return "Device" + "port=" + port + ",ip="+ip +"\\'" + ",sn=" +sn + "\\'" + '';
private static Listener listen() throws InterruptedException
System.out.println("UDPSearcher start listen.");
CountDownLatch countDownLatch = new CountDownLatch(1);
Listener listener = new Listener(LISTEN_PORT,countDownLatch);
listener.start();
countDownLatch.await();
return listener;
private static class Listener extends Thread
private final int listenPort;
private final CountDownLatch countDownLatch;
private final List<Device> deviceList = new ArrayList();
private boolean done = false;
private DatagramSocket ds = null;
private Listener(int listenPort, CountDownLatch countDownLatch)
this.listenPort = listenPort;
this.countDownLatch = countDownLatch;
@Override
public void run()
super.run();
// 通知已启动
countDownLatch.countDown();
try
// 监听回送端口
ds = new DatagramSocket(listenPort);
while(!done)
// 构建接收实体
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息与发送者的信息
// 发送者的 IP 地址
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPSearcher receive from ip:" + ip + "\\tport:" + port + "\\tdata:"+data);
String sn = MessageCreator.parseSn(data);
if( sn != null )
Device device = new Device(port, ip , sn);
deviceList.add(device);
catch (SocketException e)
// e.printStackTrace();
catch (IOException e)
// e.printStackTrace();
finally
close();
System.out.println("UDPSearcher Listener finished.");
private void close()
if( ds != null)
ds.close();
ds = null;
List<Device> getDevicesAndClose()
done = true;
close();
return deviceList;
private static void sendBroadcast() throws IOException
System.out.println("UDPSearcher sendBroadcast Started.");
// 作为搜索方,不需要指定端口,由系统自动分配端口
DatagramSocket ds = new DatagramSocket();
// 构建一份回送数据
String requestData = MessageCreator.buildWithPort(LISTEN_PORT);
byte[] requestDataBytes = requestData.getBytes();
// 直接根据发送者构建一份发送信息
DatagramPacket requestPack = new DatagramPacket(requestDataBytes,requestDataBytes.length);
// 200000端口,广播地址
requestPack.setAddress(InetAddress.getByName("255.255.255.255"));
requestPack.setPort(20000);
// 发送
ds.send(requestPack);
ds.close();
// 完成
System.out.println("UDPSearcher sendBroadcast Finished.");
运行结果:
首先要打开提供方,再打开搜索方
搜索方启动后send发送消息到提供方
提供方收到并回复。
在搜索方输入任意字符结束搜索和提供方:
以上是关于Socket网络编程学习笔记 UDP案例-消息接收回送与局域网广播回送的主要内容,如果未能解决你的问题,请参考以下文章
Socket网络编程学习笔记 网络编程Socket与TCP/UDP