BONJOUR,
J'ai un probléme d'une application en java client/serveur avec echange de documents ,c'est un mini projet que l'on doit présenté dans une semaine
voici le resultat de mon application :
Resultat sur la fenetre du client :
client dit : telecharger fichier1.txt
le serveur cherche et envoie au client lefichier demandé ligne par ligne sur la fenetre client:
serveur dit :
nom et prenom :ali
serveur dit :
age: 30ans
serveur dit :
profession : comptable
le serveur envoie au client le fichier demandé mais ligne par ligne ,
voici le code client :
___________________________________client_______________________
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; class Fenetre1 extends Frame implements Runnable { TextArea txta; TextField txtf; Socket connexion; PrintWriter ecrivain; BufferedReader lecteur; boolean ok=true; String ligne; public Fenetre1() throws IOException { this.setTitle("client"); this.txta=new TextArea(); this.txtf=new TextField(); //----------Gestionnaire De Disposition----------------------------------- LayoutManager gest=new BorderLayout(); this.setLayout(gest); //----------insertion des composants dans la Fenetre11---------------- this.add(this.txta,BorderLayout.CENTER); this.add(this.txtf,BorderLayout.SOUTH); this.setSize(300,300); this.setVisible(true); //____________objet ecouteur____________ Ecouteur ecout; ecout=new Ecouteur(this); this.txtf.addActionListener(ecout); //_____________lienfermeture_____________ EcouteFenetre1 ecoutf=new EcouteFenetre1(this); //____________etablir le lien___________ this.addWindowListener(ecoutf); //===============initialiser le serveur================ this.connexion = new Socket("192.168.1.10",12000); this.txta.append("Connexion etablie avec succes\n"); //-------------------------connexion etablie-------------------------// this.ecrivain=new PrintWriter(connexion.getOutputStream(),true); this.lecteur=new BufferedReader(new InputStreamReader(connexion.getInputStream())); } public void run() { while(ok) { try { this.ligne=this.lecteur.readLine(); //this.ecrivain.println("echo "+this.ligne); this.txta.append("\nserveur dit:\n"); this.txta.append(this.ligne); this.txta.append("\n"); } catch(IOException e) { System.out.println ("erreur de "); System.exit(0); } } } } //_____________________________________________________________ class Ecouteur implements ActionListener { Fenetre1 fen; public Ecouteur(Fenetre1 f) { this.fen=f; } public void actionPerformed(ActionEvent e) { String s=this.fen.txtf.getText(); this.fen.txta.append("client dit:"); this.fen.txta.append("\n"); this.fen.txta.append(s); this.fen.txta.append("\n"); this.fen.ecrivain.println(s); this.fen.txtf.setText(""); } } //______________Fermeture Fenetre1___________________ class EcouteFenetre1 implements WindowListener { Fenetre1 fen; public EcouteFenetre1(Fenetre1 f) { this.fen=f; } public void windowActivated(WindowEvent e) { System.out.println ("windowActivated"); } public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); } public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); System.exit(0); } public void windowDeactivated(WindowEvent e) { System.out.println("windowDeactivated"); } public void windowDeiconified(WindowEvent e) { System.out.println("windowDeiconified"); } public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); } public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } } //___________________________________________ //------------------------------------------- public class Client { public static void main(String[]args) //throws IOException // on gère en laissant le sup gérer { try { Fenetre1 fen; fen=new Fenetre1(); Thread tache = new Thread(fen); tache.start(); } catch(IOException e) { System.out.println ("lqkshdflkshf"); //System.exit(0); } } }
et voici le code serveur:
_____________________________serveur_____________________________
Je veux que ce fichier soit stocké dans un repertoire du client pour permettre au client d'y acceder et pour qu'il reste enregistré chez le client au cas il en aura besoin
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.StringTokenizer; class Fenetre1 extends Frame implements Runnable { TextArea txta; TextField txtf; ServerSocket connexionServeur; Socket connexion; PrintWriter ecrivain; BufferedReader lecteur; boolean ok=true; String ligne=null; public Fenetre1() throws IOException { this.setTitle("serveur"); this.txta=new TextArea(); this.txtf=new TextField(); //----------Gestionnaire De Disposition----------------------------------- LayoutManager gest=new BorderLayout(); this.setLayout(gest); //----------insertion des composants dans la Fenetre11---------------- this.add(this.txta,BorderLayout.CENTER); this.add(this.txtf,BorderLayout.SOUTH); this.setSize(300,300); this.setVisible(true); //____________objet ecouteur____________ Ecouteur ecout; ecout=new Ecouteur(this); this.txtf.addActionListener(ecout); //_____________lienfermeture_____________ EcouteFenetre1 ecoutf=new EcouteFenetre1(this); //____________etablir le lien___________ this.addWindowListener(ecoutf); //===============initialiser le serveur================ this.connexionServeur=new ServerSocket(12000); this.txta.append("(serveur)En attente d'une connexion\n"); this.connexion=connexionServeur.accept(); this.txta.append("Connexion etablie avec succes\n"); //-------------------------connexion etablie-------------------------// this.ecrivain=new PrintWriter(connexion.getOutputStream(),true); this.lecteur=new BufferedReader(new InputStreamReader(connexion.getInputStream())); //================================================= /* while(ok) { this.ligne=this.lecteur.readLine(); this.txta.append(this.ligne); this.txta.append("\n "); } */ } public void run() { while(ok) { try { this.ligne=this.lecteur.readLine(); StringTokenizer tokenizer = neStringTokenizer(this.ligne); int count = tokenizer.countTokens(); // traitement telechargement ; if(count>=2) { if(tokenizer.hasMoreTokens()) { String codeTelechargement = tokenizer.nextToken(); this.txta.append(codeTelechargement ); if(0==codeTelechargement.compareToIgnoreCase("telecharger")) // si demande de telechargement { // acces au nom du fichier a temecharger if(tokenizer.hasMoreTokens()) { String filePath = tokenizer.nextToken(); FileReader fichier=null; BufferedReader entree=null; String ligne=null; int i=0; try { fichier=new FileReader("C:\\" + filePath); } catch(FileNotFoundException e) { System.out.println ("fichier non trouvé"); } entree=new BufferedReader(fichier); try { while( (ligne=entree.readLine() )!=null ) { i++; System.out.println (""+i +": "+ ligne); this.ecrivain.println(ligne); } } catch(IOException e1) { System.out.println ("erreur entree/sortie"); } try { entree.close(); fichier.close(); } catch(IOException e2) { System.out.println ("erreur fermeture fichier"); } } } } } //======================================================= //this.ecrivain.println("echo "+this.ligne); this.txta.append("\nclient dit:\n"); this.txta.append(this.ligne); this.txta.append("\n"); } catch(IOException e) { System.out.println ("erreur de "); System.exit(0); } } } } //_____________________________________________________________ class Ecouteur implements ActionListener { Fenetre1 fen; public Ecouteur(Fenetre1 f) { this.fen=f; } public void actionPerformed(ActionEvent e) { String s=this.fen.txtf.getText(); this.fen.ecrivain.println(s); this.fen.txta.append("serveur dit:"); this.fen.txta.append("\n"); this.fen.txta.append(s); this.fen.txta.append("\n"); this.fen.txtf.setText(""); } } //______________Fermeture Fenetre1___________________ class EcouteFenetre1 implements WindowListener { Fenetre1 fen; public EcouteFenetre1(Fenetre1 f) { this.fen=f; } public void windowActivated(WindowEvent e) { System.out.println ("windowActivated"); } public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); } public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); System.exit(0); } public void windowDeactivated(WindowEvent e) { System.out.println("windowDeactivated"); } public void windowDeiconified(WindowEvent e) { System.out.println("windowDeiconified"); } public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); } public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } } //___________________________________________ //------------------------------------------- public class Fenetre { public static void main(String[]args) //throws IOException // on gère en laissant le sup gérer { try { Fenetre1 fen; fen=new Fenetre1(); Thread tache; tache = new Thread(fen); tache.start(); } catch(IOException e) { System.out.println ("lqkshdflkshf"); System.exit(0); } } }
il faut que le serveur envoie directement le fichier à un répertoire préçi qui va contenir que les fichiers reçus du serveur au client
Merci d'avance pour votre aide
cordialement,
Partager