title: Java simple I/O server & client connection reset?
body: <p>I started learning about networking, sockets three days ago, and I started understanding a few things after I moved to using eclipse IDE.</p>
  
  <p>I managed to get the client to work, basically send the input to the server, but when I do so i get the following error:</p>
  
  <pre><code>java.net.SocketException: Connection reset
  </code></pre>
  
  <p>That's the full log of the server:</p>
  
  <pre><code>Server started.
  Connected: /127.0.0.1:57276
  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 Server.run(Server.java:31)
      at Server.main(Server.java:22)
  </code></pre>
  
  <p>Line 31:</p>
  
  <pre><code>in = input.readLine();
  </code></pre>
  
  <p>Line 22:</p>
  
  <pre><code>run();
  </code></pre>
  
  <p>I am probably not reading the I/O correctly, I didn't really look at "many" examples, I tried to get it myself and learn from my errors.</p>
  
  <p>Can someone explain what happens here? why does the connection reset?</p>
  
  <p>Server:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.*;
  
  public class Server {
  
      private static ServerSocket socket;
      private static PrintWriter output;
      private static BufferedReader input;
      private static Socket socketClient;
  
      public static void main(String[] args) {
          try {
              System.out.println("Server started.");
              socket = new ServerSocket(43537);
              socketClient = socket.accept();
              output = new PrintWriter(socketClient.getOutputStream());
              input = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
              System.out.println("Connected: " + socketClient.getRemoteSocketAddress().toString());
              run();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public static void run() throws IOException {
          String in;
          while (socketClient.isConnected()) {
               in = input.readLine();
              if (in != null) {
                  System.out.println(in);
              }
          }
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.*;
  import java.util.Scanner;
  
  public class build {
  
      /**
       * @param args
       * @throws IOException 
       * @throws UnknownHostException 
       */
      public static void main(String[] args) {
          try {
              System.out.println("Client started");
              Socket sock = new Socket("localhost", 43537);
              Scanner scanner = new Scanner(System.in);
              String input;
              PrintWriter out = new PrintWriter(sock.getOutputStream());
              BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  
              input = scanner.nextLine();
  
              if (input != null) {
                  out.print(input);
              }
              if (reader.toString() != null) {
                  System.out.println(reader.toString());
              }
          } catch (IOException e) {
              System.out.println("Client error");
          }
  
      }
  
  }
  </code></pre>