• Aucun résultat trouvé

Programmation Java Avancée : Exercices

N/A
N/A
Protected

Academic year: 2022

Partager "Programmation Java Avancée : Exercices"

Copied!
37
0
0

Texte intégral

(1)

Programmation Java Avancée : Exercices

IUT de Villetaneuse — R&T 2ème année

Laure Petrucci 16 novembre 2016

(2)

1 Les exceptions et les fichiers texte

Exercice 1.1 : Une classe pour les réseaux

Le code des classes utilisées est fourni ci-après.

Question 1 :Expliquer ce que fait chacune des méthodes de la classeProtocolesen complétant les lignes de commentaires.

Question 2 :Tester le fonctionnement des classes qui sont fournies.

Question 3 : Quelles méthodes des classesMachine, Routeur, Hub, etc. devraient lever des ex- ceptions ? Pourquoi ? Les modifier en conséquence.

Question 4 :Rajouter à la classeHub:

• une méthode enregistrerTexte permettant de sauvegarder les caractéristiques du hub dans un fichier texte ;

• un constructeur recevant en paramètre le nom d’un fichier texte et reconstituant le hub à partir de ce fichier.

Le fichier contiendra sur la première ligne le nombre de ports du hub, puis sur chaque ligne le nom d’une machine, son adresse IP et son adresse MAC (et éventuellement ses autres adresses IP et MAC). Par exemple :

Listing 4 – hub.txt 8

r 2 0 0 0 3 ; 1 4 0 . 1 0 . 3 . 4 ; 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 1 ; 1 2 0 . 1 0 . 3 . 1 ; 0 2 : 0 2 : 0 2 : 0 2 : 0 2 : 0 2 r 2 0 1 0 1 ; 1 4 0 . 1 0 . 5 . 2 ; 0 3 : 0 3 : 0 3 : 0 3 : 0 3 : 0 3

r 1 0 2 0 4 ; 1 4 0 . 1 0 . 2 . 8 ; 0 4 : 0 4 : 0 4 : 0 4 : 0 4 : 0 4 ; 1 3 0 . 1 0 . 1 . 1 ; 0 5 : 0 5 : 0 5 : 0 5 : 0 5 : 0 5

(3)

java.util

Class ArrayList<E>

Constructor Summary

ArrayList

() Constructs an empty list with an initial capacity of ten.

ArrayList

(

Collection

<? extends

E

> c) throws

NullPointerException

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList

(int initialCapacity) throws

IllegalArgumentException

Constructs an empty list with the specified initial capacity.

Method Summary

boolean

add

(

E

o) Appends the specified element to the end of this list.

void

add

(int index,

E

element) throws

IndexOutOfBoundsException

Inserts the specified element at the specified position in this list.

boolean

addAll

(

Collection

<? extends

E

> c) Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.

boolean

addAll

(int index,

Collection

<? extends

E

> c) Inserts all of the elements in the specified Collection into this list, starting at the specified position.

void

clear

() Removes all of the elements from this list.

boolean

contains

(

Object

elem) Returns true if this list contains the specified element.

E get

(int index) throws

IndexOutOfBoundsException

Returns the element at the specified position in this list.

int

indexOf

(

Object

elem) Searches for the first occurence of the given argument, testing for equality using the equals method.

boolean

isEmpty

() Tests if this list has no elements.

int

lastIndexOf

(

Object

elem) Returns the index of the last occurrence of the specified object in this list.

E remove

(int index) throws

IndexOutOfBoundsException

Removes the element at the specified position in this list.

boolean

remove

(

Object

o) Removes a single instance of the specified element from this list, if it is present (optional operation).

protected

void

removeRange

(int fromIndex, int toIndex) Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.

E set

(int index,

E

element) throws

IndexOutOfBoundsException

Replaces the element at the specified position in this list with the specified element.

int

size

() Returns the number of elements in this list.

Object

[]

toArray

() Returns an array containing all of the elements in this list in the correct order.

<T> T[]

toArray

