java实现UDP广播数据报

要求:

       主机不断地重复播出节目预报,可以保证加入到同一组的主机随时可以接收到广播信息。

Server代码:


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JTextArea;

public class ServerDemo2 extends JFrame implements Runnable,ActionListener{

	//UDP实现广播数据报
	//加入同一个组中的主机随时都可以接收到信息
	private static int port;
	private static InetAddress group = null;
	private JButton button_begin;
	private JButton button_stop;
	private JTextArea textArea_1;
	private JTextArea textArea_2;
	private Thread thread;
	private MulticastSocket socket = null;
	private boolean flag = false;
	public ServerDemo2(){
		setTitle("UDP实现广播数据报");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,50,360,380);
		JPanel panel = new JPanel(new FlowLayout());
		button_begin = new JButton("开始接收");
		panel.add(button_begin);
		button_begin.addActionListener(this);
		button_stop = new JButton("停止接收");
		panel.add(button_stop);
		button_stop.addActionListener(this);
		
		getContentPane().add(panel,BorderLayout.NORTH);
		
		JPanel panel_1 = new JPanel(new GridLayout(1, 2));
		textArea_1 = new JTextArea();
		textArea_1.setLineWrap(true);
		panel_1.add(textArea_1);
		textArea_2 = new JTextArea();
		textArea_2.setLineWrap(true);
		panel_1.add(textArea_2);
		
		getContentPane().add(panel_1,BorderLayout.CENTER);
		thread = new Thread(this);
		validate();//刷新
		port = 9898;
		try {
			group = InetAddress.getByName("224.255.10.0");
			socket = new MulticastSocket(port);
			socket.joinGroup(group);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ServerDemo2 server = new ServerDemo2();
		server.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button_begin){
			if(!thread.isAlive()){
				thread = new Thread(this);
			}
			thread.start();
			flag = false;
		}
		if(e.getSource() == button_stop){
			flag = true;
		}
	}
	@Override
	public void run() {
		while(true){
			byte[] buff = new byte[1024];
			DatagramPacket packet = null;
			packet = new DatagramPacket(buff, 0, buff.length);
			try {
				socket.receive(packet);
				String message = new String(packet.getData(),0,packet.getLength());
				textArea_1.setText("正在接收的内容:\n"+message);
				textArea_2.append(message+"\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(flag){
				break;
			}
		}
	}
}


Client客户端代码:


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class ClientDemo2 extends Thread{

	private String weather = "节目预告:八点钟有大型晚会,请收听.";
	private int port = 9898;
	InetAddress address = null;
	MulticastSocket socket = null;
	public ClientDemo2(){
		try {
			address = InetAddress.getByName("224.255.10.0");
			socket = new MulticastSocket(port);//实例化多点广播套接字
			socket.setTimeToLive(1);//指定发送范围是本地网络
			socket.joinGroup(address);//加入广播组
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void run(){
		while(true){
			DatagramPacket packet = null;
			byte[] buff = weather.getBytes();
			packet = new DatagramPacket(buff, buff.length,address,port);
			System.out.println(new String(buff));
			try {
				socket.send(packet);
				sleep(3000);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		ClientDemo2 client = new ClientDemo2();
		client.start();
	}
}


说明:要广播或接受广播的主机必须加入到一个组内,地址在224.0.0.0-224.255.255.255之间。这类地址并不代表某个特定主机的位置。加入到同一个组的主机可以在某个端口上广播信息, 也可以在某个端口接受信息。

你可能感兴趣的:(java实现UDP广播数据报)