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(); } }