(T[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.

(4)

Class Protocoles

java.lang.Object Protocoles

public class Protocoles extends

Object

Method Summary :

Modifier and

Type Method and Description

static char

calculerClasse(String adresseIP)

calcule la classe (A, B ou C) à partir de l'adresse IP machine supposée correcte

static String

calculerIPReseau(String adresseIP)

calcule l'adresse IP réseau à partir de l'adresse IP machine supposée correcte

static void

verifierIPMachine(String adresseIP) throws Exception vérifie que l'adresse IP d'une machine est une adresse IP valide de classe A, B ou C

static void verifierIPReseau(String adresseIP) throws Exception vérifie que le paramètre représente une adresse réseau de classe A, B ou C

static void verifierMac(String mac) throws Exception

vérifie que le paramètre représente une adresse MAC

Methods inherited from class java.lang.Object : clone , equals , finalize , getClass ,

hashCode , notify , notifyAll , toString , wait , wait , wait

(5)

Listing 6 – Protocoles.java 1 import j a v a . u t i l .∗;

2

3 public c l a s s P r o t o c o l e s {

4 // La méthode t r o i s P o i n t s . . .

5 //

6 //

7 private s t a t i c boolean t r o i s P o i n t s ( S t r i n g ch ) { 8 i n t n b P o i n t s = 0 ;

9 f o r (i n t i = 0 ; i < ch . l e n g t h ( ) ; i ++) 10 i f ( ch . charAt ( i ) == ’ . ’ )

11 n b P o i n t s ++;

12 return ( n b P o i n t s == 3 ) ; 13 } // f i n t r o i s P o i n t s 14

15 // La méthode c i n q D e u x P o i n t s . . .

16 //

17 //

18 private s t a t i c boolean c i n q D e u x P o i n t s ( S t r i n g ch ) { 19 i n t nbDeuxPoints =0;

20 f o r (i n t i = 0 ; i < ch . l e n g t h ( ) ; i ++) 21 i f ( ch . charAt ( i ) == ’ : ’ )

22 nbDeuxPoints++;

23 return ( nbDeuxPoints == 5 ) ; 24 } // f i n c i n q D e u x P o i n t s

25

26 // La méthode v e r i f i e r I P M a c h i n e . . .

27 //

28 //

29 public s t a t i c void v e r i f i e r I P M a c h i n e ( S t r i n g a d r e s s e I P ) throws E x c e p t i o n {

30 //

31 //

32 i f ( a d r e s s e I P . l e n g t h ( ) > 15 | | a d r e s s e I P . l e n g t h ( ) < 7 ) 33 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

34 //

35 //

36 i f ( ! t r o i s P o i n t s ( a d r e s s e I P ) )

37 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

38 //

39 //

40 i f ( ( a d r e s s e I P . charAt ( 0 ) == ’ . ’ ) | |

41 ( a d r e s s e I P . charAt ( a d r e s s e I P . l e n g t h ( ) − 1 ) == ’ . ’ ) ) 42 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

43 //

44 //

45 i f ( a d r e s s e I P . i n d e x O f ( " . . " ) != −1)

46 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

47 //

48 //

49 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( a d r e s s e I P , " . " ) ; 50 i n t p r e m i e r = I n t e g e r . p a r s e I n t ( s t . nextToken ( ) ) ;

51 //

52 //

(6)

53 i f ( ( p r e m i e r > 2 2 3 ) | | ( p r e m i e r <= 0 ) ) 54 throw new E x c e p t i o n ( " c l a s s e ␣ i n c o r r e c t e " ) ;

55 //

56 //

57 i n t compteur = 0 ; 58 i n t i = −1;

59 while ( s t . hasMoreTokens ( ) ) {

60 i = I n t e g e r . p a r s e I n t ( s t . nextToken ( ) ) ; 61 i f ( ( i >= 2 5 5 ) | | ( i < 0 ) )

62 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

63 compteur++;

64 }

65 //

66 //

67 i f ( i == 0 )

68 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ;

69 i f ( compteur != 3 )

70 throw new E x c e p t i o n ( " a d r e s s e ␣ IP ␣ i n c o r r e c t e " ) ; 71 } // f i n v e r i f i e r I P M a c h i n e

72

73 // La méthode v e r i f i e r M a c . . .

74 //

75 //

76 public s t a t i c void v e r i f i e r M a c ( S t r i n g mac ) throws E x c e p t i o n {

77 //

78 //

79 i f ( mac . i n d e x O f ( " : : " ) != −1)

80 throw new E x c e p t i o n ( " a d r e s s e ␣MAC␣ i n c o r r e c t e " ) ;

81 //

82 //

83 i f ( mac . s t a r t s W i t h ( " : " ) | | mac . endsWith ( " : " ) ) 84 throw new E x c e p t i o n ( " a d r e s s e ␣MAC␣ i n c o r r e c t e " ) ;

85 //

86 //

87 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( mac , " : " ) ; 88 i n t compteur = 0 ;

89 //

90 //

91 while( s t . hasMoreTokens ( ) ) {

92 S t r i n g morceau = s t . nextToken ( ) ;

93 //

94 //

95 i f ( morceau . l e n g t h ( ) != 2 )

96 throw new E x c e p t i o n ( " a d r e s s e ␣MAC␣ i n c o r r e c t e " ) ;

97 //

98 //

99 i n t i = I n t e g e r . p a r s e I n t ( morceau , 1 6 ) ; 100 i f ( ( i > 2 5 5 ) | | ( i < 0 ) )

101 throw new E x c e p t i o n ( " a d r e s s e ␣MAC␣ i n c o r r e c t e " ) ;

102 compteur++;

103 }

104 //

105 //

106 i f ( compteur ! = 6 )

(7)

107 throw new E x c e p t i o n ( " a d r e s s e ␣MAC␣ i n c o r r e c t e " ) ; 108 } // f i n v e r i f i e r M a c

109

110 // La méthode c a l c u l e r C l a s s e . . .

111 //

112 //

113 public s t a t i c char c a l c u l e r C l a s s e ( S t r i n g a d r e s s e I P ) {

114 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( a d r e s s e I P , " . " ) ; 115 i n t p r e m i e r = I n t e g e r . p a r s e I n t ( s t . nextToken ( ) ) ;

116 i f ( p r e m i e r < 1 2 7 ) 117 return( ’A ’ ) ;

118 i f ( ( p r e m i e r > 1 2 7 ) && ( p r e m i e r < 1 9 2 ) ) 119 return( ’B ’ ) ;

120 return( ’C ’ ) ;

121 } // f i n c a l c u l e r C l a s s e 122

123 // La méthode c a l c u l e r I P R e s e a u . . .

124 //

125 //

126 public s t a t i c S t r i n g c a l c u l e r I P R e s e a u ( S t r i n g a d r e s s e I P ) { 127 S t r i n g a d r e s s e I P R e s e a u = " " ;

128 char c l a s s e = P r o t o c o l e s . c a l c u l e r C l a s s e ( a d r e s s e I P ) ; 129 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( a d r e s s e I P , " . " ) ; 130 switch ( c l a s s e ) {

131 case ’A ’ : a d r e s s e I P R e s e a u = s t . nextToken ()+ " . 0 . 0 . 0 " ;

132 break;

133 case ’B ’ : a d r e s s e I P R e s e a u = s t . nextToken ()+ " . "+s t . nextToken ()+ " . 0 . 0 " ;

134 break;

135 case ’C ’ : a d r e s s e I P R e s e a u = s t . nextToken ()+ " . "+s t . nextToken ()+ " . "+

136 s t . nextToken ()+ " . 0 " ;

137 break;

138 }

139 return( a d r e s s e I P R e s e a u ) ; 140 } // f i n c a l c u l e r I P R e s e a u 141

142 // La méthode v e r i f i e r C l a s s e . . .

143 //

144 //

145 public s t a t i c void v e r i f i e r C l a s s e ( S t r i n g a d r e s s e I P ) throws E x c e p t i o n { 146 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( a d r e s s e I P , " . " ) ;

147 i n t p r e m i e r = I n t e g e r . p a r s e I n t ( s t . nextToken ( ) ) ; 148 i f ( ( p r e m i e r > 2 2 3 ) | | ( p r e m i e r <= 0 ) )

149 throw new E x c e p t i o n ( " c l a s s e ␣ i n c o r r e c t e " ) ; 150 } // f i n v e r i f i e r C l a s s e

151

152 // La méthode v e r i f i e r I P R e s e a u . . .

153 //

154 //

155 public s t a t i c void v e r i f i e r I P R e s e a u ( S t r i n g a d r e s s e I P ) throws E x c e p t i o n {

156 //

157 //

158 P r o t o c o l e s . v e r i f i e r C l a s s e ( a d r e s s e I P ) ;

159 char c l a s s e = P r o t o c o l e s . c a l c u l e r C l a s s e ( a d r e s s e I P ) ; 160 S t r i n g [ ] t = new S t r i n g [ 4 ] ;

(8)

161 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r ( a d r e s s e I P , " . " ) ; 162 f o r (i n t i = 0 ; i < 4 ; i ++)

163 t [ i ] = s t . nextToken ( ) ;

164 //

165 //

166 i f ( ( c l a s s e == ’C ’ ) && ! t [ 3 ] . e q u a l s ( " 0 " ) )

167 throw new E x c e p t i o n ( " a d r e s s e ␣ r é s e a u ␣ c l a s s e ␣C␣ i n c o r r e c t e " ) ;

168 //

169 //

170 i f ( ( c l a s s e == ’B ’ ) && ( ! t [ 2 ] . e q u a l s ( " 0 " ) | | ! t [ 3 ] . e q u a l s ( " 0 " ) ) ) 171 throw new E x c e p t i o n ( " a d r e s s e ␣ r é s e a u ␣ c l a s s e ␣B␣ i n c o r r e c t e " ) ;

172 //

173 //

174 i f ( ( c l a s s e == ’A ’ ) && ( ! t [ 1 ] . e q u a l s ( " 0 " ) | | ! t [ 2 ] . e q u a l s ( " 0 " ) | | 175 ! t [ 3 ] . e q u a l s ( " 0 " ) ) )

176 throw new E x c e p t i o n ( " a d r e s s e ␣ r é s e a u ␣ c l a s s e ␣A␣ i n c o r r e c t e " ) ; 177 } // f i n v e r i f i e r I P R e s e a u

178

179 // La méthode v e r i f i e r I P R e s e a u 2 . . .

180 //

181 //

182 public s t a t i c void v e r i f i e r I P R e s e a u 2 ( S t r i n g a d r e s s e I P ) throws E x c e p t i o n { 183 P r o t o c o l e s . v e r i f i e r C l a s s e ( a d r e s s e I P ) ;

184 S t r i n g i p = P r o t o c o l e s . c a l c u l e r I P R e s e a u ( a d r e s s e I P ) ; 185 i f ( ! i p . e q u a l s ( a d r e s s e I P ) )

186 throw new E x c e p t i o n ( " a d r e s s e ␣ r é s e a u ␣ i n c o r r e c t e " ) ; 187 } // f i n v e r i f i e r I P R e s e a u 2

188 } // f i n de l a c l a s s e P r o t o c o l e s

Listing 7 – Donnees.java 1 public c l a s s Donnees {

2 private S t r i n g d o n n e e s ; 3

4 public Donnees ( S t r i n g d o n n e e s ) { 5 t h i s. d o n n e e s = d o n n e e s ;

6 }

7

8 public S t r i n g g e t D o n n e e s ( ) { 9 return(t h i s. d o n n e e s ) ;

10 }

11

12 public S t r i n g t o S t r i n g ( ) { 13 return(t h i s. d o n n e e s ) ;

14 }

15

16 public boolean e q u a l s ( O b j e c t o ) { 17 i f ( ! ( o i n s t a n ce o f Donnees ) ) 18 return f a l s e;

19 Donnees p = ( Donnees ) o ;

20 return(t h i s. d o n n e e s . e q u a l s ( p . d o n n e e s ) ) ;

21 }

22 } // f i n de l a c l a s s e Donnees

(9)

Listing 8 – Trame.java 1 public c l a s s Trame{

2 private S t r i n g DA;

3 private S t r i n g SA ; 4 private S t r i n g EType ; 5 private Donnees d o n n e e s ; 6

7 public Trame ( S t r i n g DA, S t r i n g SA , S t r i n g EType , Donnees d o n n e e s ) { 8 t h i s.DA = DA;

9 t h i s. SA = SA ;

10 t h i s. EType = EType ; 11 t h i s. d o n n e e s = d o n n e e s ;

12 }

13

14 public S t r i n g getDA ( ) { 15 return(t h i s.DA) ;

16 }

17

18 public S t r i n g getSA ( ) { 19 return(t h i s. SA ) ;

20 }

21

22 public S t r i n g getEType ( ) { 23 return(t h i s. EType ) ;

24 }

25

26 public Donnees g e t D o n n e e s ( ) { 27 return(t h i s. d o n n e e s ) ;

28 }

29

30 public S t r i n g t o S t r i n g ( ) {

31 return(t h i s.DA + " ; " + t h i s. SA + " ; " + t h i s. EType + " ; " + 32 t h i s. d o n n e e s ) ;

33 }

34 } // f i n de l a c l a s s e Trame

Listing 9 – Hub.java 1 import j a v a . u t i l .∗;

2

3 public c l a s s Hub{

4 private A r r a y L i s t <Machine> l i s t e M a c h i n e s ; 5 private i n t nbMachines ;

6

7 public Hub (i n t t a i l l e ) {

8 t h i s. l i s t e M a c h i n e s = new A r r a y L i s t <Machine > ( ) ; 9 f o r (i n t i = 0 ; i < t a i l l e ; i ++)

10 t h i s. l i s t e M a c h i n e s . add (n u l l) ; 11 t h i s. nbMachines = 0 ;

12 }

13

14 public i n t s i z e ( ) {

15 return(t h i s. l i s t e M a c h i n e s . s i z e ( ) ) ;

16 }

(10)

17

18 public i n t getNbMachines ( ) { 19 return(t h i s. nbMachines ) ;

20 }

21

22 public A r r a y L i s t <Machine> g e t L i s t e M a c h i n e s ( ) { 23 return(t h i s. l i s t e M a c h i n e s ) ;

24 }

25

26 public boolean e s t V i d e ( ) {

27 return(t h i s. nbMachines == 0 ) ;

28 }

29

30 public boolean e s t P l e i n ( ) {

31 return(t h i s. nbMachines == t h i s. s i z e ( ) ) ;

32 }

33

34 // r e t o u r n e l a machine b r a n c h é e s u r l e p o r t numéro p o r t 35 public Machine elementAt (i n t p o r t ) {

36 i f ( ( p o r t < 0 ) | | ( p o r t >= t h i s. nbMachines ) ) 37 return(n u l l) ;

38 e l s e return (t h i s. l i s t e M a c h i n e s . g e t ( p o r t ) ) ;

39 }

40

41 public boolean e s t O c c u p e (i n t p o r t ) { 42 return(t h i s. elementAt ( p o r t ) != n u l l) ;

43 }

44

45 // b r a n c h e l a machine m s u r l e p o r t p o r t s a n s p r é c a u t i o n 46 // i l n ’ y a p a s b e s o i n de v é r i f i e r l e numéro de p o r t 47 // p a r c e que l e s méthodes q u i u t i l i s e n t s e t M a c h i n e f o n t 48 // l a v é r i f i c a t i o n . Méthode u t i l e pour p a s s e r de

49 // l ’ i m p l é m e n t a t i o n a v e c t a b l e a u à l ’ i m p l é m e n t a t i o n a v e c 50 // a v e c A r r a y L i s t

51 // l a m e t t r e en p u b l i c pour p o u v o i r l a t e s t e r ; p r i v a t e e n s u i t e 52 private void s e t M a c h i n e ( Machine m, i n t p o r t ) {

53 t h i s. l i s t e M a c h i n e s . s e t ( p o r t ,m) ;

54 }

55

56 // i n i t i a l i s a t i o n i n t e r a c t i v e

57 // on p e u t b r a n c h e r d e s machines ou d e s r o u t e u r s 58 // l ’ o c c u p a t i o n d e s p o r t s d o i t ê t r e c o n t i n u e 59 public void i n i t ( ) {

60 i n t r e p o n s e ; 61 S t r i n g e n c o r e ;

62 Machine m;

63 S c a n n e r s c = new S c a n n e r ( System . i n ) ;

64 do {

65 do {

66 System . o u t . p r i n t ( " 1 ␣ : ␣ machine ␣ / ␣ 2 ␣ : ␣ r o u t e u r ␣ " ) ; 67 r e p o n s e=s c . n e x t I n t ( ) ;

68 } while ( ( r e p o n s e < 1 ) | | ( r e p o n s e > 2 ) ) ; 69 i f ( r e p o n s e == 1 )

70 m = new Machine ( ) ;

(11)

71 e l s e m = new Routeur ( ) ; 72 m. i n i t ( ) ;

73 t h i s. s e t M a c h i n e (m,t h i s. nbMachines ) ; 74 m. setHub (t h i s) ;

75 t h i s. nbMachines++;

76 System . o ut . p r i n t ( " e n c o r e ␣ ? ␣ : ␣ " ) ; 77 e n c o r e = s c . n e x t ( ) ;

78 } while ( e n c o r e . e q u a l s ( " o " ) && !t h i s. e s t P l e i n ( ) ) ;

79 }

