1.播报广播端
package test;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class Weather extends Thread{
String weather = "今晚团建";
int port = 9898;
InetAddress ipAddress = null;
MulticastSocket socket = null;
/**
* 创建广播数据的主机,广播的主机和接收广播的主机必须加入到同一组,
* 地址范围是224.0.0.0~224.255.255.255
*/
Weather(){ //构造方法
try {
ipAddress = InetAddress.getByName("224.255.10.0");
socket = new MulticastSocket(port); //实例化多点广播套接字
socket.setTimeToLive(1); //指定发送范围是本地网络
socket.joinGroup(ipAddress); //加入广播组
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
DatagramPacket packet = null; //声明datagrampacket对象
byte data[] = weather.getBytes(); //将字符串转化为字节数组
packet = new DatagramPacket(data, data.length,ipAddress,port); //将字节数组封装进数据包里
System.out.println(new String(data)); //将字节数组转化为字符串打印在控制台
try {
socket.send(packet); //发送数据包
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main (String[] args) {
Weather w = new Weather(); //创建本类对象
w.start(); //启动线程
}
}
2.接收广播端
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class Receive extends JFrame implements Runnable,ActionListener{ //实现runable,actionlistener接口
int port;
InetAddress groupAddress = null;
MulticastSocket multicastSocket = null;
JButton startButton = new JButton("开始接收");
JButton stopButton = new JButton("结束接收");
JTextArea inceAr = new JTextArea(5,10);
JTextArea inced = new JTextArea(5,10);
Thread thread;
boolean b = false;
/**
* 接收广播端
*/
public Receive () {
super("客户端接收"); //继承,设置窗口标题为客户端接收
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
thread = new Thread(this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
inceAr.setForeground(Color.blue);
inced.setForeground(Color.red); //设置文字的颜色
JPanel jPanel = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane = new JScrollPane(inced); //将文字域添加进滚动面板
jPanel.add(startButton);
jPanel.add(stopButton);
jPanel2.add(jScrollPane,BorderLayout.WEST);
jPanel2.add(inceAr,BorderLayout.EAST);
add(jPanel,BorderLayout.SOUTH);
add(jPanel2,BorderLayout.CENTER);
validate(); //刷新,如果之前设置了大小,会刷新成为设置的大小
port = 9898;
try {
groupAddress = InetAddress.getByName("224.255.10.0");
multicastSocket = new MulticastSocket(port); //绑定多点广播套接字
multicastSocket.joinGroup(groupAddress);
} catch (Exception e) {
e.printStackTrace();
}
setBounds(0,0,400,200); //设置布局
setVisible(true); //将布局设置为显示状态
}
public void run() {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = null;
packet = new DatagramPacket(data, data.length,groupAddress,port); //将字节数组封装进数据包
try {
multicastSocket.receive(packet);
String messageString = new String(packet.getData(),0,packet.getLength()); //将数据包里的数据转化为字符串
inceAr.setText("正在接收的内容是:\n"+messageString); //设置正在接收文字内容域
inced.append(messageString + "\n"); //设置为每条信息为一行
// validate();
System.out.println("asasas");
} catch (Exception e) {
e.printStackTrace();
}
if (b == true) {
break; //当变量为true时退出循环
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startButton.setBackground(Color.red);
stopButton.setBackground(Color.yellow);
if (! (thread.isAlive())) {
thread = new Thread(this);
}
thread.start(); //启动线程
}
if (e.getSource() == stopButton) {
startButton.setBackground(Color.yellow);
stopButton.setBackground(Color.red);
b = true;
}
}
public static void main(String[] args) {
Receive receive = new Receive();
// receive.setSize(400,200);
}
}
3.接收端接收广播数据
<1>广播端开启
<2>接收端开启