问题描述
我正在尝试编写一个简单的多播试用版。
I am trying to write a simple multicast trial.
我使用了标准代码(发件人和收件人)。
I used a standard code (sender and reciever).
我尝试了几种不同的标准代码。接收代码似乎停留在接收上(好像它没有收到任何东西)。
I tried a few different standard pieces of code. it appears that the receiving code is stuck on receive (as if it's not receving anything).
接收方:
byte[] b = new byte[3];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
new MulticastSocket(4545); // must bind receive side
socket.joinGroup(InetAddress.getByName("226.100.100.125"));
while(true) {
socket.receive(dgram); // blocks until a datagram is received
System.err.println("Received " + dgram.getLength() +
" bytes from " + dgram.getAddress());
dgram.setLength(b.length); // must reset length field!
}
发送方:
DatagramSocket socket = new DatagramSocket();
byte[] b = new byte[]{(byte)1,(byte)5,(byte)3};
DatagramPacket dgram;
dgram = new DatagramPacket(b, b.length,
InetAddress.getByName("226.100.100.125"), 4545);
System.err.println("Sending " + b.length + " bytes to " +
dgram.getAddress() + ':' + dgram.getPort());
while(true) {
System.err.print(".");
socket.send(dgram);
Thread.sleep(1000);
}
我的代码有什么问题?
* 我尝试了很多不同的IP *
感谢您的帮助。
推荐答案
网络239.0.0.0/8被指定用于管理员多播流量。如果您的所有计算机都位于同一网段,则可以使用此网络中的ip来进行多播。
The network 239.0.0.0/8 is designated for administrator multi-cast traffic. If all of your machines are on the same network segment, you can use an ip in this network to play around with multi-casting.
以下是定义这些内容的RFC:
Here is the RFC defining these:
至于发送方式......您的代码应如下所示:
As far as sending goes... Your code should look something like this:
DatagramPacket p = ...
MulticastSocket s = new MulticastSocket(LISTENPORT);
InetAddress group = InetAddress.getByName(LISTENIP);
s.joinGroup(group);
s.send(p);
s.leaveGroup(group);
这篇关于java中的多播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!