80

81 public S t r i n g t o S t r i n g ( ) { 82 S t r i n g s = " " ;

83 f o r (i n t i = 0 ; i < t h i s. nbMachines ; i ++) 84 s = s + " \n" + t h i s. elementAt ( i ) ;

85 return( s ) ;

86 }

87

88 // r e t o u r n e l e numéro du p o r t s u r l e q u e l l a machine m e s t 89 // b r a n c h é e ; r e t o u r n e −1 s i l a machine m n ’ e s t p a s b r a n c h é e 90 public i n t p o s i t i o n M a c h i n e ( Machine m) {

91 return(t h i s. l i s t e M a c h i n e s . l a s t I n d e x O f (m) ) ;

92 }

93

94 // r e t o u r n e t r u e s i l a machine m e s t b r a n c h é e au hub 95 // e t f a l s e s i n o n

96 public boolean c o n t a i n s ( Machine m) {

97 return (t h i s. l i s t e M a c h i n e s . c o n t a i n s (m) ) ;

98 }

99

100 // on ne d o i t p a s b r a n c h e r deux f o i s l a même machine 101 // on b r a n c h e l a machine s u r l e p r e m i e r p o r t l i b r e 102 public boolean c o n n e c t e r M a c h i n e ( Machine m) {

103 i f (t h i s. e s t P l e i n ( ) | | t h i s. c o n t a i n s (m) ) 104 return (f a l s e) ;

105 t h i s. s e t M a c h i n e (m,t h i s. nbMachines ) ; 106 m. setHub (t h i s) ;

107 t h i s. nbMachines++;

108 return(true) ;

109 }

110

111 public boolean c o n n e c t e r R o u t e u r ( Routeur r ,i n t e t h ) { 112 i f (t h i s. e s t P l e i n ( ) | | t h i s. c o n t a i n s ( r ) )

113 return (f a l s e) ;

114 t h i s. s e t M a c h i n e ( r ,t h i s. nbMachines ) ;

115 i f ( e t h == 0 )

116 r . setHub (t h i s) ; 117 e l s e r . setHub2 (t h i s) ; 118 t h i s. nbMachines++;

119 return(true) ;

120 }

121

122 // on d é b r a n c h e l a machine b r a n c h é e s u r l e p o r t p o r t 123 // ( s i c e l u i−c i e s t o c c u p é ) , on l a r e m p l a c e p a r 124 // l a d e r n i è r e e t on met c e t t e d e r n i è r e à n u l l

(12)

125 public boolean d e b r a n c h e r M a c h i n e (i n t p o r t ) { 126 i f (t h i s. e s t O c c u p e ( p o r t ) ) {

127 t h i s. elementAt ( p o r t ) . setHub (n u l l) ;

128 Machine d e r n i e r e = t h i s. elementAt (t h i s. nbMachines − 1 ) ; 129 t h i s. s e t M a c h i n e ( d e r n i e r e , p o r t ) ;

130 t h i s. s e t M a c h i n e (null,t h i s. nbMachines − 1 ) ; 131 t h i s. nbMachines−−;

132 return(true) ;

133 } e l s e return(f a l s e) ;

134 }

135

136 public boolean d e b r a n c h e r R o u t e u r (i n t p o r t ,i n t e t h ) { 137 i f (t h i s. e s t O c c u p e ( p o r t ) ) {

138 i f ( e t h == 0 )

139 t h i s. elementAt ( p o r t ) . setHub (n u l l) ;

140 e l s e ( ( Routeur )t h i s. elementAt ( p o r t ) ) . setHub2 (n u l l) ; 141 // on b r a n c h e l a d e r n i è r e machine

142 // à l a p l a c e de c e l l e qu ’ on v i e n t de d é b r a n c h e r

143 Machine d e r n i e r e = t h i s. elementAt (t h i s. nbMachines − 1 ) ; 144 t h i s. s e t M a c h i n e ( d e r n i e r e , p o r t ) ;

145 t h i s. s e t M a c h i n e (null,t h i s. nbMachines − 1 ) ; 146 t h i s. nbMachines−−;

147 return(true) ;

148 } e l s e return(f a l s e) ;

149 }

150

151 // on d é b r a n c h e l a machine m ( s i e l l e e s t b r a n c h é e )

152 // on l a r e m p l a c e p a r l a d e r n i è r e e t on met c e t t e d e r n i è r e à n u l l 153 public boolean d e b r a n c h e r M a c h i n e ( Machine m) {

154 return(t h i s. d e b r a n c h e r M a c h i n e (t h i s. p o s i t i o n M a c h i n e (m) ) ) ;

155 }

156

157 public boolean d e b r a n c h e r R o u t e u r ( Routeur r ,i n t e t h ) {

158 return(t h i s. d e b r a n c h e r R o u t e u r (t h i s. p o s i t i o n M a c h i n e ( r ) , e t h ) ) ;

159 }

160

161 // l e hub émet l a trame v e r s t o u t e s l e s machines c o n n e c t é e s 162 // s a u f c e l l e q u i a émis l a trame

163 public void emettreTrame ( Machine m a c h i n e E m e t t r i c e , Trame t ) { 164 f o r (i n t i = 0 ; i < t h i s. nbMachines ; i ++)

165 i f (t h i s. elementAt ( i ) != m a c h i n e E m e t t r i c e ) 166 t h i s. elementAt ( i ) . r e c e v o i r T r a m e ( t ) ;

167 }

168

169 public S t r i n g t o S t r i n g C o m p l e t ( ) {

170 S t r i n g s = " t a i l l e ␣du␣hub␣ : ␣ " + t h i s. s i z e ( ) ; 171 f o r (i n t i = 0 ; i < t h i s. nbMachines ; i ++)

172 s = s + " \n" + t h i s. elementAt ( i ) . t o S t r i n g C o m p l e t ( ) ; 173 return( s ) ;

174 }

175 } // f i n de l a c l a s s e Hub

(13)

Listing 10 – Machine.java 1 import j a v a . u t i l .∗;

2

3 public c l a s s Machine { 4 private S t r i n g nom ;

5 private S t r i n g a d r e s s e I P ; 6 private S t r i n g adresseMAC ; 7 private Hub hub ;

8 private A r r a y L i s t <Trame> t r a m e s R e c u e s ; 9 private A r r a y L i s t <Trame> t r a m e s E n v o y e e s ; 10

11 public Machine ( S t r i n g nom , S t r i n g a d r e s s e I P , S t r i n g adresseMAC ,

12 Hub hub ) {

13 t h i s. nom = nom ;

14 t h i s. a d r e s s e I P = a d r e s s e I P ; 15 t h i s. adresseMAC = adresseMAC ; 16 i f ( hub != n u l l)

17 hub . c o n n e c t e r M a c h i n e (t h i s) ;

18 t h i s. t r a m e s R e c u e s = new A r r a y L i s t <Trame > ( ) ; 19 t h i s. t r a m e s E n v o y e e s = new A r r a y L i s t <Trame > ( ) ;

20 }

21

22 public Machine ( ) { 23 t h i s( " " , " " , " " ,n u l l) ;

24 }

25

26 public Machine ( S t r i n g nom , S t r i n g a d r e s s e I P , S t r i n g adresseMAC ) { 27 t h i s(nom , a d r e s s e I P , adresseMAC ,n u l l) ;

28 }

29

30 public Machine ( Machine m) {

31 t h i s(m. nom ,m. a d r e s s e I P ,m. adresseMAC ,m. hub ) ;

32 }

33

34 public void i n i t ( ) {

35 S c a n n e r s c = new S c a n n e r ( System . i n ) ; 36 System . o u t . p r i n t ( "nom␣ ? ␣ : ␣ " ) ;

37 t h i s. nom = s c . n e x t L i n e ( ) ;

38 System . o u t . p r i n t ( " a d r e s s e ␣ IP ␣ ? ␣ : ␣ " ) ; 39 t h i s. a d r e s s e I P = s c . n e x t L i n e ( ) ;

40 System . o u t . p r i n t ( " a d r e s s e ␣MAC␣ ? ␣ : ␣ " ) ; 41 t h i s. adresseMAC = s c . n e x t L i n e ( ) ;

42 }

43

44 public S t r i n g getNom ( ) { 45 return(t h i s. nom ) ;

46 }

47

48 public S t r i n g g e t A d r e s s e I P ( ) { 49 return(t h i s. a d r e s s e I P ) ;

50 }

51

52 public S t r i n g getAdresseMAC ( ) {

(14)

53 return(t h i s. adresseMAC ) ;

54 }

55

56 public void setHub ( Hub hub ) { 57 t h i s. hub = hub ;

58 }

59

60 public boolean e s t C o n n e c t e e ( ) { 61 return(t h i s. hub != n u l l) ;

62 }

63

64 // r e t o u r n e un t a b l e a u de q u a t r e e n t i e r s , chacun d ’ eux c o r r e s p o n d a n t 65 // à un nombre de l ’ a d r e s s e IP de l a machine

66 public i n t[ ] g e t T a b l e a u I P ( ) { 67 i n t t a b [ ] = new i n t[ 4 ] ;

68 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r (t h i s. a d r e s s e I P , " . " ) ; 69 f o r (i n t i = 0 ; i < 4 ; i ++)

70 t a b [ i ] = I n t e g e r . p a r s e I n t ( s t . nextToken ( ) ) ; 71 return( t a b ) ;

72 }

73

74 // r e t o u r n e A, B ou C s e l o n que l ’ a d r e s s e IP de l a machine 75 // e s t de c l a s s e A, B ou C

76 public char g e t C l a s s e ( ) {

77 i n t t a b [ ] = t h i s. g e t T a b l e a u I P ( ) ; 78 i n t p r e m i e r = t a b [ 0 ] ;

79 i f ( p r e m i e r < 1 2 7 ) 80 return( ’A ’ ) ;

81 i f ( ( p r e m i e r > 1 2 7 ) && ( p r e m i e r < 1 9 2 ) ) 82 return( ’B ’ ) ;

83 return( ’C ’ ) ;

84 }

85

86 // c a l c u l e l ’ a d r e s s e IP du r é s e a u à p a r t i r de c e l l e de l a machine 87 public S t r i n g g e t I P R e s e a u ( ) {

88 S t r i n g a d r e s s e I P R e s e a u = " " ; 89 char c l a s s e = t h i s. g e t C l a s s e ( ) ;

90 S t r i n g T o k e n i z e r s t = new S t r i n g T o k e n i z e r (t h i s. a d r e s s e I P , " . " ) ; 91 switch ( c l a s s e ) {

92 case ’A ’ : a d r e s s e I P R e s e a u = s t . nextToken ( ) + " . 0 . 0 . 0 " ;

93 break;

94 case ’B ’ : a d r e s s e I P R e s e a u = s t . nextToken ( ) + " . " +

95 s t . nextToken ( ) + " . 0 . 0 " ;

96 break;

97 case ’C ’ : a d r e s s e I P R e s e a u = s t . nextToken ( ) + " . " + 98 s t . nextToken ( ) + " . " + s t . nextToken ()+ " . 0 " ;

99 break;

100 }

101 return( a d r e s s e I P R e s e a u ) ;

102 }

