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& | ||
+ | */ | ||
+ | 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 = " | ||
+ | } | ||
+ | try { | ||
+ | hostname = args[1]; | ||
+ | } catch (ArrayIndexOutOfBoundsException e) { | ||
+ | filename = " | ||
+ | } | ||
+ | // 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, | ||
+ | // preparazione del pacchetto | ||
+ | TFTPmessage msg = new TFTPmessage(seqNo, | ||
+ | ODP odp = new ODP(msg); | ||
+ | // spedizione | ||
+ | ds.send(odp.getDatagramPacket(dest, | ||
+ | System.out.println(" | ||
+ | // attesa dell' | ||
+ | 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(" | ||
+ | acked = true; | ||
+ | seqNo++; | ||
+ | } catch (Exception e) { | ||
+ | // se scatta il timeout, rispedisco il pacchetto | ||
+ | ds.send(odp.getDatagramPacket(dest, | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } while (!acked); // se non ricevo l'ack ripeto la spedizione del pacchetto corren | ||
+ | te | ||
+ | } | ||
+ | while (letti > 0); // e vado avanti finch< | ||
+ | |||
+ | } catch (FileNotFoundException e1) { | ||
+ | e1.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |
lpr-b/sender.txt · Ultima modifica: 13/11/2007 alle 19:01 (18 anni fa) da Marco Danelutto