Java 多播发送数据,不接收
Posted
技术标签:
【中文标题】Java 多播发送数据,不接收【英文标题】:Java Multicast Sending Data, Not Receiving 【发布时间】:2012-01-18 06:36:03 【问题描述】:我正在用 Java 编写一个类,用于大大简化多播过程。但是,我有两个大问题:
-
该类发送数据(我可以使用我的网络监视器 Wireshark 验证这一点),但同一组中的任何其他人都没有收到该数据。
在某些机器上,发送数据包的 TTL 在传输过程中超过了(同样,根据 Wireshark)。
谁能帮帮我?几个小时以来,我一直在尝试寻找答案,看来我的代码遵循了连接、加入、发送和接收来自多播主机的数据的所有基本过程。
这是该类相关部分的sn-p:
多播类:
public class Multicaster
public int port = 5540;
protected String IPAddress;
private MulticastSocket msConn;
private InetAddress netAddr;
public Multicaster(String IPAddress)
this.IPAddress = IPAddress;
public String recieveData()
byte[] buf = new byte[1000];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
try
this.msConn.receive(pack);
new Message(pack);
String out = new String(pack.getData());
return out.substring(0, pack.getLength());
catch (IOException e)
return new String("");
public void joinGroup()
try
this.msConn.joinGroup(this.netAddr);
catch (IOException e)
//This error shouldn't occur since it would caught by the connect() method during initial connection to the host
public void connect() throws MulticasterInitException
//Try to create a multicast connection on the given IP address and port
try
try
//Create a multicast connection on a given port, throws UnknownHostException
this.msConn = new MulticastSocket(this.port);
//If all goes well, then create a connection to a given IP address using the above port number, throws IOException and SecurityException
this.netAddr = InetAddress.getByName(this.IPAddress);
/**
* Here all of the possible exceptions that are thrown above
* are caught and handled here. This works just fine.
*/
public void sendData(String data) throws MulticasterSendException
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), this.netAddr, this.port);
try
this.msConn.send(packet);
catch (IOException e)
throw new MulticasterSendException("Java could not communicate with the server. Please check your network connections.", e);
发送数据的示例用法:
Multicaster multicast = new Multicaster("239.0.0.0");
try
multicast.connect();
catch (MulticasterInitException e)
//Handle exception...
multicast.joinGroup();
try
multicast.sendData("Hi");
catch (MulticasterSendException e)
//Handle exception...
接收数据的示例用法:
Multicaster multicast = new Multicaster("239.0.0.0");
try
multicast.connect();
catch (MulticasterInitException e)
//Handle exception...
multicast.joinGroup();
System.out.print(multicast.recieveData());
【问题讨论】:
由于问题与http://***.com/questions/8471447/java-multicast-sample-program-is-unable-to-deliver-packets-within-lan-across-ho有关,您可以参考我的cmets 【参考方案1】:我之前遇到过类似的问题,必须确保在接收端指定了 NetworkInterface。
SocketAddress socketAddress = new InetSocketAddress(groupIp, groupPort);
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
socket.joinGroup(socketAddress, networkInterface);
其中interfaceName
是在 Linux 或 Mac 上运行 ifconfig
时显示的接口名称之一。
【讨论】:
你能给我关于***.com/questions/45162552/…我的问题的建议吗? SocketAddress 是抽象类,不能新建 SocketAddress。 示例已更改。以上是关于Java 多播发送数据,不接收的主要内容,如果未能解决你的问题,请参考以下文章