当前位置: 首页>>代码示例>>Java>>正文


Java MulticastSocket.setReuseAddress方法代码示例

本文整理汇总了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;
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:26,代码来源:SsdpDiscovery.java

示例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);
    }
}
 
开发者ID:offbye,项目名称:DroidDLNA,代码行数:27,代码来源:MulticastReceiverImpl.java

示例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());
    }
}
 
开发者ID:flschiavoni,项目名称:shareMySheet,代码行数:15,代码来源:Socket.java

示例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;
                }
            }                
    }
}
 
开发者ID:bart-kneepkens,项目名称:OpenHeosControl,代码行数:40,代码来源:SsdpCallable.java

示例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;
                }
            }                
    }
}
 
开发者ID:bart-kneepkens,项目名称:OpenHeosControl,代码行数:40,代码来源:SsdpClient.java

示例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();
    }
}
 
开发者ID:alexmaret,项目名称:openhab-binding-zmote,代码行数:43,代码来源:ZMoteDiscoveryService.java


注:本文中的java.net.MulticastSocket.setReuseAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。