Vous êtes sur la page 1sur 6

Faculté des Sciences de Bizerte A.

U 2021/2022
Section GLSI3 13 octobre 2021

Développement d’applications réparties


Travaux pratiques N° 3 : modèle client/serveur
et programmation sockets (suite)

Exercice 1 Version multihtreadée d”un serveur TCP


1. Proposer une version mutlithreadée du serveur TCP du dernier exercice du TP précédent
(serveur de compte en banque).
2. Quels sont les problèmes qui peuvent avoir lieu ?

1. .
2. Il y a des risques de corruption de données, les méthodes modifiant l’état du serveur
doivent s’exécuter en exclusion mutuelle. Utiliser le mot clé synchronized.

Exercice 2 Serveur Web renvoyant la date


1. Proposer un modèle C/S permettant à un navigateur d’afficher la date. Le message
renvoyé par le serveur doit pouvoir être lu par tous les navigateurs.
2. Donner le code Java de votre serveur. Tester la solution.

— Le message renvoyé doit etre en format HTML.



1 im po rt j a v a . l a n g . ∗ ; i mpo rt j a v a . i o . ∗ ;
2 im po rt j a v a . n e t . ∗ ; i mpo rt j a v a . u t i l . Date ;
3 public c l a s s WebDateServer {
4 public s t a t i c void main ( S t r i n g a r g s [ ] ) throws E x c e p t i o n {
5 S e r v e r S o c k e t s r v r = new S e r v e r S o c k e t ( 8 0 8 8 ) ;
6 for ( ; ; ) {
7 Socket skt = s r v r . accept ( ) ;
8 P r i n t W r i t e r out = new P r i n t W r i t e r ( s k t . getOutputStream ( ) , true ) ;
9 S t r i n g r e s p o n s e =”<html>”
10 + ”<head>”
11 + ”< t i t l e >Web Date S e r v e r </ t i t l e ></head>”
12 + ”<h1> Welcome t o our Web Date S e r v e r </h1>”
13 + ”<h1>”+new Date ( ) . t o S t r i n g ( )+”</h1>”
14 + ”</html>” ;
15
16
17 out . p r i n t l n ( ”HTTP/ 1 . 0 200 ” ) ;
18 out . p r i n t l n ( ” Content−t y p e : t e x t / html ” ) ;
19 out . p r i n t l n ( ” Content−l e n g t h : ” + r e s p o n s e . l e n g t h ( ) ) ;
20 out . p r i n t l n ( ”” ) ;
21 out . println ( response ) ;
22 out . flush () ;
23 out . close () ;
24 skt . close () ;
25 }
26 }
27 }

1/6 Khaled Barbaria


Faculté des Sciences de Bizerte A.U 2021/2022
Section GLSI3 13 octobre 2021

Exercice 3 Calcul distant du pgcd de deux entiers


1. Proposer un modèle client/serveur permettant à un client de calculer le pgcd de deux
entiers sur un serveur distant
2. Implémenter le client et le serveur en java (le protocole de transport est UDP)