103

104 // permet de s a v o i r s i l a machine f o u r n i s s a n t l a méthode

105 // a p p a r t i e n t au même r é s e a u que l a machine p a s s é e en p a r a m è t r e 106 public boolean e s t C o m p a t i b l e ( Machine m) {

(15)

107 return(t h i s. g e t I P R e s e a u ( ) . e q u a l s (m. g e t I P R e s e a u ( ) ) ) ;

108 }

109

110 public S t r i n g t o S t r i n g ( ) {

111 return(new S t r i n g (t h i s. nom + " ; " + t h i s. a d r e s s e I P + " ; " + 112 t h i s. adresseMAC ) ) ;

113 }

114

115 public S t r i n g t o S t r i n g C o m p l e t ( ) {

116 S t r i n g s = "nom␣ : ␣ " + t h i s. nom + " \n" ;

117 s = s + " a d r e s s e ␣ IP ␣ : ␣ " + t h i s. a d r e s s e I P + " \n" ;

118 s = s + " a d r e s s e ␣Mac␣ : ␣ " + t h i s. getAdresseMAC ( ) + " \n" ; 119 s = s +" t r a m e s ␣ r e ç u e s ␣ : ␣ " + t h i s. t r a m e s R e c u e s + " \n" ; 120 s = s +" t r a m e s ␣ e n v o y é e s ␣ : ␣ " + t h i s. t r a m e s E n v o y e e s+ " \n" ; 121 return( s ) ;

122 }

123

124 public boolean e q u a l s ( O b j e c t o ) { 125 i f ( ! ( o i n s t a n ce o f Machine ) ) 126 return f a l s e;

127 Machine m = ( Machine ) o ;

128 return(t h i s. nom . e q u a l s (m. nom) &&

129 t h i s. a d r e s s e I P . e q u a l s (m. a d r e s s e I P ) &&

130 t h i s. adresseMAC . e q u a l s (m. adresseMAC ) ) ;

131 }

132

133 // r e n v o i e −1 s i l a machine a une a d r e s s e IP p l u s p e t i t e 134 // que c e l l e de l a machine p a s s é e en paramètre ,

135 // 1 s i e l l e e s t p l u s g r a n d e e t 0 s i l e s deux machines o n t 136 // l a même a d r e s s e IP

137 public i n t compareTo ( Machine m) { 138 i n t t a b 1 [ ] = t h i s. g e t T a b l e a u I P ( ) ; 139 i n t t a b 2 [ ] = m. g e t T a b l e a u I P ( ) ; 140 i n t i = 0 ;

141 while ( ( i < 4 ) && t a b 1 [ i ] == t a b 2 [ i ] )

142 i ++;

143 i f ( i == 4 )

144 return( 0 ) ;

145 i f ( t a b 1 [ i ] < t a b 2 [ i ] ) 146 return(−1 ) ;

147 return( 1 ) ;

148 }

149

150 // c o n n e c t e l a machine au hub p a s s é en p a r a m è t r e 151 public boolean connecterAuHub ( Hub hub ) {

152 i f ( hub . c o n t a i n s (t h i s) ) 153 return(f a l s e) ;

154 t h i s. debrancherDuHub ( ) ;

155 return( hub . c o n n e c t e r M a c h i n e (t h i s) ) ;

156 }

157

158 // d é b r a n c h e l a machine du hub a u q u e l e l l e e s t b r a n c h é e 159 public boolean debrancherDuHub ( ) {

160 i f (t h i s. e s t C o n n e c t e e ( ) )

(16)

161 return t h i s. hub . d e b r a n c h e r M a c h i n e (t h i s) ; 162 e l s e return(f a l s e) ;

163 }

164

165 // l a machine f a b r i q u e l a trame t à p a r t i r d e s d o n n é e s 166 // p u i s demande au hub a u q u e l e l l e e s t b r a n c h é e d ’ é m e t t r e 167 // l a trame v e r s l e s a u t r e s machines c o n n e c t é e s au hub . 168 // La trame e s t r a j o u t é e à l a l i s t e d e s t r a m e s e n v o y é e s 169 public boolean emettreTrame ( S t r i n g DA, S t r i n g EType , 170 Donnees d o n n e e s ) {

171 i f ( !t h i s. e s t C o n n e c t e e ( ) ) 172 return(f a l s e) ;

173 Trame t = new Trame (DA,t h i s. getAdresseMAC ( ) , EType , d o n n e e s ) ; 174 System . o u t . p r i n t l n ( " l a ␣ machine ␣ " + t h i s. nom +

175 " ␣ emet ␣ v e r s ␣ l e ␣hub␣ l a ␣ trame ␣ : ␣ " + t ) ; 176 t h i s. hub . emettreTrame (t h i s, t ) ;

177 t h i s. t r a m e s E n v o y e e s . add ( t ) ; 178 return(true) ;

179 }

180

181 // l a machine a f f i c h e l a trame que l u i a e n v o y é l e hub 182 // La trame e s t r a j o u t é e à l a l i s t e d e s t r a m e s r e ç u e s 183 public void r e c e v o i r T r a m e ( Trame t ) {

184 System . o u t . p r i n t l n ( " trame ␣ r e ç u e ␣ par ␣ l a ␣ machine ␣ " + 185 t h i s. nom + " ␣ : ␣ " + t ) ;

186 t h i s. t r a m e s R e c u e s . add ( t ) ;

187 }

188 } // f i n de l a c l a s s e Machine

Listing 11 – Routeur.java 1 import j a v a . u t i l .∗;

2

3 public c l a s s Routeur extends Machine { 4 private S t r i n g a d r e s s e I P 2 ;

5 private S t r i n g adresseMAC2 ; 6 private Hub hub2 ;

7

8 public Routeur ( ) { 9 super( ) ;

10 t h i s. a d r e s s e I P 2 = " " ; 11 t h i s. adresseMAC2 = " " ;

12 }

13

14 public Routeur ( S t r i n g nom , S t r i n g a d r e s s e I P , S t r i n g adresseMAC , 15 S t r i n g a d r e s s e I P 2 , S t r i n g adresseMAC2 , Hub hub , Hub hub2 ) { 16 super(nom , a d r e s s e I P , adresseMAC , hub ) ;

17 t h i s. a d r e s s e I P 2 = a d r e s s e I P 2 ; 18 t h i s. adresseMAC2 = adresseMAC2 ; 19 t h i s. setHub2 ( hub2 ) ;

20 }

21

22 public Routeur ( S t r i n g nom , S t r i n g a d r e s s e I P , S t r i n g adresseMAC , 23 S t r i n g a d r e s s e I P 2 , S t r i n g adresseMAC2 ) {

24 t h i s( nom , a d r e s s e I P , adresseMAC , a d r e s s e I P 2 , adresseMAC2 ,null,n u l l) ;

(17)

25 } 26

27 public Routeur ( Machine m, S t r i n g a d r e s s e I P 2 , S t r i n g adresseMAC2 , Hub hub2 ) { 28 super(m) ;

29 t h i s. a d r e s s e I P 2 = a d r e s s e I P 2 ; 30 t h i s. adresseMAC2 = adresseMAC2 ; 31 t h i s. setHub2 ( hub2 ) ;

32 }

33

34 public Routeur ( Routeur r ) { 35 super( r ) ;

36 t h i s. a d r e s s e I P 2 = r . a d r e s s e I P 2 ; 37 t h i s. adresseMAC2 = r . adresseMAC2 ; 38 t h i s. setHub2 ( r . hub2 ) ;

39 }

40

41 public void i n i t ( ) { 42 super. i n i t ( ) ;

43 S c a n n e r s c = new S c a n n e r ( System . i n ) ;

44 System . o u t . p r i n t ( " deuxième ␣ a d r e s s e ␣ IP ␣ ? ␣ : ␣ " ) ; 45 t h i s. a d r e s s e I P 2 = s c . n e x t ( ) ;

46 System . o u t . p r i n t ( " deuxième ␣ a d r e s s e ␣MAC␣ ? ␣ : ␣ " ) ; 47 t h i s. adresseMAC2 = s c . n e x t ( ) ;

48 }

49

50 public S t r i n g g e t A d r e s s e I P 2 ( ) { 51 return(t h i s. a d r e s s e I P 2 ) ;

52 }

53

54 public S t r i n g getAdresseMAC2 ( ) { 55 return(t h i s. adresseMAC2 ) ;

56 }

57

58 public void setHub2 ( Hub hub ) { 59 t h i s. hub2 = hub ;

60 }

61

62 // r e t o u r n e un t a b l e a u de q u a t r e e n t i e r s , chacun de c e s 63 // e n t i e r s c o r r e s p o n d a n t à un nombre de l a deuxième 64 // a d r e s s e IP du r o u t e u r

65 public i n t[ ] g e t T a b l e a u I P 2 ( ) {

66 return(new Machine ( " " ,t h i s. a d r e s s e I P 2 ,t h i s. adresseMAC2 ) . 67 g e t T a b l e a u I P ( ) ) ;

68 }

69

70 // r e t o u r n e A, B ou C s e l o n que l a deuxième a d r e s s e IP 71 // du r o u t e u r e s t de c l a s s e A, B ou C .

72 public char g e t C l a s s e 2 ( ) {

73 return(new Machine ( " " ,t h i s. a d r e s s e I P 2 ,t h i s. adresseMAC2 ) . 74 g e t C l a s s e ( ) ) ;

75 }

76

77 public S t r i n g g e t I P R e s e a u 2 ( ) {

78 return(new Machine ( " " ,t h i s. a d r e s s e I P 2 ,t h i s. adresseMAC2 ) .

(18)

79 g e t I P R e s e a u ( ) ) ;

80 }

81

82 // permet de s a v o i r s i l e r o u t e u r f o u r n i s s a n t l a méthode

83 // a p p a r t i e n t au même r é s e a u que l a machine p a s s é e en p a r a m è t r e 84 public boolean e s t C o m p a t i b l e ( Machine m) {

85 return(super. e s t C o m p a t i b l e (m) | |

86 new Machine ( " " ,t h i s. a d r e s s e I P 2 ,t h i s. adresseMAC2 ) . 87 e s t C o m p a t i b l e (m) ) ;

88 }

