Note that there are some explanatory texts on larger screens.

plurals
  1. POBufferedReader gives Connection reset?
    primarykey
    data
    text
    <p>I'm trying to send some data from a client to a server using Java, here is my PlayerThread which is ran on the server when a new client connects</p> <pre><code>PrintWriter out = new PrintWriter(socket.getOutputStream(), true); //Create a link to send data to the server BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); while(true) { out.println(pongData); String temp = in.readLine(); if(temp == "up") { System.out.println("Up you say"); } } //There is a little more but no point to give it all </code></pre> <p>The line String temp = in.readLine(); gives me this error when the client disconnects</p> <pre><code>java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at Pong.PongPlayerThread.run(PongPlayerThread.java:36) </code></pre> <p>Here is the client code</p> <pre><code>try { socket = new Socket(host, port); serverOut = new PrintWriter(socket.getOutputStream(), true); serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Couold not connect to host:" + host); System.exit(1); } catch (IOException e) { System.err.println("Could not get Input/Output from server"); System.exit(1); } System.out.println("Connected to Server"); while ((pos = serverInput.readLine()) != null) { String[] posValues = pos.split(":"); model.getBall().setX(Double.parseDouble(posValues[0])); model.getBall().setY(Double.parseDouble(posValues[1])); if(PongController.moveUp == true) { serverOut.println("up"); PongController.moveUp = false; } //If the client presses up or down arrow, a message will be sent to the server //the server will then update the movement and the clients will be able to see it } </code></pre> <p>This code works, but it doesn't seem to actually send the message up at all :(, If anyone could help me that would be awesome</p> <p>Canvas</p> <p>I have just editted some code and I have found out that in the PlayerThread the lines</p> <pre><code>if(temp.equals("up")) { System.out.println("Up you say"); } </code></pre> <p>is the problem, my stream on the client side is set to nothing at the start, could that be the problem? sending null? </p> <p>this is the updated version</p> <pre><code> while ((pos = serverInput.readLine()) != null) { String[] posValues = pos.split(":"); model.getBall().setX(Double.parseDouble(posValues[0])); model.getBall().setY(Double.parseDouble(posValues[1])); serverOut.println("nothing"); if(PongController.moveUp == true) { System.out.println("Up"); serverOut.println("up"); PongController.moveUp = false; } else { serverOut.println("nothing"); } } </code></pre> <p>but I still get the same error</p> <p>Checking to see if the in.readLine is equal to anything i get this here is the code</p> <pre><code>try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); System.out.println("Checking readLine value"); if(in.readLine() == null) { System.out.println("A ok"); } else { System.out.println(":" + in.readLine()); } while(true) { String temp = in.readLine(); if(temp == "up") { System.out.println("Up you say"); } out.println(pongData); } } catch (IOException e) { e.printStackTrace(); } </code></pre> <p>here is the outcome</p> <pre><code>Pong 400.0:301.0:60.0:300.0:740.0:300.0 Server started Server was setup and will try to create a socket Data sent Checking readLine value Connection reset, and then followed by lots of red lines </code></pre> <p>What else can i do?</p> <p>I just tried to check on my BufferedReader like so</p> <pre><code>BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); System.out.println(in.toString()); System.out.println(in.readLine()); </code></pre> <p>I get java.io.BufferedReader@6262937c for the in.toString but for the readLine i get the connection reset again...</p> <p>Quick update,</p> <p>In my client code, if i have this</p> <pre><code> while ((pos = serverInput.readLine()) != null) { String[] posValues = pos.split(":"); model.getBall().setX(Double.parseDouble(posValues[0])); model.getBall().setY(Double.parseDouble(posValues[1])); serverOut.println("nothing"); //&lt;--- </code></pre> <p>the client will connect, get the position of the pong ball, but then it will stop recieveing data from the server and just move the ball on its own accord (update method hasnt been disabled yet). the serverOut is </p> <pre><code>serverOut = new PrintWriter(socket.getOutputStream(), true); </code></pre>
    singulars
    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.
 

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