1 package u d p c l i e n t p g c d ;
2
3 im po rt j a v a . i o . IOException ;
4 im po rt j a v a . math . B i g I n t e g e r ;
5 im po rt j a v a . n e t . DatagramPacket ;
6 im po rt j a v a . n e t . DatagramSocket ;
7 im po rt j a v a . n e t . I n e t A d d r e s s ;
8 im po rt j a v a . n e t . S o c k e t E x c e p t i o n ;
9 im po rt j a v a . n e t . UnknownHostException ;
10 im po rt j a v a . u t i l . S c a n n e r ;
11 im po rt j a v a . u t i l . l o g g i n g . L e v e l ;
12 im po rt j a v a . u t i l . l o g g i n g . Logger ;
13 public c l a s s UDPClientPGCD {
14 /∗ ∗
15 ∗ @param a r g s t h e command l i n e arguments
16 ∗/
17 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
18 try {
19 // TODO code a p p l i c a t i o n l o g i c h e r e
20 DatagramSocket c l i e n t S o c k e t = new DatagramSocket ( ) ;
21 S c a n n e r s c = new S c a n n e r ( System . i n ) ;
22 InetAddress Serverip = InetAddress . getLocalHost () ;
23 int port = 10000;
24 System . out . p r i n t l n ( ” E n t r e z deux e n t i e r s ” ) ;
25 int x = sc . nextInt ( ) ;
26 int y = sc . nextInt ( ) ;
27 b y t e [ ] b u f f O u t = new b y t e [ 1 0 2 4 ] ;
28 b y t e [ ] b u f f I n = new b y t e [ 1 0 2 4 ] ;
29 String s = Integer . toString (x) ;
30 buffOut = s . getBytes ( ) ;
31 DatagramPacket outData = new DatagramPacket ( buffOut , b u f f O u t . l e n g t h ,
Serverip , port ) ;
32 c l i e n t S o c k e t . send ( outData ) ;
33 s = Integer . toString (y) ;
34 buffOut = s . getBytes ( ) ;
35 outData = new DatagramPacket ( buffOut , b u f f O u t . l e n g t h , S e r v e r i p , p o r t
);
36 c l i e n t S o c k e t . send ( outData ) ;
37 // r e c e v o i r l e r e s u l t a t maintenant
38 DatagramPacket inData = new DatagramPacket ( b u f f I n , b u f f I n . l e n g t h ) ;
39 c l i e n t S o c k e t . r e c e i v e ( inData ) ;
40 System . out . p r i n t l n ( ” l e pgcd e s t ”+ new S t r i n g ( b u f f I n , 0 , inData .
getLength ( ) ) ) ;
41
42 } catch ( S o c k e t E x c e p t i o n | UnknownHostException ex ) {
43 Logger . g e t L o g g e r ( UDPClientPGCD . c l a s s . getName ( ) ) . l o g ( L e v e l . SEVERE,
n u l l , ex ) ;
44 }
45 catch ( IOException ex ) {
46 Logger . g e t L o g g e r ( UDPClientPGCD . c l a s s . getName ( ) ) . l o g ( L e v e l . SEVERE,
n u l l , ex ) ;
47 }
48 }
49 }

2/6 Khaled Barbaria


Faculté des Sciences de Bizerte A.U 2021/2022
Section GLSI3 13 octobre 2021

1 /∗
2 ∗ To change t h i s l i c e n s e header , c h o o s e L i c e n s e Headers i n P r o j e c t P r o p e r t i e s .
3 ∗ To change t h i s t e m p l a t e f i l e , c h o o s e T o o l s | Templates
4 ∗ and open t h e t e m p l a t e i n t h e e d i t o r .
5 ∗/
6 package u d p s e r v e r p g c d ;
7
8 im po rt j a v a . i o . IOException ;
9 im po rt j a v a . n e t . DatagramPacket ;
10 im po rt j a v a . n e t . DatagramSocket ;
11 im po rt java . net . InetAddress ;
12 im po rt java . net . SocketException ;
13 im po rt j a v a . n e t . UnknownHostException ;
14 im po rt j a v a . u t i l . Arrays ;
15 im po rt java . u t i l . logging . Level ;
16 im po rt j a v a . u t i l . l o g g i n g . Logger ;
17
18 /∗ ∗
19 ∗
20 ∗ @author macbook
21 ∗/
22 public c l a s s UDPServerPGCD {
23
24 /∗ ∗
25 ∗ @param a r g s t h e command l i n e arguments
26 ∗/
27 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
28 try {
29 DatagramSocket s e r v e r S o c k e t = new DatagramSocket ( 1 0 0 0 0 ) ;
30 while ( true ) {
31 try {
32 // TODO code a p p l i c a t i o n l o g i c h e r e
33
34 // r e c u p e r e r l e s deux e n t i e r s
35 b y t e [ ] b u f f I n = new b y t e [ 1 0 2 4 ] ;
36 DatagramPacket inData = new DatagramPacket ( b u f f I n , b u f f I n .
length ) ;
37 // r e c e v o i r l e p r e m i e r e n t i e r
38 s e r v e r S o c k e t . r e c e i v e ( inData ) ;
39 S t r i n g f i r s t i n t e g e r = new S t r i n g ( b u f f I n , 0 , inData . g e t L e n g t h ( )
);
40 // r e c e v o i r l e deuxieme e n t i e r
41
42 s e r v e r S o c k e t . r e c e i v e ( inData ) ;
43 S t r i n g s e c o n f i n t e g e r = new S t r i n g ( b u f f I n , 0 , inData . g e t L e n g t h
() ) ;
44 I n e t A d d r e s s i p = inData . g e t A d d r e s s ( ) ;
45 i n t p o r t = inData . g e t P o r t ( ) ;
46 // c o n v e r t i r v e r s d e s e n t i e r s
47 int x = I n t e g e r . p a r s e I n t ( f i r s t i n t e g e r ) ;
48 int y = I n t e g e r . p a r s e I n t ( s e c o n f i n t e g e r ) ;
49 // c r e e r l e paquet udp
50 i n t r e s = pgcd ( x , y ) ;
51
52
53 DatagramPacket outData = new DatagramPacket ( I n t e g e r . t o S t r i n g
( r e s ) . g e t B y t e s ( ) , I n t e g e r . t o S t r i n g ( r e s ) . l e n g t h ( ) , ip ,
port ) ;
54 s e r v e r S o c k e t . send ( outData ) ;
55 } catch ( S o c k e t E x c e p t i o n | UnknownHostException ex ) {
56 Logger . g e t L o g g e r ( UDPServerPGCD . c l a s s . getName ( ) ) . l o g ( L e v e l .
SEVERE, n u l l , ex ) ;
57 } catch ( IOException ex ) {
58 Logger . g e t L o g g e r ( UDPServerPGCD . c l a s s . getName ( ) ) . l o g ( L e v e l .
SEVERE, n u l l , ex ) ;
59 }

3/6 Khaled Barbaria


Faculté des Sciences de Bizerte A.U 2021/2022
Section GLSI3 13 octobre 2021

60 }
61 } catch ( S o c k e t E x c e p t i o n ex ) {
62 Logger . g e t L o g g e r ( UDPServerPGCD . c l a s s . getName ( ) ) . l o g ( L e v e l . SEVERE,
n u l l , ex ) ;
63 }
64
65 }
66 s t a t i c i n t pgcd ( i n t x , i n t y ) {
67 i f ( x == y ) {
68 return x ;
69 }
70 i f ( x> y ) return pgcd ( x−y , y ) ;
71 e l s e return pgcd ( x , y−x ) ;
72 }
73
74 }

