Université Paris 7 M1 II Protocoles réseaux TP1 : Correction Rappels : Stream, Thread et Socket TCP Tous les programmes seront écrits en Java. 1. (a) Ecrire une application qui lit des chaines au clavier et les affiche à l’écran (utiliser la classe Scanner). L’arrêt de cette application se fait à la lecture de "stop" au clavier. import java.util.Scanner; public class Main { public static void main(String[] args) { // l’arret se fait par "stop" String nom; Scanner sc=new Scanner(System.in); System.out.println("des lignes, stop pour finir:"); while(!(sc.hasNext("stop"))){ nom=sc.nextLine(); System.out.println("question pour:"+nom); } } } (b) Ecrire une application qui lit deux chaines au clavier (soient in et out ces chaines). L’application copie le contenu du fichier in dans le fichier out (utiliser la classe PrintStream). import java.io.*; import java.util.Scanner; public class Exo { public static void main(String[] args) { String nom; Scanner sc=new Scanner(System.in); System.out.println("nom du fichier, stop pour finir:"); nom=sc.next(); System.out.println("demande:"+nom); try{ BufferedReader din= new BufferedReader( new InputStreamReader( new FileInputStream("data.txt"))); PrintStream dout = new PrintStream(new FileOutputStream(nom)); while ((nom = din.readLine()) != null){ dout.println(nom); 1 System.out.println("copier:"+nom); } } catch (IOException ex) { System.out.println("pb fichier" ); } } } 2. On considère 2 processus connectés via une socket TCP (port 1027)(soient A et B ces 2 processus). A lit des chaines au clavier et les transmet à B. B affiche les chaines transmises. (a) A et B terminent lorsque A lit stop au clavier. Quels protocoles suivent A et B pour terminer ? A lit stop et ferme la socket. B arrete quand il ne peut plus lire. (b) Donner le code correspondant lorsque A et B sont sur la même machine import java.net.*; import java.util.*; import java.io.*; class pairA { public static void main (String[] args){ ServerSocket server; int port=1027; try { // exception si le port est utilise server = new ServerSocket(port); } catch (IOException ex) { System.out.println("port utilise" + port ); server=null; } try{ Socket connection = server.accept( ); //creation du buffer en emission DataOutputStream outToClient = new DataOutputStream(connection.getOutputStream()); //lecture au clavier et envoie au client String nom; Scanner sc=new Scanner(System.in); System.out.println("des lignes stop pour finir"); while(!(sc.hasNext("stop"))){ nom=sc.nextLine(); outToClient.writeBytes(nom+"\r\n"); } connection.close( ); server.close(); 2 } catch (IOException ex) { System.out.println("port utilise" + port ); server=null; } } } } ----------import java.net.*; import java.io.*; public class pairB { // se connecte a A lit les chaines et les affiche public static void main (String[] args){ BufferedReader networkIn = null; BufferedReader userIn=null; try { // connexion avec le serveur Socket s = new Socket("localhost", 1027); networkIn = new BufferedReader(new InputStreamReader(s.getInputStre String theLine ; /* The only way to detect that the remote host has closed the connec to attempt to read or write from the connection. If the rem host properly closed the connection, read() will return -1. If the connection was not terminated normally, read() and w will throw an exception. */ while((theLine = networkIn.readLine())!=null){ System.out.println("B:"+theLine); } } catch (UnknownHostException ex) { System.err.println(ex); } catch (IOException ex) { System.err.println(ex); } } } (c) Donner le code correspondant lorsque A et B sont sur des machines différentes. Remplacer "localhost" (d) A et B terminent lorsque B lit stop au clavier. Quels protocoles suivent A et B pour terminer (utiliser 2 threads) ? 3 B lit a la fois les donnees en provenance de A et le clavier. Quand il lit stop il ferme la socket (e) Donner le code correspondant pour A et B. import import import import import java.io.DataOutputStream; java.io.IOException; java.net.ServerSocket; java.net.Socket; java.util.Scanner; public class pairA2 { public static void main (String[] args){ ServerSocket server; int port=1027; try { // exception si le port est utilise server = new ServerSocket(port); Socket connection = server.accept( ); DataOutputStream outToClient =new DataOutputStream(connection.getOutput //lecture au clavier et envoie a pair B String nom; Scanner sc=new Scanner(System.in); System.out.println("des lignes"); while(true) {nom=sc.nextLine(); outToClient.writeBytes(nom+"\r\n"); } } catch (IOException ex) { System.err.println("sortie car la socket est ferme"); } } } ----------import import import import import java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.net.Socket; java.net.UnknownHostException; public class pairB2 { public static void main (String[] args){ BufferedReader networkIn = null; try { // connexion avec le serveur Socket connection = new Socket("localhost", 1027); 4 LectServeurThread lect= new LectServeurThread(connection); networkIn = new BufferedReader(new InputStreamReader(connection.get String theLine; while((theLine = networkIn.readLine())!=null){ System.out.println("B:"+theLine); } } catch (UnknownHostException ex) { System.err.println("sortie"); } catch (IOException ex) { System.err.println("sortie car la socket a ete fermee"); } } } ---------------import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class LectServeurThread extends Thread { private Socket s; public LectServeurThread(Socket s ) { this.s = s; this.start(); } public void run() { try{ String nom; Scanner sc=new Scanner(System.in); System.out.println("stop pour finir"); while (!(sc.hasNext("stop"))){ System.out.println("stop pour finir"); sc.nextLine(); } s.close( ); System.out.println("terminer"); } catch (IOException ex) { System.out.println("erreur"); } } 5 static public void main(String argv[]) { System.out.println("blabla"); } } 6