Hesham White
Hesham White

Reputation: 85

Java: A client/server file transfer (incomplete files)

I am building an application that will send file across the network through sockets.

There are 2 programs. Server and Client each have two classes Download and Upload.

 public class Download implements Runnable{

        public ServerSocket server;
        public Socket socket;
        public int port;
        public String saveTo = "";
        public InputStream In;
        public FileOutputStream Out;

        public Download(String saveTo){
            try {
                server = new ServerSocket(1544);
                port = 1544;
                this.saveTo = saveTo;            
            } 
            catch (IOException ex) {
                System.out.println("Exception [Download : Download(...)]");
            }
        }

        @Override
        public void run() {
            try {
                socket = server.accept();
                In = socket.getInputStream();
                Out = new FileOutputStream(saveTo);
                byte[] buffer = new byte[1024];
                int count;
                while((count = In.read(buffer)) >= 0){
                    Out.write(buffer, 0, count);
                }
                Out.flush();
                if(Out != null){ Out.close(); }
                if(In != null){ In.close(); }
                if(socket != null){ socket.close(); }
            } 
            catch (Exception ex) {

                ex.printStackTrace();
            }
        }
    }

and class Upload:

public class Upload implements Runnable{
    public String addr;
    public int port;
    public Socket socket;
    public FileInputStream In;
    public OutputStream Out;
    public File file;
    public Upload(String addr, int port, File filepath){
        super();
        try {
            file = filepath;
            socket = new Socket("127.0.0.1",1545);
            Out = socket.getOutputStream();
            In = new FileInputStream(filepath);
        } 
        catch (Exception ex) {
            System.out.println("Exception [Upload : Upload(...)]");
        }
    }
    @Override
    public void run() {
        try {       
            byte[] buffer = new byte[1024];
            int count;

            while((count = In.read(buffer)) >= 0){
                Out.write(buffer, 0, count);
            }
            Out.flush();
           if(In != null){ In.close(); }
            if(Out != null){ Out.close(); }
            if(socket != null){ socket.close(); }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

And in the class server:

 File myFile = new File("temp");
 Upload upl = new Upload("temp", 1544,myFile)
 fileThreadSend = new Thread(upl);
 fileThreadSend.start();

and in class client:

 Download dwn = new Download("temp");
 Thread fileThread = new Thread(dwn);
 fileThread.start();

the problem runs but it doesn't transfer all the file sometimes it transfer just 9kb of it and sometimes 20 MBS. what is wrong? I tried to search but couldn't find applicable any solution. Thanks in advance

Upvotes: 0

Views: 1127

Answers (1)

fge
fge

Reputation: 121820

Don't be bothered and use what the JDK has to offer:

More details here. And don't forget to use try-with-resources.

Also, avoid using Thread directly and use an ExecutorService instead.

Upvotes: 1

Related Questions