package com.vmichalak.protocol.ssdp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Client for discovering UPNP devices with SSDP (Simple Service Discovery Protocol).
 */
public class SSDPClient {
    /**
     * Discover any UPNP device using SSDP (Simple Service Discovery Protocol).
     * @param timeout in milliseconds
     * @param serviceType if null it use "ssdp:all"
     * @return List of devices discovered
     * @throws IOException
     * @see <a href="https://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol">SSDP Wikipedia Page</a>
     */
    public static List<Device> discover(int timeout, String serviceType) throws IOException {
        ArrayList<Device> devices = new ArrayList<Device>();
        byte[] sendData;
        byte[] receiveData = new byte[1024];

        /* Create the search request */
        StringBuilder msearch = new StringBuilder(
                "M-SEARCH * HTTP/1.1\nHost: 239.255.255.250:1900\nMan: \"ssdp:discover\"\n");
        if (serviceType == null) { msearch.append("ST: ssdp:all\n"); }
        else { msearch.append("ST: ").append(serviceType).append("\n"); }

        /* Send the request */
        sendData = msearch.toString().getBytes();
        DatagramPacket sendPacket = new DatagramPacket(
                sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1900);
        DatagramSocket clientSocket = new DatagramSocket();
        clientSocket.setSoTimeout(timeout);
        clientSocket.send(sendPacket);

        /* Receive all responses */
        while (true) {
            try {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                clientSocket.receive(receivePacket);
                devices.add(Device.parse(receivePacket));
            }
            catch (SocketTimeoutException e) { break; }
        }

        clientSocket.close();
        return Collections.unmodifiableList(devices);
    }
}

@@ -2054,24 +2054,1352 @@
 %0A        %7D%0A%0A
+        clientSocket.close();%0A        return Collections.unmodifiableList(devices);%0A    %7D%0A%0A    public static List%3CDevice%3E discoverOne(int timeout, String serviceType) throws IOException %7B%0A        ArrayList%3CDevice%3E devices = new ArrayList%3CDevice%3E();%0A        byte%5B%5D sendData;%0A        byte%5B%5D receiveData = new byte%5B1024%5D;%0A%0A        /* Create the search request */%0A        StringBuilder msearch = new StringBuilder(%0A                %22M-SEARCH * HTTP/1.1%5CnHost: 239.255.255.250:1900%5CnMan: %5C%22ssdp:discover%5C%22%5Cn%22);%0A        if (serviceType == null) %7B msearch.append(%22ST: ssdp:all%5Cn%22); %7D%0A        else %7B msearch.append(%22ST: %22).append(serviceType).append(%22%5Cn%22); %7D%0A%0A        /* Send the request */%0A        sendData = msearch.toString().getBytes();%0A        DatagramPacket sendPacket = new DatagramPacket(%0A                sendData, sendData.length, InetAddress.getByName(%22239.255.255.250%22), 1900);%0A        DatagramSocket clientSocket = new DatagramSocket();%0A        clientSocket.setSoTimeout(timeout);%0A        clientSocket.send(sendPacket);%0A%0A        /* Receive one response */%0A        try %7B%0A            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);%0A            clientSocket.receive(receivePacket);%0A            devices.add(Device.parse(receivePacket));%0A        %7D%0A        catch (SocketTimeoutException e) %7B %7D%0A%0A
         clie

Add discoverOne()