Sockets in java and sending multiple files

0

I want to send multiple files and I did the part of the selection of files and if I select a file to send, it does very well, yes I sent it, but the problem is when I want to send more than one file, when sending more from a file it tells me that the socket was closed, but I do not understand why if I'm not closing it:

JFileChooser jf = new JFileChooser();
    jf.setMultiSelectionEnabled(true);
    Socket cl = new Socket(host, pto);
    int r = jf.showOpenDialog(null);
    if(r==JFileChooser.APPROVE_OPTION){
        File[] fs = jf.getSelectedFiles();
        for(int o=0;fs.length>o;o++){                    
            System.out.println("\n\n estoy aqui"+fs[o]);
            File f = fs[o];
            String archivo = f.getAbsolutePath();
            String nombre = f.getName();
            long tam = f.length();
            DataOutputStream dos = new DataOutputStream(cl.getOutputStream());
            DataInputStream dis = new DataInputStream(new FileInputStream(archivo));
            dos.writeUTF(nombre);
            dos.flush();
            dos.writeLong(tam);
            dos.flush();
            System.out.println("asd");
            byte[] b = new byte[1024];
                    long enviados = 0;
                    int porcentaje , n;
                    while(enviados<tam){
                        n=dis.read(b);
                        dos.write(b,0,n);
                        dos.flush();
                        enviados = enviados+n;
                        porcentaje = (int)(enviados*100/tam);
                        System.out.print(("Enviados" + porcentaje + "%\r"));
                    }
                    System.out.print("\n\n Archivo enviado");
                    dos.close();
                    dis.close();

        }
        cl.close();
    
asked by JUAN ALVARES 18.02.2018 в 17:53
source

0 answers