• No results found

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

N/A
N/A
Protected

Academic year: 2021

Share "TP1 : Correction. Rappels : Stream, Thread et Socket TCP"

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

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 ma- chine

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

(3)

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

String theLine ;

/* The only way to detect that the remote host has closed the connection is to attempt to read or write from the connection. If the remote host properly closed the connection, read() will return -1.

If the connection was not terminated normally, read() and write() 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) ?

(4)

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 java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import 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.getOutputStream());

//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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Socket;

import 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);

(5)

LectServeurThread lect= new LectServeurThread(connection);

networkIn = new BufferedReader(new InputStreamReader(connection.getInputStream()));

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");

}

}

(6)

static public void main(String argv[]) {

System.out.println("blabla");

} }

References

Related documents

public static void main (String[] args) {. Defines a method

Radical operations with lymph node dissection in patients with breast cancer are characterized by a high frequency of early postoperative complications, mainly associated

Commercial Financial Technical Execution Engineering & Procurement Construction & Startup Project Execution Planning Front-End Engineering Design Strategic Project

public static void main(String args[]) throws IOException { // Open your connection to a server, at port 1254. Socket s1 =

MiR-135b was measured in SSc dermal fibroblasts and healthy control cells using qRT-PCR and 480. normalised to let-7a levels using Taqman qPCR with primer specific

[r]

SANDVIK CNMA 12 04 08 H1P (uncoated carbide) inserts were used throughout. conventional emulsified mineral oil based flood cooling system was utilized. The three main

1995 Northeastern Forest Experiment Station and West Virginia University 1997 University of Missouri and the North Central Forest Experiment Station 1999 University of Kentucky and