Example usage for java.net MulticastSocket getSoTimeout

List of usage examples for java.net MulticastSocket getSoTimeout

Introduction

In this page you can find the example usage for java.net MulticastSocket getSoTimeout.

Prototype

public synchronized int getSoTimeout() throws SocketException 

Source Link

Document

Retrieve setting for SO_TIMEOUT.

Usage

From source file:net.pms.network.UPNPHelper.java

/**
 * Gets the new multicast socket./*from w w w .ja v  a2  s .com*/
 *
 * @return the new multicast socket
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static MulticastSocket getNewMulticastSocket() throws IOException {
    NetworkInterface networkInterface = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

    if (networkInterface == null) {
        networkInterface = PMS.get().getServer().getNetworkInterface();
    }

    if (networkInterface == null) {
        throw new IOException("No usable network interface found for UPnP multicast");
    }

    List<InetAddress> usableAddresses = new ArrayList<InetAddress>();
    List<InetAddress> networkInterfaceAddresses = Collections.list(networkInterface.getInetAddresses());

    for (InetAddress inetAddress : networkInterfaceAddresses) {
        if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
            usableAddresses.add(inetAddress);
        }
    }

    if (usableAddresses.isEmpty()) {
        throw new IOException("No usable addresses found for UPnP multicast");
    }

    InetSocketAddress localAddress = new InetSocketAddress(usableAddresses.get(0), 0);
    MulticastSocket ssdpSocket = new MulticastSocket(localAddress);
    ssdpSocket.setReuseAddress(true);

    logger.trace(
            "Sending message from multicast socket on network interface: " + ssdpSocket.getNetworkInterface());
    logger.trace("Multicast socket is on interface: " + ssdpSocket.getInterface());
    ssdpSocket.setTimeToLive(32);
    logger.trace("Socket Timeout: " + ssdpSocket.getSoTimeout());
    logger.trace("Socket TTL: " + ssdpSocket.getTimeToLive());
    return ssdpSocket;
}