title: Java socket receives byte array where each byte is 0
body: <p>I am making a program which takes a file, and sends it via socket to client. Client receives it and saves it to a file. That is what it is supposed to do.</p>
  
  <p>But somehow, byte array which client receives, contains only 0 bytes, so my output file is empty. here is the code:</p>
  
  <p>Server:</p>
  
  <pre><code>try {
          serverSocket=new ServerSocket(7575);
          serverSocket.setSoTimeout(1000000);
          System.out.println("serverSocket created.");
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("Error in creating new serverSocket on port 7575");
      } 
  
      for(int i=0;i&lt;array.length;i++)
          System.out.println(array[i]);
  
      Socket socket=null;
      try {
          System.out.println("Waiting for client...");
          socket=serverSocket.accept();
          System.out.println("Client accepted.");
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }
      PrintWriter outWriter=null;
      DataOutputStream outputStream=null;
      OutputStream os=null;
      BufferedOutputStream bos=null;
      try {
          os=socket.getOutputStream();
          outputStream=new DataOutputStream(os);
          outWriter=new PrintWriter(socket.getOutputStream());
          bos=new BufferedOutputStream(socket.getOutputStream());
          System.out.println("Server streams created.");
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }
      System.out.println("sending name "+name);
      outWriter.println(name);
      outWriter.flush();
  
      outWriter.println(array.length);
      outWriter.println("array.length"+array.length);
      outWriter.flush();
  
      try {
          os.write(array);
          os.flush();
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("couldnt send array of bytes");
      }
  
  
  
      try {
          os.close();
         outputStream.close();
  
          socket.close();
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>client:</p>
  
  <pre><code>public class Client implements Runnable {
  private Socket socket;
  private String folderPath;
  
  public Client(String p)
  {
      folderPath=p;
  }
  
  @Override
  public void run() 
  {
      try {
          System.out.println("Client connecting to localhost on 7575 port...");
          socket=new Socket("localhost", 7575);
      } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      BufferedReader reader=null;
      BufferedInputStream bis=null;
      InputStream input=null;
      DataInputStream in=null;
      try {
          System.out.println("creating streams");
          reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
          input=socket.getInputStream();
          in=new DataInputStream(input);
          bis=new BufferedInputStream(socket.getInputStream());
          System.out.println("streams created!");
      } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
      String name="";
      int size=0;
      String s="32";
      try {
          name=reader.readLine();
          s=reader.readLine();
      } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
      if(s!=null)
      size=Integer.parseInt(s);
      System.out.println("name: "+name);
      System.out.println("size: "+size);
  
      byte [] arr=new byte[size];
      try {
          input.read(arr);
      } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("couldnt read the byte array");
      }
  
      for(int i=0;i&lt;arr.length;i++)
          System.out.println(arr[i]);
  
      FileOutputStream fos=null;
      try {
           fos=new FileOutputStream(folderPath+"/"+name);
          } catch (FileNotFoundException ex) {
              System.out.println("Could write the file");
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
      try {
  
          fos.write(arr);
          fos.flush();
          } catch (IOException ex) {
              System.out.println("Could write the file2");
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      try {
          fos.close();
          } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  
          try {
          in.close();
          input.close();
          reader.close();
          socket.close();
          } catch (IOException ex) {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
          }
  
  
  
  }
  </code></pre>
  
  <p>}</p>