89

90 public S t r i n g t o S t r i n g ( ) {

91 return (super. t o S t r i n g ()+ " ; " + t h i s. a d r e s s e I P 2 + " ; " + 92 t h i s. adresseMAC2 ) ;

93 }

94

95 public boolean e q u a l s ( O b j e c t o ) { 96 i f ( ! ( o i n s t a n ce o f Routeur ) ) 97 return f a l s e;

98 Routeur r = ( Routeur ) o ;

99 return(super. e q u a l s ( o ) &&

100 t h i s. a d r e s s e I P 2 . e q u a l s ( r . a d r e s s e I P 2 ) &&

101 t h i s. adresseMAC2 . e q u a l s ( r . adresseMAC2 ) ) ;

102 }

103

104 // r e n v o i e −1 s i l e r o u t e u r a l a p l u s p e t i t e d e s q u a t r e a d r e s s e s IP , 105 // 1 s i c ’ e s t l e r o u t e u r r e ç u en p a r a m è t r e q u i a l a p l u s p e t i t e d e s 106 // q u a t r e a d r e s s e s IP , e t 0 s i l e s q u a t r e a d r e s s e s IP s o n t é g a l e s 107 public i n t compareTo ( Routeur r ) {

108 i n t tabMin1 [ ] ; 109 i n t tabMin2 [ ] ;

110 i n t t a b 1 [ ] = t h i s. g e t T a b l e a u I P ( ) ; 111 i n t t a b 2 [ ] = t h i s. g e t T a b l e a u I P 2 ( ) ; 112 i n t t a b 3 [ ] = r . g e t T a b l e a u I P ( ) ; 113 i n t t a b 4 [ ] = r . g e t T a b l e a u I P 2 ( ) ;

114 // r e c h e r c h e de l a p l u s p e t i t e a d r e s s e IP de t h i s 115 i n t i = 0 ;

116 while ( ( i < 4 ) && ( t a b 1 [ i ] == t a b 2 [ i ] ) )

117 i ++;

118 i f ( ( i == 4 ) | | ( t a b 1 [ i ] < t a b 2 [ i ] ) )

119 tabMin1 = t a b 1 ;

120 e l s e tabMin1 = t a b 2 ;

121 // r e c h e r c h e de l a p l u s p e t i t e a d r e s s e IP de r

122 i = 0 ;

123 while ( ( i < 4 ) && ( t a b 3 [ i ] == t a b 4 [ i ] ) )

124 i ++;

125 i f ( ( i == 4 ) | | ( t a b 3 [ i ] < t a b 4 [ i ] ) )

126 tabMin2 = t a b 3 ;

127 e l s e tabMin2 = t a b 4 ;

128 // c o m p a r a is o n d e s deux tabMin

129 i = 0 ;

130 while ( ( i < 4 ) && ( tabMin1 [ i ] == tabMin2 [ i ] ) )

131 i ++;

132 i f ( i == 4 )

(19)

133 return( 0 ) ;

134 i f ( tabMin1 [ i ] < tabMin2 [ i ] ) 135 return(−1 ) ;

136 e l s e return( 1 ) ;

137 }

138 } // f i n de l a c l a s s e Routeur

Listing 12 – TestMenu.java 1 import j a v a . u t i l . S c a n n e r ;

2

