Hello, Iīm trying to send more files than 1 file from client to server via TCP. Server has to save the retrieved file to disc.
Java Code:
public class Client {
           connect(port, ip);
        doCopy(vstup_k);
 
    }
    public static void doCopy(  Scanner scann){
            while(file_number>0 ){
             if(accepted){
            System.out.println("Type absolute Path to the file");
            path=scann.nextLine();
        if( test_souboru(cesta)){
            copy(path,os); //os is outputstream
           file_number--;}
        }
            }
    
    }
  
    
    public static void connect(int portik, String ip_ad){
        try{
            socket = new Socket(ip_ad, portik);
            os = socket.getOutputStream();            
        } catch(IOException a){
            System.out.println(a.toString());
            System.exit(1);
        }
        
    }
    public static void copy(String path,OutputStream a) {
        accepted=false;
 
        try{
            File file = new File(path);
            
            file_in = new FileInputStream(file);
            DataOutputStream data_o=new DataOutputStream(a);
            data_in=new DataInputStream(socket.getInputStream());
            int lLength;
                
       
            data_o.writeUTF(file.getName());
                  while ((lLength =  file_in.read(buffer)) > 0) {
                a.write(buffer, 0, lLength);
            }
            String mess=data_in.readUTF();
            if(mess=="Accepted"){
            accepted=true;
            }
                 try{
         file_in .close();
            os.close();
        } catch(IOException ta){
                System.out.println(ta.toString());
            }
        } catch(IOException b){
            System.out.println(b.toString());
        }
  
    }
    
    
}
And server part
Java Code:
public class Server implements Runnable {
    
       public static void main(String[] _) {
        try {
            ServerSocket listener = new ServerSocket(port);
            
            while (true) {
                Server  file_rec = new Server();
                file_rec.socket = listener.accept();
                
                new Thread( file_rec ).start();
            }
        } catch (java.lang.Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    
    public void run() {
        int asa=4; //this is variable which tells the server that 4 files will be transfer, I havenīt fixed the problem with sending server the number of files which are transfering from client
        while(asa>0){
            acceptAndSave();
            asa--;
            buffer=new byte[128];
        }
        try{
            out.close();
            in.close();
        }catch(IOException wq){
            System.out.println(wq.toString());
        }
    }
    public  void acceptAndSave(){
        String file_name="";
        boolean name=false;
        
        try {
            File file=new File("server_copy");
            in = socket.getInputStream();
            out=new FileOutputStream(file);
            data_i=new DataInputStream(socket.getInputStream());
            data_o=new DataOutputStream(socket.getOutputStream());
            int length;
            
            if(!name){
                file=new File(data_i.readUTF());
                System.out.println(file.getName());
                out = new FileOutputStream(file);
                file_name=file.getName();
                name=true;
            }
            
            while ((length = in .read(buffer)) >=0) {
                out.write(buffer,0, length);
            }
            name=false;
            System.out.println("Byl prijat soubor klientem a nazvan "+file_name);
            data_o.writeUTF("Accepted");
            
        } catch (java.lang.Exception ex) {
            ex.printStackTrace(System.out);
            
        }
        
    }
}
And now client sends 1 file and server accepts it and saves it on disk but after this both (client and server) are waiting for somethig (probably client doesnīt notice that server has sent message "Accepted" but I donīt know why, or mayebe Server hasnīt sent the messge ). Please could you help me?