下面列出了java.net.MulticastSocket#setReuseAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
synchronized public void init(NetworkInterface networkInterface,
Router router,
NetworkAddressFactory networkAddressFactory,
DatagramProcessor datagramProcessor) throws InitializationException {
this.router = router;
this.networkAddressFactory = networkAddressFactory;
this.datagramProcessor = datagramProcessor;
this.multicastInterface = networkInterface;
try {
log.info("Creating wildcard socket (for receiving multicast datagrams) on port: " + configuration.getPort());
multicastAddress = new InetSocketAddress(configuration.getGroup(), configuration.getPort());
socket = new MulticastSocket(configuration.getPort());
socket.setReuseAddress(true);
socket.setReceiveBufferSize(32768); // Keep a backlog of incoming datagrams if we are not fast enough
log.info("Joining multicast group: " + multicastAddress + " on network interface: " + multicastInterface.getDisplayName());
socket.joinGroup(multicastAddress, multicastInterface);
} catch (Exception ex) {
throw new InitializationException("Could not initialize " + getClass().getSimpleName() + ": " + ex);
}
}
synchronized public void init(NetworkInterface networkInterface,
Router router,
NetworkAddressFactory networkAddressFactory,
DatagramProcessor datagramProcessor) throws InitializationException {
this.router = router;
this.networkAddressFactory = networkAddressFactory;
this.datagramProcessor = datagramProcessor;
this.multicastInterface = networkInterface;
try {
log.info("Creating wildcard socket (for receiving multicast datagrams) on port: " + configuration.getPort());
multicastAddress = new InetSocketAddress(configuration.getGroup(), configuration.getPort());
socket = new MulticastSocket(configuration.getPort());
socket.setReuseAddress(true);
socket.setReceiveBufferSize(32768); // Keep a backlog of incoming datagrams if we are not fast enough
log.info("Joining multicast group: " + multicastAddress + " on network interface: " + multicastInterface.getDisplayName());
socket.joinGroup(multicastAddress, multicastInterface);
} catch (Exception ex) {
throw new InitializationException("Could not initialize " + getClass().getSimpleName() + ": " + ex);
}
}
@Override
protected void connect() throws Exception {
// Start multicast receiver
multicastReceiver = new MulticastSocket(udpPort);
multicastReceiver.setReuseAddress(udpReuseAddr);
InetAddress inetAddress = InetAddress.getByName(udpAddress);
if (netIf == null) {
multicastReceiver.joinGroup(inetAddress);
} else {
InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, udpPort);
try {
multicastReceiver.joinGroup(socketAddress, netIf);
} catch (Exception unsupportedAddress) {
disconnect();
return;
}
}
// Start thread
super.connect();
// Log
String msg = "Multicast discovery service started on udp://" + udpAddress + ':' + udpPort;
if (netIf == null) {
logger.info(msg + '.');
} else {
logger.info(msg + " (" + netIf.getDisplayName() + ").");
}
}
private UDPTransport(String address, int port, int ttl, int soTimeout) throws IOException {
// InetSocketAddress
inetSocketAddress = new InetSocketAddress(address, port);
// Create the socket but we don't bind it as we are only going to send data
// Note that we don't have to join the multicast group if we are only sending data and not receiving
multicastSocket = new MulticastSocket();
multicastSocket.setReuseAddress(true);
multicastSocket.setSoTimeout(soTimeout);
multicastSocket.setTimeToLive(ttl);
}
/**
* Broadcasts a SSDP discovery message into the network to find provided
* services.
*
* @return The Socket the answers will arrive at.
* @throws UnknownHostException
* @throws IOException
* @throws SocketException
* @throws UnsupportedEncodingException
*/
private MulticastSocket sendDiscoveryBroacast()
throws UnknownHostException, IOException, SocketException, UnsupportedEncodingException {
InetAddress multicastAddress = InetAddress.getByName("239.255.255.250");
final int port = 1900;
MulticastSocket socket = new MulticastSocket(port);
socket.setReuseAddress(true);
socket.setSoTimeout(130000);
socket.joinGroup(multicastAddress);
byte[] requestMessage = DISCOVER_MESSAGE.getBytes("UTF-8");
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, multicastAddress,
port);
socket.send(datagramPacket);
return socket;
}
public MulticastEchoServer() throws IOException {
socket = new MulticastSocket(4446);
socket.setReuseAddress(true);
group = InetAddress.getByName("230.0.0.0");
socket.joinGroup(group);
}