title: How to read messages coming in on a TCP/IP port that do not contain end of line in Java?
body: <p>Let me explain what I'm trying to do first: I want to have a PortListener that listens to a port for incoming messages and when a message is coming in it needs to do something with that incoming message. The thing is: the listener will have nothing to do for most of the time, but when there's a message I want it processed within my Java application within a second as it's always possible that a next message is coming in already and I want that processed too asap. Messages do have some standard format and approximately the same length (4 chars, comma, 16 chars, comma, 2 chars, comma, 4 chars, comma, 12 chars, comma, 2-16 chars) but no end of line character. I know letting the software sending this data always sending 16 chars at the end would make this easier, but I'd rather solve this in the receiver in Java and leave the sending software as is.
  My first approach was to use a BufferedReader, but that stalls for 45 seconds :-S</p>
  
  <pre><code>    public void run() {
      try {
          serverSocket = new ServerSocket(portNumber);
  
          // this says to the OS: it's okay to reuse the address after (abnormal) program termination
          serverSocket.setReuseAddress(true);
      } catch (IOException e) {
          logger.error("Could not start listener on port: " + portNumber);
          return;
      }
  
      while (stayConnected) {
          try {
              serverSocket.setSoTimeout(1000);
              clientSocket = serverSocket.accept();
              logger.info("Connection established");
  
              //let's see if this helps ...
              //serverSocket.setSoTimeout(1000);
  
              BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
              int intOne;
  
              while (true) {
                  intOne = in.read();
                  if (intOne != -1) {
                      logger.debug("start reading from buffer");
                      char[] incomingStream = new char[100]; // more than enough for one message, but sometimes two are coming in after each other ...
                      boolean keepReading = true;
                      incomingStream[0] = (char) intOne;
                      int i = 1;
                      while (keepReading) {
                          int anotherInt = in.read();
                          if (anotherInt == -1 || i == 100) {
                              keepReading = false;
                          } else {
                              incomingStream[i] = (char) anotherInt;
                          }
                          i++;
                      }
                      logger.debug("first -1 found, so message finished");
                      final String incomingMessage = new String(incomingStream).trim();
                      Thread t1 = new Thread(new Runnable() {
                          public void run() {
                              System.out.println("Incoming : "+ incomingMessage);
                          }
                      });
                      t1.start();
                  }
              }
          } catch (IOException ioe) {
              if (stayConnected) {
                  logger.error("Portlistener loop failed: " + ioe.getMessage());
              }
          }
  </code></pre>
  
  <p>So I found this setting setSoTimeout which doesn't work when placed after serverSocket.accept() and when placed before serverSocket.accept() accept itself will throw a Time out all the time. Then I searched on stackoverflow and found this <a href="https://stackoverflow.com/questions/17943915/java-listening-on-port">Java listening on port</a> which seemed like a solution as serverSocket.accept() of course can time out when there's nothing to accept a connection for, unfortunately the code from that page doesn't work at all throwing "java.net.SocketException: Socket is closed
      at java.net.ServerSocket.accept(Unknown Source)
      at nl.secusystems.bws.bmonitor.thread.PortListener$1.run(PortListener.java:54)
  " all the time with some "Listener Running . . ." lines in betweeen before crashing because it can nog longer create another thread as it's getting a memory exception.</p>
  
  <p>My main question is: what's the way to go? Solution one where the bufferedReader will be placed inside a Future that times out in less than a second (making it possible one complete message and the start of the next message are caught in one String), the solution from that link but somehow adapting it that it won't create threads all the time, but maybe gets a pool of 5 threads and reuses them or a third way?</p>