作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在用 Java 编写一个类,用于大大简化多播过程。但是,我有两个大问题:
谁能帮帮我?几个小时以来,我一直在尝试并寻找答案,看来我的代码遵循了连接、加入、发送和接收来自多播主机的数据的所有基本过程。
这是该类(class)相关部分的片段:
多路广播类:
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());
最佳答案
我以前遇到过类似的问题,必须确保在接收端指定了 NetworkInterface。
SocketAddress socketAddress = new InetSocketAddress(groupIp, groupPort);
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
socket.joinGroup(socketAddress, networkInterface);
interfaceName
是在 Linux 或 Mac 上运行 ifconfig
时显示的接口(interface)名称之一。
关于Java多播发送数据,不接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8471403/
今天小米米家官方微博宣布,小米手表尊享版开启限量定金预售,3 月 3 日早 10 点起支付尾款。购买即赠 399 元小米真无线蓝牙耳机 Air 2。 小米手表尊享版相比普通版核心没有变化,采用
今天下午,小米官方公布了小爱触屏音箱Pro 8,可以看到触控屏幕尺寸比较大,音箱上“长”了一个平板。据悉,小米小爱触屏音箱Pro 8具有白色的配色设计,下方有一个长条状的扬声器,上方带有一个尺寸比较
我是一名优秀的程序员,十分优秀!