title: Java: EOFException in networking
body: <p>I am new to networking and I am getting an EOFException when I am trying to run the <strong>MyClient.java</strong>.</p>
  
  <p><strong>MyServer.java</strong></p>
  
  <pre><code>public class MyServer {
      public static void main(String[] args) {
          ServerSocket serverSocket = null;
          try {
              serverSocket = new ServerSocket(4321);
          } catch (IOException e) {
              e.printStackTrace();
          }
          while(true) {
              try {
                  Socket socket = serverSocket.accept();
                  OutputStream os =socket.getOutputStream();
                  OutputStreamWriter osw = new OutputStreamWriter(os);
                  BufferedWriter bw = new BufferedWriter(osw);
                  bw.write("Hello networking");
  
                  bw.close();
                  socket.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>MyClient.java</strong></p>
  
  <pre><code>public class MyClient {
      public static void main(String[] args) {
          Socket socket = null;
          try {
              socket = new Socket("127.0.0.1", 4321);
              InputStream is = socket.getInputStream();
              DataInputStream dis = new DataInputStream(is);
              System.out.println(dis.readUTF());
  
              dis.close();
              socket.close();
          } catch (UnknownHostException e) {
              e.printStackTrace();
          } catch (ConnectException e) {
              System.err.println("Could not connect");
          } catch (UTFDataFormatException e) {
              System.err.println("if the bytes do not represent a valid modified UTF-8 encoding of a string");
          } catch (EOFException e) {
              System.err.println("if this input stream reaches the end before reading all the bytes");
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I get <strong>if this input stream reaches the end before reading all the bytes</strong> for the error and the error seems to arise from the <strong>dis.readUTF()</strong> call.</p>
  
  <p>Could someone help me understand what am I missing? I am trying just trying to read what the server writes to the client when it gets connected, that is <strong>Hello networking</strong>.</p>
  
  <p>Thanks.</p>