0

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

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • Might be a typo - but isn't your Upload class always creating a socket on port 1545 and ignoring the passed in port parameter? If it's not a typo - you might be Uploading to a different port than you thought. For what it's worth - the code transfers fine on my machine (client and server running on the same machine), although it doesn't close sockets cleanly on error. I wonder whether something can interrupt one of the threads? Are you testing client and server on the same machine, or different? – J Richard Snape Jan 14 '15 at 16:09
  • No you are right it's a typo, in the complete program it's a the same port on both sides. i am testing on the same machine. – user3538203 Jan 14 '15 at 16:54
  • OK - well - I can't make it fail in that scenario (i.e. can't reproduce failure). Do you get any exceptions when only part of the file is copied? Is there a reason you want to implement your own client server solution via your own Thread implementation working with sockets directly? – J Richard Snape Jan 14 '15 at 17:23
  • No reason. It's just the way i stumbled upon and understood so i implemented it so if you have a another solution it would be greatly appreciated – user3538203 Jan 14 '15 at 17:30

1 Answers1

1

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.

fge
  • 119,121
  • 33
  • 254
  • 329