本文整理汇总了Java中java.net.MulticastSocket.setReuseAddress方法的典型用法代码示例。如果您正苦于以下问题:Java MulticastSocket.setReuseAddress方法的具体用法?Java MulticastSocket.setReuseAddress怎么用?Java MulticastSocket.setReuseAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.MulticastSocket
的用法示例。
在下文中一共展示了MulticastSocket.setReuseAddress方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendDiscoveryBroacast
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* 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;
}
示例2: init
import java.net.MulticastSocket; //导入方法依赖的package包/类
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);
}
}
示例3: Socket
import java.net.MulticastSocket; //导入方法依赖的package包/类
public Socket(MainWindow main) throws UnknownHostException {
this.main = main;
this.address = InetAddress.getByName(Socket.INET_ADDR);
try {
multicastSocket = new MulticastSocket(this.main.getPort());
multicastSocket.setSendBufferSize(256000);
multicastSocket.setReceiveBufferSize(256000);
multicastSocket.setReuseAddress(true);
multicastSocket.joinGroup(address);
} catch (IOException ex) {
System.out.println("There is no socket connection. Sorry.");
System.out.println(ex.toString());
}
}
示例4: call
import java.net.MulticastSocket; //导入方法依赖的package包/类
@Override
public Object call() throws Exception {
InetAddress multicastAddress = InetAddress.getByName(this.SSDP_HOST);
MulticastSocket socket = new MulticastSocket(SSDP_PORT);
socket.setReuseAddress(true);
socket.setSoTimeout(10000);
socket.joinGroup(multicastAddress);
byte[] packetBuffer = SSDP_REQUEST().getBytes("UTF-8");
DatagramPacket mSearchPacket = new DatagramPacket(packetBuffer, packetBuffer.length, multicastAddress, SSDP_PORT);
socket.send(mSearchPacket);
/**
* Should run until either:
* - A compatible device is found
* - The socket times out (10000ms)
*/
while(true){
byte[] responseBuffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(packet);
String packetAddress = packet.getAddress().getHostAddress();
String fullResponse = new String(responseBuffer, 0, packet.getLength());
/**
* Make sure the response is not from localhost and the USN contains the HEOS urn.
*/
if(!packetAddress.equals(InetAddress.getLocalHost().getHostAddress())){
if(fullResponse.contains(SSDP_ST)){
return packetAddress;
}
}
}
}
示例5: findHeosIp
import java.net.MulticastSocket; //导入方法依赖的package包/类
public String findHeosIp() throws UnknownHostException, IOException{
InetAddress multicastAddress = InetAddress.getByName(this.SSDP_HOST);
MulticastSocket socket = new MulticastSocket(SSDP_PORT);
socket.setReuseAddress(true);
socket.setSoTimeout(10000);
socket.joinGroup(multicastAddress);
byte[] packetBuffer = SSDP_REQUEST().getBytes("UTF-8");
DatagramPacket mSearchPacket = new DatagramPacket(packetBuffer, packetBuffer.length, multicastAddress, SSDP_PORT);
socket.send(mSearchPacket);
/**
* Should run until either:
* - A compatible device is found
* - The socket times out
*/
while(true){
byte[] responseBuffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(packet);
String packetAddress = packet.getAddress().getHostAddress();
String fullResponse = new String(responseBuffer, 0, packet.getLength());
/**
* Make sure the response is not from localhost and the USN contains the HEOS urn.
*/
if(!packetAddress.equals(InetAddress.getLocalHost().getHostAddress())){
if(fullResponse.contains(SSDP_ST)){
return packetAddress;
}
}
}
}
示例6: executeDiscovery
import java.net.MulticastSocket; //导入方法依赖的package包/类
private void executeDiscovery() {
try {
final InetAddress inetAddress = InetAddress.getByName(MCAST_GROUP);
final byte[] datagramBuffer = new byte[512];
discoverySocket = new MulticastSocket(DISCOVERY_SOCKET_PORT);
discoverySocket.setSoTimeout(0);
discoverySocket.setReuseAddress(true);
discoverySocket.joinGroup(inetAddress);
while (!Thread.currentThread().isInterrupted()) {
final DatagramPacket recv = new DatagramPacket(datagramBuffer, datagramBuffer.length);
discoverySocket.receive(recv);
final String responseMessage = new String(recv.getData(), recv.getOffset(), recv.getLength());
final ZMoteDevice zmoteDevice = parseDiscoveryResponse(responseMessage);
if (zmoteDevice != null) {
discoveryResults.put(zmoteDevice.getUuid(), new ZMoteDiscoveryResult(zmoteDevice));
notifyDiscovery(zmoteDevice);
if (logger.isDebugEnabled()) {
logger.debug("Discovered ZMote device: {}", zmoteDevice.toString());
}
} else if (logger.isDebugEnabled()) {
logger.debug("Discovered unsupported device: {}", responseMessage);
}
}
} catch (final Exception e) {
if ((e instanceof SocketException) && Thread.currentThread().isInterrupted()) {
if (logger.isDebugEnabled()) {
logger.debug("Discovery service worker has been terminated.");
}
} else if (logger.isWarnEnabled()) {
logger.warn("ZMote device discovery failed!", e);
}
} finally {
safeCloseDiscoverySocket();
}
}