title: Java sending and receiving file (byte[]) over sockets
body: <p>I am trying to develop a very simple client / server where the client converts a file to bytes, sends it to the server, and then converts the bytes back in to a file. </p>
  
  <p>Currently the program just creates an empty file. I'm not a fantastic Java developer so any help much appreciated.</p>
  
  <p>This is the server part that receives what the client sends.</p>
  
  <pre><code>ServerSocket serverSocket = null;
  
      serverSocket = new ServerSocket(4444);
  
  
      Socket socket = null;
      socket = serverSocket.accept();
  
      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
      DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      byte[] bytes = new byte[1024];
  
      in.read(bytes);
      System.out.println(bytes);
  
      FileOutputStream fos = new FileOutputStream("C:\\test2.xml");
      fos.write(bytes);
  </code></pre>
  
  <p>And here is the client part</p>
  
  <pre><code>Socket socket = null;
      DataOutputStream out = null;
      DataInputStream in = null;
      String host = "127.0.0.1";     
  
      socket = new Socket(host, 4444);
      out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
      in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  
      File file = new File("C:\\test.xml");
      //InputStream is = new FileInputStream(file);
      // Get the size of the file
      long length = file.length();
      if (length &gt; Integer.MAX_VALUE) {
          System.out.println("File is too large.");
      }
      byte[] bytes = new byte[(int) length];
  
      //out.write(bytes);
      System.out.println(bytes);
  
      out.close();
      in.close();
      socket.close();
  </code></pre>