UDT接收发送广播

public class Weather extends Thread{
String weather="节目预报:八点有大型晚会,请收听";
int port=9898;//端口号
InetAddress iaddress=null;
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(){
	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);//发送数据
		Thread.sleep(3000);
	}catch(Exception e){
		e.printStackTrace();
	}
	}
}
	 
	public static void main(String[] args) {
		 Weather w=new Weather();
		 w.start();
	}

}

public class Receive extends JFrame implements Runnable,ActionListener{
	int port;
	InetAddress group=null;
	MulticastSocket socket=null;//创建多点广播套接字
	JButton ince=new JButton("开始接收");
	JButton stop=new JButton("停止接收");
	JTextArea inceAr=new JTextArea(10,10);
	JTextArea inced=new JTextArea(10,10);
	Thread thread;
	boolean b=false;
	public Receive(){//构造方法
		super("广播数据包");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		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();//创建中间的面板
		center.setLayout(new GridLayout(1,2));
		center.add(inceAr);
		center.add(inced);
		add(center, BorderLayout.CENTER);
		validate();//刷新
		port=9898;//端口号
		try{
			group=InetAddress.getByName("224.255.10.0");
			socket=new MulticastSocket(port);//绑定多点广播她接着
			socket.joinGroup(group);//加入广播组
		}catch(Exception e){
			e.printStackTrace();
		}
		setBounds(100,50,360,380);
		setVisible(true);
	}
	public void 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());//获取数据包中内容
				inceAr.setText("正在接受的内容:\n"+message);;//将接受内容显示 在文本域
				inced.append(message+"\n");//每条信息为一行
				
			}catch(Exception e){
				e.printStackTrace();
			}
			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 rec=new Receive();
		rec.setSize(460,200);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值