Strumenti Utente

Strumenti Sito


lpr-b:sender
no way to compare when less than two revisions

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.


lpr-b:sender [13/11/2007 alle 19:01 (18 anni fa)] (versione attuale) – creata Marco Danelutto
Linea 1: Linea 1:
 +<code java>
 +/*
 + * Created on Nov 17, 2003
 + *
 + * To change the template for this generated file go to
 + * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 + */
 +package TFTPudp;
  
 +import java.io.FileInputStream;
 +import java.io.FileNotFoundException;
 +import java.net.DatagramPacket;
 +import java.net.DatagramSocket;
 +import java.net.InetAddress;
 +
 +/**
 + * @author Danelutto Marco
 + *
 + * Questa <8F> la classe che si usa per spedire un file. 
 + * Il nome del file da spedire e' passato come primo parametro della riga di comando
 + * Il nome della macchina destinazione e' passato come secondo parametro della riga di comando
 + */
 +public class Sender {
 +
 +        /**
 +         * sono i parametri utilizzati in questo main e (PORT) nel main del receiver
 +         */
 +        static final int LPCK = 16;
 +        public static final int PORT = 12312;
 +        static final int TIMEOUT = 1000;
 +
 +        public static void main(String[] args) throws Exception {
 +                try {
 +                        String filename = ""; 
 +                        String hostname = "";
 +                        try {
 +                                filename = args[0];
 +                        } catch (ArrayIndexOutOfBoundsException e) {
 +                                filename = "prova.txt";
 +                        }
 +                        try {
 +                                hostname = args[1];
 +                        } catch (ArrayIndexOutOfBoundsException e) {
 +                                filename = "127.0.0.1";
 +                        }
 +                        // apertura del file in lettura
 +                        FileInputStream inputFile = new FileInputStream(filename);
 +                        InetAddress dest = InetAddress.getByName(hostname);
 +                        int port = PORT;
 +                        // creazione del datagram socket 
 +                        DatagramSocket ds = new DatagramSocket();
 +                        int letti = 0;
 +                        int seqNo = 0;
 +                        // ciclo di spedizione
 +                        do {
 +                                byte[] buf = new byte[LPCK];
 +                                // lettura di un blocco dal file
 +                                letti = inputFile.read(buf, 0, LPCK);
 +                                // preparazione del pacchetto 
 +                                TFTPmessage msg = new TFTPmessage(seqNo, buf, (letti>0?letti:0), (letti > 0));
 +                                ODP odp = new ODP(msg);
 +                                // spedizione
 +                                ds.send(odp.getDatagramPacket(dest, port));
 +                                System.out.println("Spedito pacchetto "+seqNo);
 +                                // attesa dell'ack
 +                                ds.setSoTimeout(TIMEOUT);
 +                                DatagramPacket ack = new DatagramPacket(new byte[1024], 1024);
 +                                boolean acked = false;
 +                                do {
 +                                        try {
 +                                                ds.setSoTimeout(TIMEOUT);
 +                                                ds.receive(ack);
 +                                                ds.setSoTimeout(0);
 +                                                ODP oack = ODP.getODP(ack);
 +                                                TFTPmessage mack = (TFTPmessage) oack.getObject();
 +                                                System.out.println("Ricevuto ack "+mack.nSeq);
 +                                                acked = true;
 +                                                seqNo++;
 +                                        } catch (Exception e) {
 +                                                // se scatta il timeout, rispedisco il pacchetto
 +                                                ds.send(odp.getDatagramPacket(dest, port));
 +                                                System.out.println("Rispedito pacchetto "+seqNo);
 +                                        }
 +                                } while (!acked); // se non ricevo l'ack ripeto la spedizione del pacchetto corren
 +te
 +                        }
 +                        while (letti > 0);  // e vado avanti finch<8F> ho cose da spedire
 +
 +                } catch (FileNotFoundException e1) {
 +                        e1.printStackTrace();
 +                }
 +        }
 +}
 +</code>
lpr-b/sender.txt · Ultima modifica: 13/11/2007 alle 19:01 (18 anni fa) da Marco Danelutto

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki