import java.net.DatagramPacket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.security.acl.Group;
import javax.swing.plaf.multi.MultiButtonUI;
public class Weather extends Thread{
String weather="天气预报,今天下雨";
int port=9897;
InetAddress iaddress=null; //创建inetAddress地址对象;
MulticastSocket socket=null; //创建多点广播套接字;
Weather(){ //构造函数
try {
iaddress=InetAddress.getByName("224.255.10.0"); //指定广播的地址
socket=new MulticastSocket(port); //套接字端口设置
socket.setTimeToLive(1); //指定发送范围为本地网络
socket.joinGroup(iaddress); //将地址加入广播组
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(){ //重写线程的run
while(true){
DatagramPacket packet=null; //数据包
byte data[]=weather.getBytes(); //声明字节数组
packet=new DatagramPacket(data, data.length,iaddress,port);
System.out.println(new String(data));
try {
socket.send(packet);
sleep(3000);
} catch (Exception e) {
// TODO: handle exception
//e.printStackTrace();
}
}
}
public static void main(String[] args) {
Weather w=new Weather();
w.start();
}
}
主机代码:
单独运行截图:
客户端代码:
import java.awt.BorderLayout;
import java.awt.Color;
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.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.plaf.multi.MultiButtonUI;
public class Receive extends JFrame implements Runnable,ActionListener{
int port; //端口
InetAddress group=null; //声明inetDress 地址
MulticastSocket socket=null; //多点广播套用字
JButton ince=new JButton("开始接收"); //创建按钮对象
JButton stop=new JButton("停止接收"); //me too
JTextArea inceAr=new JTextArea(10,10); //显示接受广播的文本域
JTextArea inced=new JTextArea(10,10);
Thread thread; //线程
boolean b=false; //创建boolean型变量
public Receive(){ //构造
super("广播数据报");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//以下为窗体布局设计
thread=new Thread(this);
ince.addActionListener(this);
stop.addActionListener(this);
inceAr.setForeground(Color.blue);
JPanel northJPanel=new JPanel();
northJPanel.add(ince);
northJPanel.add(stop);
add(northJPanel,BorderLayout.NORTH);
JPanel centerJPanel=new JPanel();
centerJPanel.setLayout(new GridLayout(1,2));
centerJPanel.add(inceAr);
centerJPanel.add(inced);
add(centerJPanel,BorderLayout.CENTER);
validate();
port=9897;
try {
group=InetAddress.getByName("224.255.10.0");
socket=new MulticastSocket(port);
socket.joinGroup(group);
} catch (Exception e) {
}
setBounds(100,50,360,380);
setVisible(true);
}
public void run(){ //重写线程run函数
while(true){
byte data[]=new byte[1024]; //数据接收的大小
DatagramPacket packet=null; //一个数据包对象
packet=new DatagramPacket(data,data.length,group,port); //连接
try {
socket.receive(packet); //接受数据包
String message=new String(packet.getData(),0,packet.getLength());//数据下载成String格式
inceAr.setText("正在接收的内容\n"+message);
inced.append(message+'\n');
} catch (Exception e) {
}
if(b==true){
break;
}
}
}
public void actionPerformed(ActionEvent e){ //对于窗口内的各个单击事件的处理
if(e.getSource()==ince){
ince.setBackground(Color.red);
stop.setBackground(Color.yellow);
if(!(thread.isAlive())){
thread=new Thread(this);
}
thread.start();
b=false;
}
if(e.getSource()==stop){
ince.setBackground(Color.yellow);
stop.setBackground(Color.red);
b=true;
}
}
public static void main(String[] args) { //主函数创建实例
Receive receive=new Receive();
receive.setSize(480,200);
}
}
主机运行时客户端运行效果截图:
如果对于本文有疑问请回复或者加头像wx。