Search code examples
javaservertcpclient

Why is my program running twice on my Server?


I am writing tcp client-server program. When the client types "Hello" the server returns a list of files and directories of his current directory. When the client types "FileDownload " it downloads the selected file from the server.
When I type "Hello" it works fine, but when I type "FileDownload ", on the server side it runs twice the else if(received.contains("FileDownload")) block. Because of this the server is sending twice the data which is causing other issues on the client side.

Here is the server code:

public static void main(String[] args) throws IOException {
        
            ServerSocket servSock = new ServerSocket(1333);
            
            String received="";
            String[] s = null;
            File[] f1;
            int i=0;
            
            File f=new File(System.getProperty("user.dir"));
            f1=f.listFiles();
            
            for(File f2:f1) {
                if(f2.isDirectory())
                    System.out.println(f2.getName() + "\t<DIR>\t" + i);
                if(f2.isFile()) 
                    System.out.println(f2.getName() + "\t<FILE>\t" + i);
                i++;
            }
            
                while (true) { 
                    
                    Socket client = servSock.accept();
                    
                    InputStream in = client.getInputStream();
                    OutputStream out = client.getOutputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    PrintWriter pw = new PrintWriter(out, true);
                    s=f.list();
                    
                    while(true) {

                        received = reader.readLine();
                        if(received.equals("END")) break;
                        
                        if(received.equals("Hello")) {
                            System.out.println("Hello-Start");

                            int length=f1.length;
                            pw.println(length);
                            i=0;
                            for(File f2:f1) {
                                if(f2.isDirectory())
                                    pw.println(f2.getName() + "\t<DIR>\t" + i);
                                if(f2.isFile()) 
                                    pw.println(f2.getName() + "\t<FILE>\t" + i);
                                i++;
                            }
                            pw.println("Options: " + "\tFileDownload <FID>" + "\tFileUpload <name>" + "\tChangeFolder <name>");
                            System.out.println("Hello-End");

                        }
                        else if(received.contains("FileDownload")) {
                            System.out.println("FileDownload-Start");

                            int j=-1;
                            try {
                                j=Integer.parseInt(received.substring(13).trim());
                            }catch(NumberFormatException e) { 
                                System.err.println("error: " + e);
                            }
                            
                            if(j>0 && j<s.length) {
                                FileInputStream fi=new FileInputStream(s[j]);
                                byte[] b=new byte[1024];
                                System.out.println("file: "+s[j]);

                                pw.println(s[j]);
                                fi.read(b,0,b.length);
                                out.write(b,0,b.length);
                                System.out.println("FileDownload-End");
                            }
                        }

Here is the client code:

public static void main(String[] args) throws IOException {
        if ((args.length != 2))
        throw new IllegalArgumentException("Parameter(s): <Server> <Port>");
        
        Socket socket = new Socket(args[0], Integer.parseInt(args[1]));
        Scanner sc = new Scanner(System.in);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        PrintWriter pw = new PrintWriter(out, true);

        
        while(true) {
            String msg="", received="";
            String length;
            
            msg = sc.nextLine();
            pw.println(msg);
            
            if(msg.equals("END")) break; 
            
                if(poraka.equals("Hello")) {
                    System.out.println();
                    length = reader.readLine();
                    for(int i=0;i<Integer.parseInt(length);i++) {
                        received = reader.readLine();
                        System.out.println(received);
                    }
                    System.out.println("\n"+reader.readLine());
                }
                else if(msg.contains("FileDownload")) {
                    System.out.println("FileDownload-Start");
                    pw.println(msg);
                    byte[] b=new byte[1024];
                    File file=new File(reader.readLine().trim());
                    System.out.println(file.getName().trim());
                    FileOutputStream fo=new FileOutputStream("D:\\Eclipse WorkSpace\\proekt\\src\\client\\"+file.getName().trim());
                    System.out.println("file: "+file.getName().trim());

                    in.read(b,0,b.length);
                    fo.write(b,0,b.length);
                    System.out.println("FileDownload-End");
                }

I could not find what's causing this issue, so any help possible would be very highly appreciated!


Solution

  • It is because your client requests the data twice:

            msg = sc.nextLine();
            pw.println(msg);  // <-- this is the first time
    

    and then later

                else if(msg.contains("FileDownload")) {
                    System.out.println("FileDownload-Start");
                    pw.println(msg);  // <-- this is the second time