bonjours, apres plusieurs jours de recherche, je me suis inscris sur ce forum pour avoir la réponse de mon probleme ( je ne sai pas si j aurai la réponse) mais je reste optimiste :-)
j ai un projet à faire, j ai 3 classe: SocketThrdServer.java SocketAdmin.java
et socketClient.java
le but c est que l admin doit envoyer des messagee aux clients mais les clients ne peuvent envoyer des messages.
j ai essayé de lire beaucoup de programme, comme je débute, j ai du mal à les comprendre :-( mais je ne baisse pas les bras!!!
alors quel sait comment ajouter des methodes pour que Admin puisse envoyer des messages aux clients?
voici mon programme:
La classe Admin:la Classe SocketThrdServer
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 import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class SocketAdmin extends JFrame implements ActionListener { JLabel text, clicked; JButton button; JPanel panel; JTextField textField; Socket socket = null; PrintWriter out = null; BufferedReader in = null; SocketAdmin(){ //Begin Constructor text = new JLabel("Text to send over socket:"); textField = new JTextField(20); button = new JButton("Click Me"); button.addActionListener(this); panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel); panel.add("North", text); panel.add("Center", textField); panel.add("South", button); } //End Constructor public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if(source == button){ //Send data over socket String text = textField.getText(); out.println(text); textField.setText(new String("")); //Receive text from server try{ String line = in.readLine(); System.out.println("Text received :" + line); } catch (IOException e){ System.out.println("Read failed"); System.exit(1); } } } public void listenSocket(){ //Create socket connection try{ socket = new Socket("127.0.0.1", 4444); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.out.println("Unknown host: 127.0.0.1.eng"); System.exit(1); } catch (IOException e) { System.out.println("No I/O"); System.exit(1); } } public static void main(String[] args){ SocketAdmin frame = new SocketAdmin(); frame.setTitle("Administrateur Program"); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); frame.listenSocket(); } }
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 import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class ClientWorker implements Runnable { private Socket client; private JTextArea textArea; ClientWorker(Socket client, JTextArea textArea) { this.client = client; this.textArea = textArea; } public void run(){ String line; BufferedReader in = null; PrintWriter out = null; try{ in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(), true); } catch (IOException e) { System.out.println("in or out failed"); System.exit(-1); } while(true){ try{ line = in.readLine(); //Send data back to client out.println(line); textArea.append(line); } catch (IOException e) { System.out.println("Read failed"); System.exit(-1); } } } }Merci d avance
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 class SocketThrdServer extends JFrame{ JLabel label = new JLabel("Text received over socket:"); JPanel panel; JTextArea textArea = new JTextArea(); ServerSocket server = null; SocketThrdServer(){ //Begin Constructor panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel); panel.add("North", label); panel.add("Center", textArea); } //End Constructor public void listenSocket(){ try{ server = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port 4321"); System.exit(-1); } while(true){ ClientWorker w; try{ w = new ClientWorker(server.accept(), textArea); Thread t = new Thread(w); t.start(); } catch (IOException e) { System.out.println("Accept failed: 4321"); System.exit(-1); } } } protected void finalize(){ //Objects created in run method are finalized when //program terminates and thread exits try{ server.close(); } catch (IOException e) { System.out.println("Could not close socket"); System.exit(-1); } } public static void main(String[] args){ SocketThrdServer frame = new SocketThrdServer(); frame.setTitle("Server Program"); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); frame.listenSocket(); } }
Partager