Exercice 4 Application Echo avec UDP et Swing


1. Proposer un modèle client/serveur permettant de réaliser le scénario suivant :(1)les
client envoient leurs packets aux serveurs (2)le serveur renvoie les memes données au
client avec des informations supplémentaires (taille des données, date de réception, port
de réception, etc).
2. Implementer l’application en UDP
3. Les clients et serveurs sont des JFrame Swing, mettre à jour l’application.

— Le message renvoyé doit etre en format HTML.



1 im po rt j a v a . awt . ∗ ; i mpo rt j a v a x . swing . ∗ ; i mpo rt j a v a . awt . e v e n t . ∗ ;
2
3
4 im po rt j a v a . i o . IOException ;
5 im po rt j a v a . n e t . ∗ ;
6
7
8 public c l a s s UDPJFrameClient e x t e n d s JFrame{
9 private f i n a l J T e x t F i e l d m s g F i e l d=new J T e x t F i e l d ( ) ;
10 private f i n a l JTextArea msgArea=new JTextArea ( ) ;
11 private DatagramSocket s o c k e t ;
12
13 public UDPJFrameClient ( ) {
14 s u p e r ( ”UDP C l i e n t ” ) ;
15 s u p e r . add ( msgField , BorderLayout .NORTH) ;
16 s u p e r . add (new J S c r o l l P a n e ( msgArea ) ,
17 BorderLayout .CENTER) ;
18 s u p e r . s e t S i z e (new Dimension ( 4 5 0 , 3 5 0 ) ) ;
19 s u p e r . s e t V i s i b l e ( true ) ;
20 s u p e r . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE) ;
21 msgArea . s e t E d i t a b l e ( f a l s e ) ;
22
23 try {
24 s o c k e t=new DatagramSocket ( ) ;
25 } catch ( S o c k e t E x c e p t i o n ex ) {
26 System . e x i t ( 1 ) ;
27 }
28

4/6 Khaled Barbaria


Faculté des Sciences de Bizerte A.U 2021/2022
Section GLSI3 13 octobre 2021

29
30 m s g F i e l d . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( ) {
31 public void a c t i o n P e r f o r m e d ( ActionEvent e v t ) {
32 try {
33 S t r i n g msg=e v t . getActionCommand ( ) ;
34 showMsg ( ” \ nSending message p a c k e t : ”+msg ) ;
35 b y t e b u f f [ ] = msg . g e t B y t e s ( ) ;
36 DatagramPacket pa cket Sen d=
37 new DatagramPacket ( b u f f , b u f f . l e n g t h ,
38 InetAddress . getLocalHost () ,
12345) ;
39 s o c k e t . send ( pac ketS end ) ;
40 showMsg ( ” \ nPacket s e n t ” ) ;
41 } catch ( IOException ex ) {
42 showMsg ( ex . g e t M e s s a g e ( ) ) ;
43 }}
44 }) ;
45 }
46
47 public void r e a d y T o R e c e i v P a c k e t ( ) {
48 while ( true ) {
49 try {
50 b y t e b u f f [ ] =new b y t e [ 1 2 8 ] ;
51 DatagramPacket p a c k e t=
52 new DatagramPacket ( b u f f , b u f f . l e n g t h ) ;
53 socket . r e c e i v e ( packet ) ;
54 showMsg ( ” \ nHost : ” + p a c k e t . g e t A d d r e s s ( )
55 + ” \ nPort : ” + p a c k e t . g e t P o r t ( )
56 + ” \ nLength : ” + p a c k e t . g e t L e n g t h ( )
57 + ” \ nData : ”
58 + new S t r i n g ( p a c k e t . getData ( ) ) ) ;
59
60 } catch ( IOException ex ) {
61 showMsg ( ex . g e t M e s s a g e ( ) ) ;
62 }
63 }
64 }
65
66 public void showMsg ( f i n a l S t r i n g msg ) {
67 msgArea . append ( msg ) ;
68 }
69
70 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
71 UDPJFrameClient c l i e n t=new UDPJFrameClient ( ) ;
72 c l i e n t . readyToReceivPacket ( ) ;
73 }
74 }


