Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with reading data from a socket and appending it to a stringbuilder
    primarykey
    data
    text
    <p>I have followed some tutorials to create a basic server that can handle multiple clients at once. It receives data correctly but it chews up more and more memory until it runs out of heap space. I believe it is because my loop is not recognising my end character and leaving the for loop.</p> <p>Here is the code for the thread which receives the data:</p> <pre><code> while (true){ try { is = new BufferedInputStream(s.getInputStream()); InputStreamReader isr = new InputStreamReader(is); int character; StringBuilder process = new StringBuilder(); try { while((character = isr.read()) != 13) { process.append((char)character); //here is the issue - it just continues to append until it runs out of memory } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } parent.parent.writeToLog("Client " + Integer.toString(num) + " sent a message: " + process.toString()); System.out.println(process); is = null; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BufferedOutputStream os; try { os = new BufferedOutputStream(s.getOutputStream()); OutputStreamWriter osw = new OutputStreamWriter(os); osw.write("Response from server at client socket " + Integer.toString(num) + " at " + (new java.util.Date()).toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>I've put a comment into the code where it shows me the error on the stack trace. Here is the client, which is just the EchoClient from the Java tutorials:</p> <pre><code> import java.io.*; import java.net.*; import java.util.Random; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("localhost", 2405); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); out.println("Message to host: hello" + (char) 13); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } out.println(Integer.toString((new Random()).nextInt()) + (char) 13); out.close(); in.close(); stdIn.close(); echoSocket.close(); } } </code></pre> <p>You can see where I append the character 13 to the output and that is what is meant to end the loop but from my understanding this is not working. Could anyone help me out with this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload