0

I am looking for way to improve the way I read incoming data from the server. Right now I am using a while loop to catch the incoming data:

BufferedReader myReader = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
            read=myReader.readLine();
            String line;
            while (!read.contentEquals("stop")) {

                read = myReader.readLine().trim();
                if((line=read)!=null) {
                Thread Comm = new Thread(new CommThread(read));
                Comm.start();
                }
            }

I start a new Thread to handle incoming data (I want to send commands to the client) and prevent it from blocking the UI Thread. The Comm Thread will execute given commands.

I want the Bufferedreader to keep listening for new incoming data, but also I want to filter out the messages where nothing is coming.

Quick question: After the Bufferedreader reads an incoming message, is the message deleted from the InputStream?

Are there events like "OnRequestReceive" or something like that or am I bound to keep using while loops?

Dboy0Z
  • 43
  • 1
  • 1
  • 8
  • 1
    `myReader.readLine().trim();` If readLine() returns null the trim() call will cause for a NullPointerException. – blackapps Jun 07 '20 at 10:05
  • Why do you have a String read; and a String line; ? Confusing. Usually one uses Sting line; – blackapps Jun 07 '20 at 10:09
  • Yes all the buffered reader reads from the inputstream is consumed hence deleted. – blackapps Jun 07 '20 at 10:09
  • There are no events if you use a blocking socket. – blackapps Jun 07 '20 at 10:11
  • You can't read anything from a `ServerSocket`, whether efficiently or otherwise. Do you mean `Socket`? – user207421 Jun 07 '20 at 10:12
  • The String line I added because otherwise it would'nt let me check for !=null for some reason. – Dboy0Z Jun 07 '20 at 10:13
  • yes I mean socket I think, I am new to this whole serversocket and socket stuff @Marquis of Lorne – Dboy0Z Jun 07 '20 at 10:14
  • It is unclear why you would start a thread for every line received. Or even one thread. Please explain why. Explain your protocol. – blackapps Jun 07 '20 at 10:14
  • I start a new Thread to handle incoming data (I want to send commands to the client) and prevent it from blocking the UI Thread. The Comm Thread will execute given commands. – Dboy0Z Jun 07 '20 at 10:16
  • 1
    Try: `while ((line=readLine())!=null)` – blackapps Jun 07 '20 at 10:18
  • Dont start a new thread. Just call a function that handles the command. It cannot block the UI as readLine() is already executed not in the UI thread. – blackapps Jun 07 '20 at 10:19
  • Some moderater has closed your post assuming the problem is a null pointer exception. A big mistake. Please continue. – blackapps Jun 07 '20 at 10:23
  • @blackapps No mistake. This code will incur an NPE at end of stream, and your own comment above fixes that. Otherwise why did you post it? You can't have it both ways. – user207421 Jun 07 '20 at 10:31
  • @Marquis of Lorne, It does not matter that once in a wile an npe will occurr. It has not even been reported. The problem is something completely different. Its about efficiency. You are killing a nice thread where i try to help OP with some socket programming. So please undo. – blackapps Jun 07 '20 at 10:38
  • I am indeed trying to look for improvement of efficiency. – Dboy0Z Jun 07 '20 at 10:41

0 Answers0