Pagina 3 di 3 primaprima 1 2 3
Visualizzazione dei risultati da 21 a 26 su 26

Hybrid View

  1. #1
    Allora dopo svariati tentativi ho deciso di invertire il funzionamento del mio programma per far in modo che un client possa inviare in un qualsiasi momento un file al server. Il server aprirà la connessione e salverà i file ricevuti in una cartella predefinità. Il Problema è che sembra darmi sempre lo stesso errore con la scelta del file

    Ho modificato le due classi e ho diviso il metodo per l'invio dei parametri in due metodi distinti "invia_nome" e invia_dimensione" ma non so se può andare bene comunque
    Ho paura di fare solo ancora iù confusione
    Ultima modifica di sanchez88; 19-05-2014 a 12:09 Motivo: messaggio non terminato

  2. #2
    codice:
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import javax.swing.JFileChooser;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author ste
     */
    public class Client_Socket {
        Socket socket;
        
        public Client_Socket() throws IOException{
            
            String indirizzoServer = "192.168.1.234";
            int porta = 9876;
            
            JFileChooser fileChooser = new JFileChooser();
            int n = fileChooser.showOpenDialog(null);
            File file = fileChooser.getSelectedFile(); 
            String path_file = file.getAbsolutePath();
            String nome = file.getName();
            long dimensione = file.length();
           
            System.out.println("Il nome del fileselezionato è: " +nome);
            System.out.println("La sua dimensione :  " +dimensione+ " byte");
            System.out.println("Percorso: " +path_file);
            
            
            boolean connesso = connect(indirizzoServer,porta);
            
            if (connesso == false)
            {
                System.out.println("*** ATTENZIONE: Non esiste alcun server sulla porta ***" + porta);
                System.exit(0);
            }
            
            // avvio la ricezione del file
            invia_File(file, nome, dimensione);
        }
        
        /**************************************************************************/
        public boolean connect(String ip,int porta)
        {
            boolean result = false;
            try {
                socket = new Socket(ip,porta);
                result = true;
                System.out.println("*** CONNESSO CON SUCCESSO *** ");
                } catch (UnknownHostException e) {
                e.printStackTrace();
                } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
        /**************************************************************************/
        public void invia_File(File file, String nome,long dimensione) throws  IOException
        {
            System.out.println("*** Inizio invio file");
            String name = nome;
            long dimension = dimensione;
            FileInputStream fis = null;
            DataOutputStream oos;
            try{
                fis = new FileInputStream(file);
                oos = new DataOutputStream(socket.getOutputStream());
                 byte[] buf = new byte[1024];
                int read,tot=0;
                // invio i dati del file al client
                invia_nome(nome, oos);
                invia_dimensione(dimensione, oos);
                System.out.println("iNVIATI: ");
                oos.flush();
                oos.flush();
                while ((read = fis.read(buf)) != -1 ) {
                    tot += read;
                    System.out.println("  - "+tot+ " byte");
                    oos.write(buf, 0, read);
                    oos.flush();
                }
                System.out.println("*** FILE INVIATO CON SUCCESSO ***");
                } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }  
        /**************************************************************************/
        public void invia_nome(String nome, DataOutputStream oos){
            try {
                oos.flush();
                oos.writeUTF(nome);
                //oos.writeLong(dimensione);
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void invia_dimensione(long dimensione, DataOutputStream oos){
            try {
                oos.flush();           
                oos.writeLong(dimensione);
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**************************************************************************/
        public static void main(String[] args) throws IOException {
            new Client_Socket();
        }
        
    }

  3. #3
    codice:
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author ste
     */
    public class Server_Socket {
    
      ServerSocket serverSocket;
      Socket socket;
      
      /****************************************************************************/
      // Qui faremo partire la connessione e richiameremo la ricezione del file
      public Server_Socket() throws IOException {
          
            final int porta = 9876;
            String destinazione = "C:\\prova";
            System.out.println("La cartella di destinazione è "+destinazione);
            
            // Qui faccio partire la connessione
            socket = connessione (porta);
            ricevi_File(destinazione);
      }
      
      /****************************************************************************/
      // METODO PER LA CONNESSIONE
      public Socket connessione(int porta) {
            Socket sock = null;
            try{
                serverSocket = new ServerSocket(9876);
                System.out.println("*** Attendo un client ***");
                sock = serverSocket.accept();
                }catch(UnknownHostException e) {
                e.printStackTrace();
                }catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("CONNESSIONE RIUSCITA CON SUCCESSO ");
            return sock;
        }
      /****************************************************************************/
      //   QUI E' DA CONTROLLARE MEGLIO LO SPLIT CHE CENTRA POCO
      public String acquisisci_nome(String nome, DataInputStream ois)
        {
            String name = null;
            try {
                name = (String)ois.readUTF();
                } catch (IOException e) {
                e.printStackTrace();
                }
            return name;
        }
      public long acquisisci_dimensione(long dimensione, DataInputStream ois)
        {
            long dimension = 0;
            try {
                dimension = (long)ois.readLong();
                } catch (IOException e) {
                e.printStackTrace();
                }
            return dimension;
        }
      /****************************************************************************/
      // METODO RICEVI FILE
      public void ricevi_File(String dest) throws IOException
        {
            System.out.println("* Inizio processo di ricezione");
            String destinazione = dest;
            int read = 0;
            int tot = 0;
            String nome = null;
            long dimensione = 0;
            DataInputStream ois = new DataInputStream(socket.getInputStream());
            String nome_file = acquisisci_nome(nome , ois);
            long dimensione_file = acquisisci_dimensione(dimensione , ois);
            //nome = param_file;
            //int dimensione = Integer.parseInt(param_file[1]);
            // stream per il salvataggio del file su disco
            File file = new File(destinazione+nome_file);
            // apro outputStream
            FileOutputStream fos = new FileOutputStream(file, true);
            byte[] buf = new byte[1024];
            System.out.println(" Ricevo: ");
            // prendo il pacchetto di 1024 kb
            try{
                while ((read = ois.read(buf)) != -1) {
                    tot += read;
                    System.out.println("  - "+tot+ " byte");
                    // scrivo il pacchetto su disco
                    fos.write(buf, 0, read);
                }
            }catch(java.net.SocketException e)
            {
                if (tot == dimensione_file)
                {
                    System.out.println("*** FILE RICEVUTO CON SUCCESSO ***");
                }
                else
                {
                    System.out.println("*** ATENZIONE - c''è stato un problema!");
                }
            }
        }
      /****************************************************************************/
      
      public static void main(String[] args) throws IOException {
            // TODO code application logic here
            
          new Server_Socket();
      }
      /****************************************************************************/
    }

  4. #4
    codice:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    
    
    
    public class TFTP_Server {
        
        DatagramSocket dataSocket;
        DatagramPacket dataPacket;
        
        private int porta;
        private String destinazione;
        
        /****   METODO PRINCIPALE  *****/
        public void connessioneRicezione() throws IOException {
            
            int read = 0;
            int tot = 0;
            String nome = null;
            long dimensione = 0;
            
             porta = 9876;
            destinazione = "C:\\prova\\";
            System.out.println("La cartella di destinazione è " +destinazione);
            //System.out.println(" ");
            // Creo la connessione 
            try {
                dataSocket = new DatagramSocket(porta);
                System.out.println("Server in attesa sulla porta: " +porta);
                
            } catch (IOException e) {
                e.printStackTrace();
            } 
            
            byte[] buf = new byte[1024];
            System.out.println("Attendo un Client ... ");
            dataPacket = new DatagramPacket(buf, buf.length);
            
                
            String name = acquisisci_nome(nome);
            System.out.println("Il nome del file é: " +name);
            File file = new File(destinazione+name);
            
            ricevi_File(file, name);
            
            
            
            
            
        } // FINE CONNESSIONE
        
        private void ricevi_File(File file, String name) throws IOException {
            
            String nome = name;
            int read = 0;
            int tot = 0;
            
            String percorso = file.getAbsolutePath();
            System.out.println("Destinazione : " +percorso);
            byte[] buf = new byte[1024];
            
            FileInputStream inputStream = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(percorso);
            System.out.println("creato file in:" +percorso);
            try{
                while ((read = inputStream.read(buf)) != -1) {
                    dataSocket.receive(dataPacket);
                    tot += read;
                    fos.write(nome.getBytes());
                }  
            }catch (java.net.SocketException e)
            {
                    System.out.println("*** FILE RICEVUTO CON SUCCESSO ***");
                
            }
                
              
        }
        
        private String acquisisci_nome(String nome) throws IOException {
            String nome_file = null;
            
                dataSocket.receive(dataPacket);
                
                //Acquisisco le informazioni del client che si collega
                InetAddress address = dataPacket.getAddress();           
                String client = address.getHostName();          
                int port = dataPacket.getPort();
                
                nome_file = (new String(dataPacket.getData()).trim());       
                return nome_file;
        }
    
        
        public static void main(String[] args) throws IOException
            {
                TFTP_Server tftpServer = new TFTP_Server();
                tftpServer.connessioneRicezione();
            }
    
        
        
        
            
            
       
        
    }

  5. #5
    Ciao. Allora il programma precedente funziona correttamente. Grazie e tutti per l'aiuto che mi avete fornito. =D
    Specialmente ad andbin.
    Comunque adesso sto cercando di sviluppare lo stesso programma però utilizzando il protocollo UDP e perciò sfruttando le DatagramSocket e i DatagramPacket.
    Le due classi comunicano perfettamente solo che il file non viene creato nella cartella selezionata. Potete darmi una mano?
    Grazie =)
    Di seguito vi posto le due classi

  6. #6
    codice:
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import javax.swing.JFileChooser;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
        
    /**
     *
     * @author ste
     */
    public class TFTP_Client {
        
        DatagramSocket dataSocket;
        DatagramPacket dataPacket;
        
        String nome;
        long dimensione;
        private InetAddress indirizzoServer;
        private int porta;
        
        public void Start() throws IOException {
                
                porta = 9876;
                indirizzoServer = InetAddress.getByName("127.0.0.1");  
            
                JFileChooser fileChooser = new JFileChooser();
                int n = fileChooser.showOpenDialog(null);
                File file = fileChooser.getSelectedFile(); 
                String path_file = file.getAbsolutePath();
                nome = file.getName();
                dimensione = file.length();
           
                //System.out.println("Il nome del fileselezionato è: " +nome);
                System.out.println("La sua dimensione :  " +dimensione+ " byte");
                System.out.println("Percorso: " +path_file);
                
                invia_Nome(nome);
                invia_File(file, nome );
        }
        
        private void invia_File(File file, String nome) throws IOException {
            System.out.println("*** Inizio invio file");
        
            try {
                dataSocket = new DatagramSocket();
                byte[] buf = new byte[1024];
                int read,tot=0;
                FileInputStream inputStream = new FileInputStream(file);
                dataPacket = new DatagramPacket (buf, buf.length, indirizzoServer, porta);
                
                // INVIO I PACCHETTI 
                while ((read = inputStream.read(buf)) != -1 ){
                    tot += read;
                    System.out.println("  - "+tot+ " byte");
                    dataSocket.send(dataPacket);
                }
                System.out.println("*** FILE INVIATO CON SUCCESSO ***");    
            } catch (FileNotFoundException e) {
                e.printStackTrace();    
            }
            
        }   // FINE INVIA_FILE   
    
        private void invia_Nome(String nome) throws IOException {
            
            dataSocket = new DatagramSocket();
            String nome_file = nome;
            System.out.println("Il nome del file è: " +nome_file);
            byte [] name = {0};
            name  = nome_file.getBytes();
            dataPacket = new DatagramPacket (name, name.length, indirizzoServer, porta);
            dataPacket.setData(name);
            dataPacket.setLength(name.length);
            
            // INVIO DEL PACCEHTTO SUL SOCKET
            dataSocket.send(dataPacket);
        }// FINE INVIA NOME
        
       
        
        
    
        public static void main(String[] args) throws IOException
            {
                TFTP_Client tftpClient = new TFTP_Client();
                tftpClient.Start();
            }
    
        
    
        
    
        
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.