Memory problem in Java service
I have an application which listens on a port and when it receives something runs a process (using the same port) in a new thread. The application runs as a windows service so I'm using a socket timeout to force the loop to keep evaluating rather than just blocking to allow the service to be stopped. The problem is that new socket objects are being created by the welcomeSocket.accept() each time round the loop so that it eventually runs out of memory. If I close the socket in the loop then the new thread throws an error. Code snippet below. Any help would be much appreciated.
private void go() throws Exception {
Socket connectionSocket = null;
welcomeSocket = new ServerSocket(triggerPort);
welcomeSocket.setSoTimeout(3000);
while(running) {
try { connectionSocket = welcomeSocket.accept();
if (connectionSocket != null) {
ReaderThread t = new ReaderThread(connectionSocket);
}
} catch(SocketTimeoutException timeOutEx) {}
}
}
public void stop() {
running = false;
}

