[信息] [引用] [回复] 楼 主 BACK TOP 我写了一个组播的发送与接收的程序 ,编译和运行都通过了,但不能接收数据,源程序如下:接收源程序:import java.io.IOException;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.MulticastSocket;import javax.swing.*;public class ReceiveMulPacket implements Runnable{ DatagramPacket pack; MulticastSocket mulSocket; InetAddress inetAddress; byte msg[]=new byte[5000]; public ReceiveMulPacket() throws IOException{ mulSocket=new MulticastSocket(9875); inetAddress=InetAddress.getByName("226.255.8.0"); mulSocket.joinGroup(inetAddress); new Thread(this).start(); } public void run() { while(true){ try { System.out.println("before"); pack=new DatagramPacket(msg,msg.length,inetAddress,9875); mulSocket.receive(pack); System.out.println("after"); String reMsg=new String(pack.getData(),0,pack.getLength()); System.out.println(reMsg); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { new ReceiveMulPacket(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}发送源程序:import java.io.IOException;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.MulticastSocket;public class SendMsg { public static void sendMulPacket(String msg) throws IOException{ byte data[]=msg.trim().getBytes(); MulticastSocket mulSocket=new MulticastSocket(9875); InetAddress inetAddress=InetAddress.getByName("226.255.8.0"); mulSocket.joinGroup(inetAddress); DatagramPacket pack=new DatagramPacket(data,data.length,inetAddress,9875); mulSocket.send(pack); } public static void main(String[] args) { try { sendMulPacket("upl"+"information"); System.out.println("sended"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}