1 im po rt j a v a . awt . Dimension ;
2 im po rt j a v a . i o . IOException ;
3 im po rt java . net . ∗ ;
4 im po rt j a v a x . swing . ∗ ;
5
6 public c l a s s UDPJFrameServer e x t e n d s JFrame {
7
8 private f i n a l JTextArea msgArea = new JTextArea ( ) ;
9 private DatagramSocket s o c k e t ;
10
11 public UDPJFrameServer ( ) {
12 s u p e r ( ” Message S e r v e r ” ) ;
13 s u p e r . add (new J S c r o l l P a n e ( msgArea ) ) ;
14 s u p e r . s e t S i z e (new Dimension ( 4 5 0 , 3 5 0 ) ) ;
15 s u p e r . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE) ;
16 s u p e r . s e t V i s i b l e ( true ) ;
17 msgArea . s e t E d i t a b l e ( f a l s e ) ;

5/6 Khaled Barbaria


Faculté des Sciences de Bizerte A.U 2021/2022
Section GLSI3 13 octobre 2021

18
19 try {
20 s o c k e t = new DatagramSocket ( 1 2 3 4 5 ) ;
21
22 } catch ( S o c k e t E x c e p t i o n ex ) {
23 System . e x i t ( 1 ) ;
24 }
25 }
26
27 public void r e a d y T o R e c e i v P a c k e t ( ) {
28 while ( true ) {
29 try {
30 b y t e b u f f e r [ ] = new b y t e [ 1 2 8 ] ;
31 DatagramPacket p a c k e t =
32 new DatagramPacket ( b u f f e r , b u f f e r . l e n g t h ) ;
33 socket . r e c e i v e ( packet ) ;
34 showMsg ( ” \ nHost : ” + p a c k e t . g e t A d d r e s s ( )
35 + ” \ nPort : ” + p a c k e t . g e t P o r t ( )
36 + ” \ nLength : ” + p a c k e t . g e t L e n g t h ( )
37 + ” \ nData : ”
38 + new S t r i n g ( p a c k e t . getData ( ) ) ) ;
39 sendPacket ( packet ) ;
40 } catch ( IOException ex ) {
41 showMsg ( ex . g e t M e s s a g e ( ) ) ;
42 }
43 }
44 }
45
46 public void s e n d P a c k e t ( DatagramPacket p a c k e t R e c e i v e d ) {
47 showMsg ( ” \nEcho t o c l i e n t . . . ” ) ;
48 try {
49 DatagramPacket p a c k e t =
50 new DatagramPacket ( p a c k e t R e c e i v e d . getData ( ) ,
51 packetReceived . getLength ( ) ,
52 packetReceived . getAddress ( ) ,
53 packetReceived . getPort () ) ;
54 s o c k e t . send ( p a c k e t ) ;
55 showMsg ( ” \ nMessage s e n t ” ) ;
56 } catch ( IOException ex ) {
57
58 }
59 }
60
61 public void showMsg ( f i n a l S t r i n g msg ) {
62 msgArea . append ( msg ) ;
63 }
64
65 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
66 UDPJFrameServer s e r v e r=new UDPJFrameServer ( ) ;
67 s e r v e r . readyToReceivPacket ( ) ;
68 }
69 }

6/6 Khaled Barbaria

Vous aimerez peut-être aussi