UDP如何实现广播:
使用广播地址:255.255.255.255
a. 发送端发送的数据包的目的地写的是广播地址、且指定端口。(255.255.255.255,9999)
b. 本机所在网段的其他主机的程序只要匹配端口成功即就可以收到消息了。 (9999)
1.单发单收
// 研究UDP广播,发送AP的名称和密码。发送端
// 接受方收到ap+密码后,执行连接AP功能。
public class ClientUDP {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ClientUDP++++++++++++++");
// 发送方socket对象。
DatagramSocket socket = new DatagramSocket(); // 发送方自带默认的端口号.
byte[] buffer = "789798发送的内容888".getBytes();
// 参数3:服务端的ip(InetAddress.getLocalHost() ); 服务端的端口
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("127.0.0.1"), 8888);
// 发送数据出去
socket.send(packet);
socket.close();
}
}
/**
* 接收端
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ServerDemo++++++++++++++");
// 1.接收端对象,注册端口。接收端socket对象。
DatagramSocket socket = new DatagramSocket(8888);
// 2.创建一个数据包对象,接收数据。
byte[] buffer = new byte[1024]; //64kb
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// 3. 等待,接收数据
socket.receive(packet);
// 4.取出buffer中的数据
int len = packet.getLength();
String result = new String(buffer, 0, len);
System.out.println("收到数据:" + result);
// 获取发送方的ip+端口
String ip = packet.getSocketAddress().toString();
int port = packet.getPort();
System.out.println("对方的ip和断开:" + ip);
System.out.println("port:" + port);
socket.close(); // 5.关闭通道
}
}
2. 多发多收
// 研究UDP广播,发送AP的名称和密码。发送端
// 接受方收到ap+密码后,执行连接AP功能。
public class ClientUDP {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ClientUDP++++++++++++++");
// 发送方socket对象。
DatagramSocket socket = new DatagramSocket(); // 发送方自带默认的端口号.可自定义为7777
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请说话:");
String msg = sc.nextLine();
if ("exit".equals(msg)) {
System.out.println("离线了");
socket.close();
break;
}
byte[] buffer = msg.getBytes();
// 2.创建1个数据包对象封装数据。参数3:服务端的ip(InetAddress.getLocalHost() ); 服务端的端口
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("127.0.0.1"), 8888);
// 3.发送数据出去
socket.send(packet);
}
}
}
/**
* 接收端
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ServerDemo++++++++++++++");
// 1.接收端对象,注册端口。接收端socket对象。
DatagramSocket socket = new DatagramSocket(8888);
// 2.创建一个数据包对象,接收数据。
byte[] buffer = new byte[1024]; //64kb
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
// 3. 等待,接收数据
socket.receive(packet);
// 4.取出buffer中的数据
int len = packet.getLength();
String result = new String(buffer, 0, len);
System.out.println("收到数据:" + result);
// 获取发送方的ip+端口
System.out.println("对方的ip和端口:" + packet.getAddress() + " 端口:" + packet.getPort());
}
}
}
3. 广播
public class ClientUDP {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ClientUDP++++++++++++++");
// 发送方socket对象。
DatagramSocket socket = new DatagramSocket(); // 发送方自带默认的端口号.可自定义为7777
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请说话:");
String msg = sc.nextLine();
if ("exit".equals(msg)) {
System.out.println("离线了");
socket.close();
break;
}
byte[] buffer = msg.getBytes();
// 2.创建1个数据包对象封装数据。参数3:服务端的ip(InetAddress.getLocalHost() ); 服务端的端口
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("255.255.255.255"), 9999);
// 3.发送数据出去
socket.send(packet);
}
}
}
public class ServerDemo {
public static void main(String[] args) throws Exception {
System.out.println("+++++++++ServerDemo++++++++++++++");
// 1.接收端对象,注册端口。接收端socket对象。
DatagramSocket socket = new DatagramSocket(9999);
// 2.创建一个数据包对象,接收数据。
byte[] buffer = new byte[1024]; //64kb
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
// 3. 等待,接收数据
socket.receive(packet);
// 4.取出buffer中的数据
int len = packet.getLength();
String result = new String(buffer, 0, len);
System.out.println("收到数据:" + result);
// 获取发送方的ip+端口
System.out.println("对方的ip和端口:" + packet.getAddress() + " 端口:" + packet.getPort());
}
}
}










网友评论