server/client java 0kb file recieved
<p>I am sending a file through sockets but I am always getting 0kb files  and the problem is that no matter how much I tried it always doesn't send the file - its just creates a file with the same name and all with 0 kB.</p>

<p>here is my server code </p>

<pre><code>package networking;

import java.io.*;
import java.net.*;
import java.util.Scanner;



class TCPServer {



    public static String[] listfile() {

 String path = "C:/SAVE"; 

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 
  String d1[] = new String[listOfFiles.length];
  for (int i = 0; i &lt; listOfFiles.length; i++) 
  {

   if (listOfFiles[i].isFile()) 
   {
   files = listOfFiles[i].getName();
   d1[i]=listOfFiles[i].getName();

      }
  }

  return d1;

    }

    public static void main(String argv[]) throws Exception {
        Scanner s = new Scanner(System.in);
        String clientSentence;
        String capitalizedSentence;
        System.out.print("Listen on Port Number : ");
        int Socket = s.nextInt();

        ServerSocket welcomeSocket = new ServerSocket(Socket);
        while (true) {

            Socket connectionSocket = welcomeSocket.accept();

            BufferedReader inFromClient =
                    new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));


            DataOutputStream outToClient =
                    new DataOutputStream(connectionSocket.getOutputStream());

            clientSentence = inFromClient.readLine();
            String fn = inFromClient.readLine();
            SendFile(fn);


        }
    }

    public static void SendFile(String FileName) {
        while (true) {
            ServerSocket welcomeSocket = null;
            Socket connectionSocket = null;
            BufferedOutputStream outToClient = null;

            try {
                welcomeSocket = new ServerSocket(3248);
                connectionSocket = welcomeSocket.accept();
                outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
                // Do exception handling
            }


            if (outToClient != null) {
                File myFile = new File("C:\\" + FileName);

                byte[] mybytearray = new byte[(int) myFile.length()];

                FileInputStream fis = null;

                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException ex) {
                    // Do exception handling
                }
                BufferedInputStream bis = new BufferedInputStream(fis);

                try {
                    bis.read(mybytearray, 0, mybytearray.length);
                    outToClient.write(mybytearray, 0, mybytearray.length);
                    outToClient.flush();
                    outToClient.close();
                    connectionSocket.close();

                    // File sent, exit the main method
                    return;
                } catch (IOException ex) {
                    // Do exception handling
                }
            }
        }
    }
}
</code></pre>

<p>and the client is as follow</p>

<pre><code>public static void RecieveFile(String Fname , String Dire,String Hst,int Socket)
{
    byte[] reader = new byte[1];
    int bytesRead;

    Socket clientSocket = null;
    InputStream is = null;
   while(true)
   {
    try {
        clientSocket = new Socket(Hst, Socket);
        is = clientSocket.getInputStream();
    } catch (IOException ex) {
       ex.printStackTrace();
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    if (is != null) {

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(Dire+Fname);
            bos = new BufferedOutputStream(fos);
            bytesRead = is.read(reader, 0, reader.length);

            do {
                    baos.write(reader);
                    bytesRead = is.read(reader);
            } while (bytesRead != -1);

            bos.write(baos.toByteArray());
            bos.flush();
            bos.close();
            clientSocket.close();
        } catch (IOException ex) {
            ex.getStackTrace();
        }
}
   }
</code></pre>

<p>}</p>
