package udp;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/*
* 接收广播程序:单击“开始接收”按钮,系统开始接收主机播出的信息;单击“停止接收”按钮,
* 系统会停止接收广播主机播出的信息
*
*/
public class Receive extends JFrame implements Runnable,ActionListener{
int port;
//声明InetAddress对象
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 Thread;
//创建Boolean变量
boolean b=false;
public Receive() {//构造方法
//调用父类方法
super("广播数据包");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Thread=new Thread(this);
//绑定按钮ince的单击事件
ince.addActionListener(this);
//绑定按钮stop的单击事件
stop.addActionListener(this);
//指定文本域中文字的颜色
inceAr.setForeground(Color.blue);
//创建JPanel对象
JPanel jp=new JPanel();
//将按钮添加到面板jp上
jp.add(ince);
jp.add(stop);
//将jp放置在窗体上部
add(jp,BorderLayout.NORTH);
//创建面板对象center
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);
}
@Override
public void actionPerformed(ActionEvent e) {//单击事件
//单击按钮ince触发的事件
if(e.getSource()==ince){
//设置按钮颜色
ince.setBackground(Color.red);
stop.setBackground(Color.yellow);
if(!(Thread.isAlive())) {//线程不处于新建状态
Thread=new Thread(this);//实例化Thread对象
}
Thread.start();
b=false;//设置变量值
}
if(e.getSource()==stop) {
//设置按钮颜色
ince.setBackground(Color.yellow);
stop.setBackground(Color.red);
b=true;
}
}
@Override
public void run() {
while(true) {
//创建byte数组
byte data[]=new byte[1024];
//创建DatagramPacket对象
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) {//当变量为true时,退出循环
break;
}
}
}
public static void main(String[] args) {
new Receive().setSize(460, 200);//创建本类对象并设置窗体大小
}
}
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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/*
* 接收广播程序:单击“开始接收”按钮,系统开始接收主机播出的信息;单击“停止接收”按钮,
* 系统会停止接收广播主机播出的信息
*
*/
public class Receive extends JFrame implements Runnable,ActionListener{
int port;
//声明InetAddress对象
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 Thread;
//创建Boolean变量
boolean b=false;
public Receive() {//构造方法
//调用父类方法
super("广播数据包");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Thread=new Thread(this);
//绑定按钮ince的单击事件
ince.addActionListener(this);
//绑定按钮stop的单击事件
stop.addActionListener(this);
//指定文本域中文字的颜色
inceAr.setForeground(Color.blue);
//创建JPanel对象
JPanel jp=new JPanel();
//将按钮添加到面板jp上
jp.add(ince);
jp.add(stop);
//将jp放置在窗体上部
add(jp,BorderLayout.NORTH);
//创建面板对象center
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);
}
@Override
public void actionPerformed(ActionEvent e) {//单击事件
//单击按钮ince触发的事件
if(e.getSource()==ince){
//设置按钮颜色
ince.setBackground(Color.red);
stop.setBackground(Color.yellow);
if(!(Thread.isAlive())) {//线程不处于新建状态
Thread=new Thread(this);//实例化Thread对象
}
Thread.start();
b=false;//设置变量值
}
if(e.getSource()==stop) {
//设置按钮颜色
ince.setBackground(Color.yellow);
stop.setBackground(Color.red);
b=true;
}
}
@Override
public void run() {
while(true) {
//创建byte数组
byte data[]=new byte[1024];
//创建DatagramPacket对象
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) {//当变量为true时,退出循环
break;
}
}
}
public static void main(String[] args) {
new Receive().setSize(460, 200);//创建本类对象并设置窗体大小
}
}
运行结果