3 public c l a s s TestMenu {

4 public s t a t i c void main ( S t r i n g [ ] a r g s ) {

5 Hub hub = new Hub ( 5 ) ;

6 Machine [ ] t a b M ac h i n e s = new Machine [ 5 ] ; 7 i n t r e p o n s e , p o s i t i o n ;

8 i n t nbMachines ; // nombre de machines dans l e t a b l e a u du main 9 S c a n n e r s c = new S c a n n e r ( System . i n ) ;

10 System . o u t . p r i n t l n (

11 " 1/ ␣ i n i t i a l i s e r ␣ l e ␣hub␣ v i d e ␣ e t ␣ u t i l i s e r ␣ d e s ␣ mach ine s ␣ p r é d é f i n i e s " ) ; 12 System . o u t . p r i n t l n ( " 2/ ␣ i n i t i a l i s e r ␣ l e ␣hub␣ à ␣ p a r t i r ␣ de ␣ l a ␣ méthode ␣ i n i t " ) ;

13 do {

14 System . o ut . p r i n t ( " v o t r e ␣ c h o i x ␣ ? ␣ : ␣ " ) ; 15 r e p o n s e=s c . n e x t I n t ( ) ;

16 } while ( ( r e p o n s e < 1 ) | | ( r e p o n s e > 2 ) ) ; 17 i f ( r e p o n s e ==1){

18 // on u t i l i s e 5 machines p r é d é f i n i e s

19 t a b M ac h i n e s [ 0 ] = new Machine ( " m e n e l a s " , " 1 0 . 1 0 . 1 0 . 1 " , " 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 1 " ) ; 20 t a b M ac h i n e s [ 1 ] = new Machine ( " p o l u x " , " 1 0 . 1 0 . 1 0 . 2 " , " 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 2 " ) ; 21 t a b M ac h i n e s [ 2 ] = new Machine ( " a j a x " , " 1 0 . 1 0 . 1 0 . 3 " , " 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 3 " ) ; 22 t a b M ac h i n e s [ 3 ] = new Machine ( " e n e e " , " 1 0 . 1 0 . 1 0 . 4 " , " 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 4 " ) ; 23 t a b M ac h i n e s [ 4 ] = new Machine ( " c r e s u s " , " 1 0 . 1 0 . 1 0 . 5 " , " 0 1 : 0 1 : 0 1 : 0 1 : 0 1 : 0 5 " ) ;

24 nbMachines = 5 ;

25 } e l s e {

26 // s i n o n , on f a i t c o n s t r u i r e l e r é s e a u à l ’ u t i l i s a t e u r 27 // en commençant p a r c r é e r un hub

28 hub . i n i t ( ) ;

29 f o r (i n t i = 0 ; i < hub . getNbMachines ( ) ; i ++) 30 t ab M a c h i n e s [ i ] = hub . elementAt ( i ) ;

31 nbMachines = hub . getNbMachines ( ) ;

32 }

33 do {

34 System . o ut . p r i n t l n ( " 1/ ␣ t e s t e r ␣ s i ␣ l e ␣hub␣ e s t ␣ v i d e ␣ " ) ; 35 System . o ut . p r i n t l n ( " 2/ ␣ t e s t e r ␣ s i ␣ l e ␣hub␣ e s t ␣ p l e i n ␣ " ) ; 36 System . o ut . p r i n t l n (

37 " 3/ ␣ t e s t e r ␣ s i ␣ une ␣ machine ␣ donnée ␣ e s t ␣ b r a n c h é e ␣ s u r ␣ l e ␣hub" ) ; 38 System . o ut . p r i n t l n (

39 " 4/ ␣ b r a n c h e r ␣ une ␣ machine ␣ à ␣ p a r t i r ␣ de ␣ l a ␣ c l a s s e ␣ Machine " ) ; 40 System . o ut . p r i n t l n (

41 " 5/ ␣ b r a n c h e r ␣ une ␣ machine ␣ à ␣ p a r t i r ␣ de ␣ l a ␣ c l a s s e ␣Hub" ) ; 42 System . o ut . p r i n t l n (

43 " 6/ ␣ c r é e r ␣ e t ␣ c o n n e c t e r ␣ une ␣ n o u v e l l e ␣ machine ␣ à ␣ p a r t i r ␣ de ␣ l a ␣ c l a s s e ␣ Machine " ) ; 44 System . o ut . p r i n t l n (

45 " 7/ ␣ d é b r a n c h e r ␣ une ␣ machine ␣ à ␣ p a r t i r ␣ de ␣ l a ␣ c l a s s e ␣ Machine " ) ; 46 System . o ut . p r i n t l n (

(20)

47 " 8/ ␣ d é b r a n c h e r ␣ une ␣ machine ␣ à ␣ p a r t i r ␣ de ␣ l a ␣ c l a s s e ␣Hub" ) ; 48 System . o ut . p r i n t l n (

49 " 9/ ␣ d é b r a n c h e r ␣ une ␣ machine ␣d ’ un␣ p o r t ␣ p r é c i s ␣du␣hub" ) ; 50 System . o ut . p r i n t l n ( " 10/ ␣ é m e t t r e ␣ une ␣ trame ␣ s u r ␣ l e ␣ r é s e a u " ) ; 51 System . o ut . p r i n t l n ( " 11/ ␣ a f f i c h e r ␣ l e ␣ c o n t e n u ␣du␣hub␣ " ) ; 52 System . o ut . p r i n t l n ( " 0/ ␣ t e r m i n e r ␣ : ␣ " ) ;

53 do {

54 System . o u t . p r i n t ( " v o t r e ␣ c h o i x ␣ ? ␣ : ␣ " ) ; 55 r e p o n s e = s c . n e x t I n t ( ) ;

56 } while ( ( r e p o n s e < 0 ) | | ( r e p o n s e > 1 1 ) ) ; 57 switch ( r e p o n s e ) {

58 case 1 : i f ( hub . e s t V i d e ( ) )

59 System . o u t . p r i n t l n ( "hub␣ v i d e " ) ;

60 e l s e System . ou t . p r i n t l n ( "hub␣ non ␣ v i d e " ) ;

61 break;

62 case 2 : i f ( hub . e s t P l e i n ( ) )

63 System . o u t . p r i n t l n ( "hub␣ p l e i n " ) ;

64 e l s e System . ou t . p r i n t l n ( "hub␣ non ␣ p l e i n " ) ;

65 break;

66 case 3 : do {

67 System . o u t . p r i n t (

68 " p o s i t i o n ␣ de ␣ l a ␣ machine ␣ dans ␣ l e ␣ t a b l e a u ␣ l o c a l ␣ ? ␣ : ␣ " ) ;

69 p o s i t i o n = s c . n e x t I n t ( ) ;

70 } while ( ( p o s i t i o n < 0 ) | | ( p o s i t i o n > nbMachines − 1 ) ) ; 71 i f ( hub . c o n t a i n s ( t a b M a ch i n e s [ p o s i t i o n ] ) )

72 System . o u t . p r i n t l n ( " l a ␣ machine ␣ e s t ␣ b r a n c h é e ␣ s u r ␣ l e ␣hub" ) ; 73 e l s e System . ou t . p r i n t l n (

74 " l a ␣ machine ␣n ’ e s t ␣ pas ␣ b r a n c h é e ␣ s u r ␣ l e ␣hub" ) ;

75 break;

76 case 4 : do {

77 System . o u t . p r i n t (

78 " p o s i t i o n ␣ de ␣ l a ␣ machine ␣ dans ␣ l e ␣ t a b l e a u ␣ l o c a l ␣ ? ␣ : ␣ " ) ;

79 p o s i t i o n = s c . n e x t I n t ( ) ;

80 } while ( ( p o s i t i o n < 0 ) | | ( p o s i t i o n > nbMachines − 1 ) ) ; 81 i f ( t a b M a c h in e s [ p o s i t i o n ] . connecterAuHub ( hub ) )

82 System . o u t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ r é u s s i " ) ; 83 e l s e System . ou t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ é c h o u é " ) ;

84 break;

85 case 5 : do {

86 System . o u t . p r i n t (

87 " p o s i t i o n ␣ de ␣ l a ␣ machine ␣ dans ␣ l e ␣ t a b l e a u ␣ l o c a l ␣ ? ␣ : ␣ " ) ;

88 p o s i t i o n = s c . n e x t I n t ( ) ;

89 } while ( ( p o s i t i o n < 0 ) | | ( p o s i t i o n > nbMachines − 1 ) ) ; 90 i f ( hub . c o n n e c t e r M a c h i n e ( t a b M ac h i n e s [ p o s i t i o n ] ) )

91 System . o u t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ r é u s s i " ) ; 92 e l s e System . ou t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ é c h o u é " ) ;

93 break;

94 case 6 : i f ( nbMachines == t ab M a c h i n e s . l e n g t h ) 95 System . o u t . p r i n t l n ( " t a b l e a u ␣ p l e i n " ) ;

96 e l s e {

97 Machine m = new Machine ( ) ;

98 m. i n i t ( ) ;

99 i f (m. connecterAuHub ( hub ) ) {

100 System . o u t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ r é u s s i " ) ;

(21)

101 t ab M a c h i n e s [ nbMachines ] = m;

102 nbMachines++;

103 } e l s e System . o u t . p r i n t l n ( " l a ␣ c o n n e x i o n ␣ a ␣ é c h o u é " ) ;

104 }

105 break;

106 case 7 : do {

107 System . o ut . p r i n t (

108 " p o s i t i o n ␣ de ␣ l a ␣ machine ␣ dans ␣ l e ␣ t a b l e a u ␣ l o c a l ␣ ? ␣ : ␣ " ) ;

109 p o s i t i o n = s c . n e x t I n t ( ) ;

110 } while ( ( p o s i t i o n < 0 ) | | ( p o s i t i o n > nbMachines − 1 ) ) ; 111 i f ( t a b M a c h in e s [ p o s i t i o n ] . debrancherDuHub ( ) )

112 System . o ut . p r i n t l n ( " l a ␣ machine ␣ a ␣ é t é ␣ d é b r a n c h é e " ) ; 113 e l s e System . ou t . p r i n t l n ( " pr o bl èm e " ) ;

114 break;

115 case 8 : do {

116 System . o ut . p r i n t (

117 " p o s i t i o n ␣ de ␣ l a ␣ machine ␣ dans ␣ l e ␣ t a b l e a u ␣ l o c a l ␣ ? ␣ : ␣ " ) ;

118 p o s i t i o n=s c . n e x t I n t ( ) ;

119 } while ( ( p o s i t i o n < 0 ) | | ( p o s i t i o n > nbMachines − 1 ) ) ; 120 i f ( hub . d e b r a n c h e r M a c h i n e ( ta b M a c h i n es [ p o s i t i o n ] ) )

121 System . o ut . p r i n t l n ( " l a ␣ machine ␣ a ␣ é t é ␣ d é b r a n c h é e " ) ; 122 e l s e System . ou t . p r i n t l n ( " pr o bl èm e " ) ;

123 break;

124 case 9 : System . o u t . p r i n t ( " p o s i t i o n ␣du␣ p o r t ␣ ? ␣ : ␣ " ) ;

125 p o s i t i o n = s c . n e x t I n t ( ) ;

126 i f ( hub . d e b r a n c h e r M a c h i n e ( p o s i t i o n ) )

127 System . o ut . p r i n t l n ( " l a ␣ machine ␣ a ␣ é t é ␣ d é b r a n c h é e " ) ; 128 e l s e System . ou t . p r i n t l n ( " pr o bl èm e " ) ;

129 break;

130 case 10 : System . o u t . p r i n t (

131 " numéro ␣ de ␣ p o r t ␣ de ␣ l a ␣ machine ␣ e m e t t r i c e ␣ : ␣ " ) ;

132 p o s i t i o n = s c . n e x t I n t ( ) ;

133 i f ( hub . e s t O c c u p e ( p o s i t i o n ) ) {

134 System . o ut . p r i n t ( " a d r e s s e ␣MAC␣ p o s t e ␣ d e s t i n a t i o n ␣ : ␣ " ) ;

135 S t r i n g adresseMAC = s c . n e x t ( ) ;

136 System . o ut . p r i n t ( " message ␣ : ␣ " ) ;

137 S t r i n g message = s c . n e x t ( ) ;

138 Donnees d o n n e e s = new Donnees ( message ) ;

139 hub . elementAt ( p o s i t i o n ) .

140 emettreTrame ( donnees , adresseMAC , " e t y p e " ) ;

141 } e l s e System . o u t . p r i n t l n (

142 " aucune ␣ machine ␣ c o n n e c t é e ␣ s u r ␣ c e ␣ p o r t " ) ;

143 break;

144 case 11 : System . o u t . p r i n t l n ( hub ) ;

145 break;

146 } // f i n s w i t c h

147 } while ( r e p o n s e != 0 ) ; 148 } // f i n main

149 } // f i n de l a c l a s s e TestMenu

(22)

2 Classes abstraites

Exercice 2.1 : Analyse de trame dans la classe Machine

Rappel du format d’une trame Ethernet II :

Préambule SFD DA SA EType données Bourrage FCS

7o 1o 6o 6o 2o 0–1500o 0–46o 4o

Dans ce qui suit, nous ne considérons ni le préambule, ni le SFD, ni le FCS. Nous nous affranchirons aussi de la nécessité de rajouter du bourrage dans le cas d’une longueur de trame inférieure à64octets.

Le type des données (LPDU) est déterminé par leEType. Il peut s’agir par exemple de données de type ARP, IP, . . .

Dans ces conditions, on pourrait définir le type de trame par : 1 public c l a s s Trame{

2 private S t r i n g DA;

3 private S t r i n g SA ; 4 private S t r i n g EType ;

5 private DonneesARP d o n n e e s ;

6 . . .

7 }

ou bien

1 public c l a s s Trame{

2 private S t r i n g DA;

3 private S t r i n g SA ; 4 private S t r i n g EType ; 5 private DonneesIP d o n n e e s ;

6 . . .

7 }

ou encore d’autres, avec d’autres types de données.

De plus, certains types de données peuvent être considérés commeterminaux (par exemple les données ARP), tandis que d’autres (par exemple IP) contiennent eux aussi des données dont le type est déterminé par la valeur du champ protocole. Si le paquet IP contient des données de type UDP par exemple, ces données contiennent elles-mêmes des données dont le type sera fixé soit par la valeur du port source soit par celle du port destination.

Pour remédier à ce problème, on définit uneclasse abstraiteDonneesdont hériteront les classes réellesDonneesARP, DonneesIP, . . .

Donnees

DonneesARP DonneesICMP DonneesIP DonneesUDP Trame

(23)

La classe Tramedevient alors : 1 public c l a s s Trame{

2 private S t r i n g DA;

3 private S t r i n g SA ; 4 private S t r i n g EType ; 5 private Donnees d o n n e e s ;

6 . . .

7 }

