salut a tout le monde
bon mon problème est que j'ai fait une application java client serveur il fonctionne bien avec les messages simple mais pour transférer des fichier il ne fonctionne pas ,
voila le serveur :
et voila le 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 package serveur; /** * * @author Taima-info */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.*; import java.io .*; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Serveur extends JFrame implements ActionListener{ public static final int BUFFER_SIZE =4096; int port; boolean etat=true; Socket s; ServerSocket sv; JButton b; JTextField txtF; JComboBox comB1; JTextArea txtA; JButton b1; JPanel pan; JScrollPane sc; File f=null; public Serveur(){ this.setTitle("serveur"); this.setBounds(0,0,300,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(100,100); this.setLayout(null); pan=new JPanel(); pan.setLayout(null); pan.setBounds(0,0,this.getWidth(),this.getHeight()); pan.setBackground(Color.GREEN); b=new JButton("envoie"); b.setBounds(100,320,100, 30); b1=new JButton("Add File"); b1.setBounds(150,280,100, 30); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { f=null; JFileChooser file=new JFileChooser(); file.showOpenDialog(null); f=file.getSelectedFile(); //txtF.setText(f.toString()); } }); txtF=new JTextField(); comB1=new JComboBox(); txtF.setBounds(10, 280, 130, 30); comB1.setBounds(50, 10, 200, 30); txtA=new JTextArea(); sc=new JScrollPane(txtA); sc.setBounds(20,50,242,220); pan.add(b) ; pan.add(b1) ; pan.add(txtF); pan.add(comB1); pan.add(sc) ; this.add(pan); this.setVisible(true); b.addActionListener(this); try { if(!txtF.getText().equalsIgnoreCase(null)){ sv = new ServerSocket(22000); // JOptionPane.showConfirmDialog(new JFrame(), "", "", JOptionPane.OK_OPTION); while (etat) { System.out.println("Serveur lancé!"); Socket sok =sv.accept(); BufferedReader in = new BufferedReader( new InputStreamReader(sok.getInputStream())); // c'est la l'ajout des choses BufferedInputStream bis =new BufferedInputStream(sok.getInputStream()); BufferedOutputStream bos =new BufferedOutputStream( new FileOutputStream( new File("d://f.txt"))); byte buf[] =new byte[Serveur.BUFFER_SIZE]; while(bis.read(buf) !=-1){ bos.write(buf); bos.flush(); } bos.close(); bis.close(); sv.close(); sok.close(); System.out.println("Fichier réceptionné !\n\n"); String message = in.readLine(); txtA.append("client dit :"+message+"\n"); if(this.isVisible()) { etat=true; }else{ etat=false; } } } } catch (Exception e) { } } @Override public void actionPerformed(ActionEvent arg0) { try { Socket sock= new Socket("127.0.0.1",80); PrintStream out = new PrintStream(sock.getOutputStream()); if(f!=null){ out.println(f); }else{ out.println(""+txtF.getText()); } sock.close(); } catch (Exception e) { } } }
moi je veut faire l’application transféré aussi des fichier pas des message simple stt
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 package client; /** * * @author Taima-info */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.*; import java.io .*; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Client extends JFrame implements ActionListener{ int port; boolean etat=true; Socket s; ServerSocket sv; JButton b; JTextField txtF; JComboBox comB1; JTextArea txtA; JButton b1; JPanel pan; JScrollPane sc; File f=null; public Client(){ this.setTitle("serveur"); this.setBounds(0,0,300,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(100,100); this.setLayout(null); pan=new JPanel(); pan.setLayout(null); pan.setBounds(0,0,this.getWidth(),this.getHeight()); pan.setBackground(Color.GREEN); b=new JButton("envoie"); b.setBounds(100,320,100, 30); b1=new JButton("Add File"); b1.setBounds(150,280,100, 30); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { f=null; JFileChooser file=new JFileChooser(); file.showOpenDialog(null); f=file.getSelectedFile(); //txtF.setText(f.toString()); } }); txtF=new JTextField(); comB1=new JComboBox(); txtF.setBounds(10, 280, 130, 30); comB1.setBounds(50, 10, 200, 30); txtA=new JTextArea(); sc=new JScrollPane(txtA); sc.setBounds(20,50,242,220); pan.add(b) ; pan.add(b1) ; pan.add(txtF); pan.add(comB1); pan.add(sc) ; this.add(pan); this.setVisible(true); b.addActionListener(this); try { //if(!txtF.getText().equalsIgnoreCase(null)){ sv = new ServerSocket(80); // JOptionPane.showConfirmDialog(new JFrame(), "", "", JOptionPane.OK_OPTION); while (etat) { Socket sok = sv.accept(); BufferedReader in = new BufferedReader( new InputStreamReader(sok.getInputStream())); String message = in.readLine(); txtA.append("client dit :"+message+"\n"); if(this.isVisible()) { etat=true; }else{ etat=false; } } // } } catch (Exception e) { } } @Override public void actionPerformed(ActionEvent arg0) { BufferedInputStream bis =null; try { bis =new BufferedInputStream(new FileInputStream(new File("s.avi"))); Socket sock= new Socket("127.0.0.1",8080); BufferedOutputStream bos =new BufferedOutputStream(sock.getOutputStream()); PrintStream out = new PrintStream(sock.getOutputStream()); if(f!=null){ out.println(f); }else{ out.println(""+txtF.getText()); } sock.close(); } catch (Exception e) { } } }
et merci d'avance .
Partager