组播的发送与接收问题

[信息] [引用] [回复] 楼 主 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();
                }
        }

}
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值