Lors de l’appel au constructeur de la classeTrame, les données seront du bon type : DonneesARP, DonneesIP, . . .

Pour résoudre le problème des classesnon terminales, la classeDonneescontiendra une variable d’instance nomméedonneeNiveauSuperieurqui sera de typeDonnees:

1 public abstract c l a s s Donnees {

2 private Donnees d o n n e e N i v e a u S u p e r i e u r ;

3 . . .

4 }

En pratique, dans la classe DonneesIP, cette variable d’instance sera de typeDonneesICMPou DonneesUDPou . . .

Question 1 : Écrire un programme de test dans lequel vous instancierez un Hub et quelques Machines que vous connecterez auHub.

Question 2 : Compléter le programme en utilisant les classes de typeDonneespour simuler un ARP.requestdepuis la machine A, concernant la machine B puis la réponse de la machine B à la machine A.

Question 3 :Simuler unpingentre les machines C et D (ECHO.requestsuivi deECHO.reply).

Question 4 :Rajouter à la classeMachineune méthode :

public void e n v o y e r A r p R e q u e s t ( S t r i n g a d r e s s e I P D e s t i n a t i o n ) throws E x c e p t i o n

Question 5 :Rajouter à la classeMachineune méthode :

public void e n v o y e r E c h o R e q u e s t ( S t r i n g a d r e s s e I P D e s t i n a t i o n , S t r i n g a d r e s s e M A C D e s t i n a t i o n ) throws E x c e p t i o n

Question 6 :Modifier le classeMachineafin que celle-ci devienne capable d’analyser les trames qu’elle reçoit et qu’éventuellement elle y réponde. Rédiger la méthode analyserARP : lorsque la machine reçoit unARP.requestet que c’est elle qui est concernée, elle répond par unARP.reply; lorsquelle reçoit unARP.reply et qu’elle est destinatrice de la trame, elle affiche l’adresse MAC recherchée.

La méthoderecevoirTramedevient : 1 public void r e c e v o i r T r a m e ( Trame t ) {

2 System . o u t . p r i n t l n ( "Trame␣ r e ç u e ␣ par ␣ l a ␣ machine " + t h i s. nom + " : " + t ) ; 3 t h i s. t r a m e s R e c u e s . add ( t ) ;

4 try {

5 t h i s. A n a l y s e r T r a m e E t h e r n e t ( t ) ;

6 } catch( E x c e p t i o n e ) { System . o u t . p r i n t l n ( e ) ; }

7 }

(24)

La méthodeanalyserTrameEthernetse base sur leEType de la trame : 1 public void a n a l y s e r T r a m e E t h e r n e t ( Trame t ) throws E x c e p t i o n { 2 S t r i n g eType = t . getEType ( ) ;

3 i f ( eType . e q u a l s ( " 0806 " ) ) { // ARP

4 DonneesARP donneesARP = ( DonneesARP ) t . g e t D o n n e e s ( ) ; 5 t h i s. analyserARP ( donneesARP ) ;

6 }

7 i f ( eType . e q u a l s ( " 0800 " ) ) { // IP

8 DonneeIP donneeIP = ( DonneesIP ) t . g e t D o n n e e s ( ) ; 9 t h i s. a n a l y s e r I P ( donneesIP , t . getSA ( ) ) ;

10 }

11 . . .

12 }

(25)

Class Donnees

java.lang.Object Donnees

Direct Known Subclasses:

DonneesARP, DonneesICMP, DonneesIP, DonneesUDP

public abstract class Donnees extends java.lang.Object

Field Summary

Modifier and Type Field and Description private Donnees donneesNiveauSuperieur

Constructor Summary

Constructor and Description Donnees()

Donnees(Donnees donneesNiveauSuperieur)

Method Summary

Modifier and Type Method and Description boolean equals(java.lang.Object o)

Donnees getDonneesNiveauSuperieur()

Methods inherited from class java.lang.Object :

clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

(26)

Class DonneesARP

java.lang.Object Donnees

DonneesARP

3

public class DonneesARP extends Donnees

Field Summary

Modifier and Type Field and Description

private int opCode

private java.lang.String SHA

private java.lang.String SIPA

private java.lang.String THA

private java.lang.String TIPA

Constructor Summary

Constructor and Description

DonneesARP(int opCode, java.lang.String SHA, java.lang.String SIPA, java.lang.String THA, java.lang.String TIPA)

throws Exception

Method Summary

Modifier and Type Method and Description

int getOpCode()

java.lang.String getSHA()

java.lang.String getSIPA()

java.lang.String getTHA()

java.lang.String getTIPA()

java.lang.String toString()

Methods inherited from class Donnees

equals, getDonneesNiveauSuperieur

Methods inherited from class java.lang.Object

clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

(27)

Class DonneesIP

java.lang.Object Donnees

DonneesIP

public class DonneesIP extends Donnees

Field Summary

Modifier and Type Field and Description

private int protocole

private java.lang.String SIP

private java.lang.String TIP

private int version

Constructor Summary

Constructor and Description

DonneesIP(int version, int protocole, java.lang.String SIP, java.lang.String TIP, Donnees donneesNiveauSup)

throws Exception

Method Summary

Modifier and Type Method and Description

int getProtocole()

java.lang.String getSIP() java.lang.String getTIP()

int getVersion()

java.lang.String toString()

Methods inherited from class Donnees

equals, getDonneesNiveauSuperieur

Methods inherited from class java.lang.Object

clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

(28)

Class DonneesICMP

java.lang.Object Donnees

DonneesICMP

!

public class DonneesICMP extends Donnees

Field Summary

Modifier and Type Field and Description

private int code

private int type

Constructor Summary

Constructor and Description DonneesICMP(int type, int code)

Method Summary

Modifier and Type Method and Description

int getCode()

int getType()

java.lang.String toString()

Methods inherited from class Donnees

equals, getDonneesNiveauSuperieur

Methods inherited from class java.lang.Object

clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

(29)

3 Client/Serveur UDP

Exercice 3.1 : Services

Question 1 :Que contient le fichier /etc/services? À quoi sert-il ?

Question 2 :xinetdest unméta-serveur qui peut écouter sur plusieurs ports, lancer les démons associés lorsque c’est nécessaire et les arrêter quand ils ne servent plus. Regardez le contenu des fichiersecho-udp,daytime-udp,echoetdaytimedans le répertoire/etc/xinetd.d. Que peut-on en conclure quant à l’activation des ports 7 (echo) et 13 (daytime) enudpettcp? Faites en sorte que les quatre services soient activés sur votre machine en modifiant si nécessaire les fichiers de configuration.

Question 3 :Relancez xinetd: /etc/init.d/xinetd restart

Vérifez le bon fonctionnement des service 7 et 13 sur votre machine puis sur celle de votre voisin, en utilisant le clientncen UDP puis en TCP, puis avec le client TCPtelnet.

Exercice 3.2 : Programmation java Client/Serveur

Question 1 :Quelles sont les deux méthodes fondamentales que doivent fournir un client ou un serveur ?

Question 2 :En déduire une première version d’une interfaceClientServeurqui sera complétée par la suite, et implémentée par tous les clients et tous les serveurs.

Exercice 3.3 : Client/Serveur UDP

Question 1 : Rédigez une classe ClientUDP implémentant l’interface ClientServeur. Les lis- tings 21 et 22 en donnent deux exemples d’utilisation.

Listing 21 – TestClientUDPEcho.java 1 public c l a s s TestClientUDPEcho {

2 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 3 i f ( a r g s . l e n g t h != 2 ) {

4 System . o ut . p r i n t l n ( " u s a g e ␣ : ␣ j a v a ␣ TestClientUDPEcho ␣<s e r v e u r >␣<message>" ) ; 5 System . e x i t ( 0 ) ;

6 }

7 try{

8 ClientUDP c l i e n t 1 = new ClientUDP ( a r g s [ 0 ] , 7 ) ; 9 c l i e n t 1 . e n v o y e r M e s s a g e ( a r g s [ 1 ] ) ;

10 System . o ut . p r i n t l n ( c l i e n t 1 . r e c e v o i r M e s s a g e ( ) ) ;

(30)

11 c l i e n t 1 . f e r m e r ( ) ;

12 ClientUDP c l i e n t 2 = new ClientUDP ( ) ;

13 c l i e n t 2 . e n v o y e r M e s s a g e ( a r g s [ 1 ] , a r g s [ 0 ] , 7 ) ; 14 System . o ut . p r i n t l n ( c l i e n t 2 . r e c e v o i r M e s s a g e ( ) ) ; 15 c l i e n t 2 . f e r m e r ( ) ;

16 } catch( E x c e p t i o n e ) { 17 System . o ut . p r i n t l n ( e ) ;

18 }

19 }

20 } // f i n de l a c l a s s e TestClientUDPEcho

Listing 22 – TestClientUDPDaytime.java 1 public c l a s s TestClientUDPDaytime {

2 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 3 i f ( a r g s . l e n g t h != 1 ) {

4 System . o ut . p r i n t l n ( " u s a g e ␣ : ␣ j a v a ␣ TestClientUDPDaytime ␣<s e r v e u r >" ) ; 5 System . e x i t ( 0 ) ;

6 }

7 try{

8 ClientUDP c l i e n t = new ClientUDP ( a r g s [ 0 ] , 1 3 ) ; 9 c l i e n t . e n v o y e r M e s s a g e ( " " ) ;

10 System . o ut . p r i n t l n ( c l i e n t . r e c e v o i r M e s s a g e ( ) ) ; 11 c l i e n t . f e r m e r ( ) ;

12 } catch( E x c e p t i o n e ) { 13 System . o ut . p r i n t l n ( e ) ;

14 }

15 }

16 } // f i n de l a c l a s s e TestClientUDPDaytime

Question 2 :Utilisez ces programmes avec votre machine comme serveur puis avec celle de votre voisin comme serveur.

Question 3 : Rédigez la classe abstraite ServeurUDP implémentant l’interface ClientServeur puis la classeServeurUDP7777fournissant sur le port 7777 le même service queudp echosur le port 7.

Question 4 :Testez votre serveur avec votre client UDP dans deux fenêtres différentes, puis avec le client de votre voisin.

