If they're simple commands separated by newlines, then it should be fairly simple to roll your own client:
EDIT: HTTP isn't the optimal protocol for keeping a long-lived connection open and you might run into timeout issues. The code below is untested but should give you a good starting point.
EDIT 2: As pointed out in the comments, the thread pool may or may not be necessary or useful here. You should note that using threads won't magically make things faster or increase the amount of requests you can handle. The benefit of using a thread pool is to allow the client to immediately receive new commands while a previous command is still executing. That might be important if processing the command requires some I/O task, e.g. calling out to another server, but in your situation it might be overkill.
ExecutorService svc = Executors.newCachedThreadPool();
URLConnection c =
new URL("http://192.168.1.122/push/out.php?nduid=1").openConnection();
c.setReadTimeout(30 * 60 * 1000); // Set the read timeout to 30 minutes
c.connect();
InputStream is = c.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String cmd = null;
while ((cmd = r.readLine()) != null) {
svc.execute(new Runnable() {
public void run() {
processCmd(cmd); // work happens here in a different thread.
}
});
}
is.close();
The key here is that the input stream returned from the socket remains open as long as the underlying TCP connection remains open. The client can just keep reading from the stream, and the readLine
call will simply block as necessary until new data arrives. When readLine
returns null, that means the underlying input stream encountered end-of-file, which in turn means that the server closed the socket.
Once you have a full line of data, you can submit it to the thread pool to be parsed and executed in a different thread. Breaking the work up in this way allows you to focus on parsing and executing a command from a single line of text, which is much more manageable than trying to process an arbitrarily large stream of data.