初学JAVA,在做一个UDP的练习
然后老是阻塞在socket.recieve(packet)这句,我检查来检查去看不出毛病,后来想想,要不是端口问题?
我就把宽带连接给断了,没想到就可以了,连接上后又不行了。
这应该是InetAddress或者port的关系吧,具体不是很明白,望高手指教。具体代码如下广播主机程序:
import java.net.*;public class Herald extends Thread {
private String herald = "节目预报:八点有大型晚会,请收听";
private int port = 9898;
private InetAddress address = null;
private DatagramPacket packet = null;
private MulticastSocket socket = null; public Herald() {
try {
socket = new MulticastSocket(port);
address = InetAddress.getByName("224.255.10.0");
socket.joinGroup(address);
socket.setTimeToLive(1);
} catch (Exception err) {
err.printStackTrace();
}
} public void run() {
byte[] heraldData = herald.getBytes();
packet = new DatagramPacket(heraldData, heraldData.length, address,
port);
while (true) {
try {
socket.send(packet);
System.out.println(new String(heraldData));
Thread.sleep(3000);
} catch (Exception err) {
err.printStackTrace();
}
}
} public static void main(String[] args) {
Herald myHerald = new Herald();
myHerald.start();
}
}接收广播程序:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Receive extends JFrame {
private static final long serialVersionUID = 20101022L;
private JPanel north = null;
private JPanel center = null;
private JButton start = null;
private JButton stop = null;
private JTextArea now = null;
private JTextArea all = null;
private MulticastSocket socket = null;
private InetAddress address = null;
private DatagramPacket packet = null;
private Thread thread = null;
private int port = 9898;
private boolean flag = false; public Receive() {
super("广播数据报");
init();
validate();
} //初始化窗口
private void init() {
Container container = this.getContentPane();
this.setBounds(100, 50, 460, 200);
this.setVisible(true);
container.add(getNorth(), BorderLayout.NORTH);
container.add(getCenter(), BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} //JPanel,放置两个按钮
private JPanel getNorth() {
if (north == null) {
north = new JPanel(new FlowLayout());
north.add(getStart());
north.add(getStop());
}
return north;
} //JPanel,放置两个JTextArea
private JPanel getCenter() {
if (center == null) {
center = new JPanel(new GridLayout(1, 2));
center.add(getNow());
center.add(getAll());
}
return center;
} //开始按钮,按下此按钮运行接收线程,接收广播包
private JButton getStart() {
if (start == null) {
start = new JButton("开始接受");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg) {
start.setBackground(Color.RED);
stop.setBackground(Color.YELLOW);
receiveThread();
if (!thread.isAlive()) {
flag = false;
thread.start();
}
}
});
}
return start;
} //停止按钮,按下此按钮停止接收广播包
private JButton getStop() {
if (stop == null) {
stop = new JButton("停止接收");
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg) {
start.setBackground(Color.YELLOW);
stop.setBackground(Color.RED);
flag = true;
}
});
}
return stop;
} //显示正在接收的packet
private JTextArea getNow() {
if (now == null) {
now = new JTextArea(10, 10);
now.setForeground(Color.BLUE);
}
return now;
} //显示所有packet
private JTextArea getAll() {
if (all == null) {
all = new JTextArea(10, 10);
}
return all;
} //接收广播包线程
private void receiveThread() {
thread = new Thread(new Runnable() {
public void run() {
try {
socket = new MulticastSocket(port);
address = InetAddress.getByName("224.255.10.0");
socket.joinGroup(address);
byte[] receive = new byte[1024];
packet = new DatagramPacket(receive, receive.length,
address, port);
while (true) {
//如果宽带连接上后,老是在这句阻塞
socket.receive(packet);
String message = new String(packet.getData());
now.setText("正在接收的内容:\n" + message);
all.append(message + "\n");
if (flag) {
break;
}
}
} catch (Exception err) {
err.printStackTrace();
}
}
});
} public static void main(String[] args) {
new Receive();
}
}

解决方案 »

  1.   

    广播和组播都是需要路由支持的,你宽带连接连上的时候你的UDP报文发到你的网关去了,对广播和组播,如果没有在路由做设置的话,默认是丢弃的,断开以后就在本地广播,所以你就可以收到
      

  2.   

    广播和组播都是需要路由支持的,你宽带连接连上的时候你的UDP报文发到你的网关去了,对广播和组播,如果没有在路由做设置的话,默认是丢弃的,断开以后就在本地广播,所以你就可以收到