Remarque : Pour éviter les conflits sur les numéros de port, vous devez choisir un numéro de port différent de ceux déjà utilisés par le système. Il faut vérifier qu’il n’est pas déjà utilisé dans /etc/serviceset ne fait pas non plus partie des numéros attribués dynamiquement aux serveurs RPC (Remote Procedure Call), par la commanderpcinfo -p(ourpc_dump), ni déjà utiilisés par divers processus (netstat -an). Dans tous les cas choisissez un numéro supérieur à 1024.

(31)

Interface ClientServeur

All Known Implementing Classes : ClientUDP, ServeurUDP, ServeurUDP7777

public interface ClientServeur

Method Summary :

Modifier and Type Method and Description void envoyerDatas(byte[] datas) throws Exception byte[] recevoirDatas() throws Exception

(32)

C l a s s C l i e n t U D P

java.lang.Object ClientUDP

All Implemented Interfaces : ClientServeur

public class ClientUDP extends Object implements ClientServeur

Field Summary :

Modifier and Type Field and Description private InetAddress adresseIPDistante

private int portDistant

private DatagramSocket socket

Constructor Summary :

Constructor and Description ClientUDP() throws Exception

ClientUDP(String nomServeur, int portServeur) throws Exception

Method Summary :

Modifier and

Type Method and Description

void envoyerDatas(byte[] datas) throws Exception

void envoyerDatas(byte[] datas, InetAddress adresseIPDistante, int portDistant) throws Exception

void envoyerMessage(String message) throws Exception

void envoyerMessage(String message, String adresseIPDistante, int portDistant) throws Exception

void fermer()

byte[] recevoirDatas() throws Exception String recevoirMessage() throws Exception

Methods inherited from class java.lang.Object :

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

(33)

C l a s s S e r v e u r U D P

java.lang.Object ServeurUDP

All Implemented Interfaces : ClientServeur Direct Known Subclasses : ServeurUDP7777

public abstract class ServeurUDP extends Object implements ClientServeur

Field Summary :

Modifier and Type Field and Description protected InetAddress adresseIPDistante

private int portDistant

private DatagramSocket socket

Constructor Summary :

Constructor and Description ServeurUDP(int port) throws Exception

Method Summary :

Modifier and Type Method and Description void envoyerDatas(byte[] datas) throws Exception void envoyerMessage(String message) throws Exception

void fermer()

abstract void fonctionner() throws Exception

byte[] recevoirDatas() throws Exception

String recevoirMessage() throws Exception

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

(34)

C l a s s S e r v e u r U D P 7 7 7 7

java.lang.Object ServeurUDP

ServeurUDP7777 All Implemented Interfaces : ClientServeur

public class ServeurUDP7777 extends ServeurUDP

Field Summary :

Fields inherited from class ServeurUDP :

adresseIPDistante

Constructor Summary :

Constructor and Description ServeurUDP7777() throws Exception

Method Summary :

Modifier and Type Method and Description void fonctionner() throws Exception

Methods inherited from class ServeurUDP :

envoyerDatas, envoyerMessage, fermer, recevoirDatas, recevoirMessage

Methods inherited from class java.lang.Object :

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

(35)

4 Client/Serveur TCP

Exercice 4.1 : Client TCP

Question 1 : Rédigez une classe ClientTCPimplémentant l’interface ClientServeur. Contrai- rement à la classeDatagramSocket, la classe Socketutilise deux fichiers texte, l’un en entrée et l’autre en sortie. Dans le cas d’un client TCP, le fichier en sortie ira vers le serveur, celui en entrée arrivera du serveur.

Les listings 28 et 29 donnent deux exemples d’utilisation de cette classe.

Listing 28 – TestClientTCPEcho.java 1 public c l a s s TestClientTCPEcho {

2 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 3 i f ( a r g s . l e n g t h != 2 ) {

4 System . o ut . p r i n t l n ( " u s a g e ␣ : ␣ j a v a ␣ TestClientTCPEcho ␣<s e r v e u r >␣<message>" ) ; 5 System . e x i t ( 0 ) ;

6 }

7 try{

8 ClientTCP c l i e n t = new ClientTCP ( a r g s [ 0 ] , 7 ) ; 9 c l i e n t . e n v o y e r M e s s a g e ( a r g s [ 1 ] ) ;

10 System . o ut . p r i n t l n ( c l i e n t . r e c e v o i r M e s s a g e ( ) ) ; 11 c l i e n t . e n v o y e r M e s s a g e ( " b i s ␣ : ␣ " + a r g s [ 1 ] ) ; 12 System . o ut . p r i n t l n ( c l i e n t . r e c e v o i r M e s s a g e ( ) ) ; 13 c l i e n t . f e r m e r ( ) ;

14 } catch( E x c e p t i o n e ) { 15 System . o ut . p r i n t l n ( e ) ;

16 }

17 }

18 } // f i n de l a c l a s s e TestClientTCPEcho

Listing 29 – TestClientTCPDaytime.java 1 public c l a s s TestClientTCPDaytime {

2 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 3 i f ( a r g s . l e n g t h != 1 ) {

4 System . o ut . p r i n t l n ( " u s a g e ␣ : ␣ j a v a ␣ TestClientTCPDaytime ␣<s e r v e u r >" ) ; 5 System . e x i t ( 0 ) ;

6 }

7 try{

8 ClientTCP c l i e n t = new ClientTCP ( a r g s [ 0 ] , 1 3 ) ; 9 c l i e n t . e n v o y e r M e s s a g e ( " " ) ;

10 System . o ut . p r i n t l n ( c l i e n t . r e c e v o i r M e s s a g e ( ) ) ; 11 c l i e n t . f e r m e r ( ) ;

12 } catch( E x c e p t i o n e ) { 13 System . o ut . p r i n t l n ( e ) ;

14 }

15 }

16 } // f i n de l a c l a s s e TestClientTCPDaytime

(36)

Question 2 :Utilisez ces programmes avec votre machine comme serveur puis avec celle de votre voisin comme serveur.

Question 3 :Utilisez les clients TCP telnetouncpour :

• récupérer le contenu d’un fichier d’extension.htmlde votre machine puis de la machine de votre voisin ;

• récupérer la réponse du scriptphpdu listing exécuté sur votre machine puis sur celle de votre voisin.

1 < s c r i p t l a n g u a g e="php">

2 i f (i s s e t($_GET[ ’ x ’ ] ) )

3 print($_GET[ ’ x ’ ] ∗ $_GET[ ’ x ’ ] ) ; 4 e l s e print( "pb␣ p a r a m è t r e s " ) ; 5 </ s c r i p t >

Question 4 : Rédigez une classe ClientTCPWeb permettant d’envoyer des requêtes http à un serveur web et d’afficher sa réponse.

Question 5 :Utilisez une instance de cette classe pour faire la même chose qu’à la question 3.

Exercice 4.2 : Serveur TCP

Question 1 :Rédigez une classeMultiServeurTCPEcho. Son constructeur recevra en paramètre un numéro de port (supérieur à 1024) sur lequel le serveur sera en écoute. Elle possèdera une méthoderun(rédigée sur le modèle de celle des threads) dans laquele elle se mettra indéfiniment en attente des connexions des clients. Elle déléguera le traitement de chaque connexion à une instance de la classeServeurTCPEcho(fichier.classfourni). Chaque instance deServeurTCPEcho retourne au client le message que celui-ci lui a envoyé. Plusieurs communications client/serveur peuvent avoir lieu simultanément.

Question 2 :Rédigez une classeLancerMultiServeurTCPEchodont la méthodemaindémarrera dans une première console le multi-serveur en écoute sur un port passé en paramètre.

Question 3 : Rédigez une classe de testTestClientMultiServeurqui utilisera un client TCP dans une seconde console pour envoyer un message au multi-serveur et afficher la réponse de ServeurTCPEcho.

Question 4 : Rédigez votre propre classeServeurTCPEchoCette classe présente de nombreuses analogies avec la classeClientTCP. Son constructeur reçoit la connexion client deMultiServeurTCPEcho.

(37)

public class MultiServeurTCPEcho

Field Summary

private java.net.ServerSocket ss

Constructor Summary

MultiServeurTCP

Echo(int port) throws Exception

Method Summary

void demarrer() throws Exception

méthode qui s'exécute tant que le serveur fonctionne :

traite plusieurs connexions en en déléguant le traitement à des threads

ou plus exactement à des instances de la classe ServeurTCPEcho qui implémente un thread

public class ServeurTCPEcho implements java.lang.Runnable

Field Summary

private java.io.BufferedReader in private java.io.PrintWriter out

private java.net.Socket socket

Constructor Summary

ServeurTCPEcho(java.net.Socket socket) throws IOException

Method Summary

void envoyerMessage(java.lang.String message) throws IOException void fermer() throws IOException

java.lang.String recevoirMessage() throws IOException

void demarrer() //invoque la méthode start du thread qui elle-même déclanche la méthode run()

Methods inherited from interface java.lang.Runnable

void run()

Références

Documents relatifs

stop the assembly at this point and check for shorts and solder bridges around the IC sockets, voltage regulators, and the lOO-pin

Step 1 : Eah student throws a die ten times, builds a frequeny table and draws a.. relative frequeny polygon, with sale 2 m for 1 on the x -axis and 10 m for

Build a frequency table to show your results, with both absolute and relative frequencies.. Draw a relative frequency polygon in grey, with unit 2 cm on the x -axis and 10 cm on the

(Note that the value of distinguishing N O T I F Y exceptions is that if an operation is guaranteed to be resumed after an exception is handled, the imple- mentor

The ATtiny15L provides 1K byte of Flash, 64 bytes EEPROM, six general purpose I/O lines, 32 general purpose working registers, two 8-bit Timer/Counters, one with high- speed PWM

N ote : Le caractère de commande Telnet IAC (255 en décimal) peut être un membre d'une &lt;chaîne de remplacement&gt; mais est LE SEUL caractère qui NE DOIT PAS être défini

2.8.5 Refactoring de la pile avec des listes chaînées.. Cliquez ici pour telecharger le

Intitulé Cours : Programmation JAVA avancée Copyright Sun Service