JAVA基于UDP的多点广播数据报技术的一个实现例子

本文介绍了一个 Java 实现的 UDP 多播程序,包括发送端 Weather 类和接收端 Receive 类。发送端定时向指定多播地址发送消息,接收端通过 GUI 显示接收到的数据。此程序展示了多播技术的应用及关键实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This is a UDP network program. The following presents a multicast datagram program, which is actually a new technology.

package com.han; import java.net.*; /** * This is a UDP network program. * The following presents a multicast datagram program, * which is actually a new technology. * * @author HAN * */ public class Weather extends Thread{ String weather ="节目预告: 八点有大型晚会,请收听"; int port =9898; InetAddress iaddress;//没法初始化,这里只能声明。因为初始化new对象时要抛出异常所以在成员变量区域是语法通不过的。 MulticastSocket socket; //在构造方法中初始化成员变量 Weather(){ try { //A multicast group is specified by a class D IP address //and by a standard UDP port number. //Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. //The address 224.0.0.0 is reserved and should not be used. iaddress=InetAddress.getByName("233.0.0.0"); socket=new MulticastSocket(port); socket.setTimeToLive(1); socket.joinGroup(iaddress); }catch(Exception e){ e.printStackTrace(); } } @Override //最简单的方法也就是建立一个线程来运行 public void run(){ while(true){ byte[] data=weather.getBytes(); DatagramPacket packet=new DatagramPacket(data,data.length,iaddress,port); // System.out.println(weather); System.out.println(new String(data)); try { socket.send(packet); sleep(3000);//线程休眠3秒 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args){ Weather w=new Weather(); w.start(); } }
This is the receive part.

package com.han; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.*; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * This is the receive part. * @author HAN * */ public class Receive extends JFrame implements Runnable, ActionListener{ private static final long serialVersionUID = 3362377947503474102L; int port=9898; InetAddress group; MulticastSocket socket; //socket sends and receives the packet. DatagramPacket packet; JButton ince=new JButton("开始接收"); JButton stop=new JButton("停止接收"); JTextArea inceAr=new JTextArea(10,20); JTextArea inced=new JTextArea(10,20); Thread thread; boolean b = false; byte[] buf=new byte[30];// If the message is longer than the packet's length, the message is truncated. //在构造方法中设置具体参数特性,也就是初始化 public Receive(){ super("广播数据报"); thread=new Thread(this); ince.addActionListener(this); stop.addActionListener(this); inceAr.setForeground(Color.blue); JPanel north=new JPanel(); north.add(ince); north.add(stop); add(north,BorderLayout.NORTH); JPanel center=new JPanel(); JScrollPane sp=new JScrollPane(inceAr); JScrollPane sp2=new JScrollPane(inced); inceAr.setLineWrap(true); inceAr.setEditable(false); inced.setLineWrap(true); inced.setEditable(false); center.add(sp); center.add(sp2); add(center,BorderLayout.CENTER); pack(); //JFrame inherited from Window validate(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); try { socket=new MulticastSocket(port); //A multicast group is specified by a class D IP address //and by a standard UDP port number. //Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. //The address 224.0.0.0 is reserved and should not be used. group=InetAddress.getByName("233.0.0.0"); socket.joinGroup(group); packet=new DatagramPacket(buf,buf.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run(){ while(true){ try { socket.receive(packet); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // String message=new String(buf); String message=new String(packet.getData(),0,packet.getLength());//very important !! // System.out.println(message.length()); inceAr.setText("正在接受的内容: \n"+message); inced.append(message+"\n"); if(b==true){ break; } } } public void actionPerformed(ActionEvent e){ Object object=e.getSource(); if(object==ince){ //If this thread is already on the state "run", do nothing. if(!thread.isAlive()){ thread=new Thread(this); thread.start(); ince.setBackground(Color.red); stop.setBackground(Color.yellow); b=false; } } if(object==stop){ ince.setBackground(Color.yellow); stop.setBackground(Color.red); b=true; } } public static void main(String[] args){ @SuppressWarnings("unused") Receive rec=new Receive(); // rec.setSize(460, 200);//提供了额外设置窗体大小的方式 } }
这上面的程序运用了很多JAVA的核心技术以及很多需要注意的地方,可以堪称一个JAVA的精髓例子,记录下来以便以后查阅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值