Programmazione di rete con UDP UDP: nessuna connessione ! Invio: il mittente indica esplicitamente indirizzo IP e porta del ricevente ! Il ricevente deve esplicitamente estrarre indirizzo IP e n. di porta da pacchetti in arrivo Applicazioni Trasferimento non affidabile di Messaggi tra client e server UDP: pacchetti possono essere persi o arrivare fuori ordine 2: Application Layer 1 Interazione tra client e server Server (running on hostid) create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port umber Client create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read reply from clientSocket close clientSocket 2: Application Layer 2 Classe InetAddress 2: Application Layer 3 Example: Java client (UDP) import java.io.*; import java.net.*; Create input stream Create client socket Traduzione Indirizzo usando DNS class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); 2: Application Layer 4 Example: Java client (UDP), cont. Crea datagramma con dati da spedire, lungh., ind. IP, porta send datagramma al server DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); Ricevi datagramma dal server clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } 2: Application Layer 5 Example: Java server (UDP) import java.io.*; import java.net.*; Crea Socket su porta 9876 class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { Alloca buffer di Ricezione pacchetto Ricevi pacchetto DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); 2: Application Layer 6 Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); Estrai ind. IP da pacchetto ricevuto InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); Costruisci datagramma DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Scrivi datagram Su socket serverSocket.send(sendPacket); } } } Fine ciclo while Attendi altro datagramma 2: Application Layer 7 2: Application Layer 8