很久以前寫的程序,搬過來做個(gè)備忘:
服務(wù)器端代碼:
1
package com.test.talk;
2
3
import java.awt.BorderLayout;
4
import java.awt.FlowLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.awt.event.WindowAdapter;
8
import java.awt.event.WindowEvent;
9
import java.io.DataInputStream;
10
import java.io.DataOutputStream;
11
import java.io.IOException;
12
import java.net.BindException;
13
import java.net.ServerSocket;
14
import java.net.Socket;
15
import java.util.LinkedList;
16
import java.util.List;
17
18
import javax.swing.JButton;
19
import javax.swing.JFrame;
20
import javax.swing.JLabel;
21
import javax.swing.JPanel;
22
import javax.swing.JTextField;
23
24
public class Server extends JFrame
25

{
26
boolean started = false;
27
28
private ServerSocket ss = null;
29
30
private List clientList = new LinkedList();
31
32
private JLabel portLbl = null;
33
34
private JTextField portTxt = null;
35
36
private JButton portSetBtn = null;
37
38
private String port = null;
39
40
private JButton startBtn = null;
41
42
private JButton stopBtn = null;
43
44
private JPanel mainPanle = null;
45
46
private JPanel headPanle = null;
47
48
public static void main(String[] args)
49
{
50
new Server();
51
}
52
53
public Server()
54
{
55
headPanle = new JPanel(new FlowLayout(FlowLayout.LEFT));
56
portLbl = new JLabel("PORT");
57
portTxt = new JTextField(7);
58
portSetBtn = new JButton("OK");
59
portSetBtn.addActionListener(new ActionListener()
60
{
61
public void actionPerformed(ActionEvent e)
62
{
63
if (portTxt.getText().matches("\\d+"))
64
{
65
port = portTxt.getText();
66
startBtn.setEnabled(true);
67
stopBtn.setEnabled(true);
68
}
69
else
70
{
71
javax.swing.JOptionPane.showMessageDialog(null, "port be inputted is illegal");
72
}
73
}
74
});
75
headPanle.add(portLbl);
76
headPanle.add(portTxt);
77
headPanle.add(portSetBtn);
78
getContentPane().add(headPanle, BorderLayout.NORTH);
79
startBtn = new JButton("Start");
80
stopBtn = new JButton("Stop");
81
startBtn.setEnabled(false);
82
stopBtn.setEnabled(false);
83
mainPanle = new JPanel(new FlowLayout(FlowLayout.CENTER));
84
startBtn.addActionListener(new StartClickListener());
85
86
stopBtn.addActionListener(new ActionListener()
87
{
88
public void actionPerformed(ActionEvent e)
89
{
90
started = false;
91
clientList.clear();
92
try
93
{
94
if (ss != null)
95
{
96
ss.close();
97
}
98
}
99
catch(IOException e1)
100
{
101
e1.printStackTrace();
102
}
103
}
104
});
105
mainPanle.add(startBtn);
106
mainPanle.add(stopBtn);
107
getContentPane().add(mainPanle, BorderLayout.CENTER);
108
109
addWindowListener(new WindowAdapter()
110
{
111
public void windowClosing(WindowEvent e)
112
{
113
started = false;
114
clientList.clear();
115
try
116
{
117
if (ss != null)
118
{
119
ss.close();
120
}
121
}
122
catch(IOException e1)
123
{
124
e1.printStackTrace();
125
}
126
System.exit(0);
127
}
128
});
129
130
this.setSize(300, 300);
131
setLocation(100, 100);
132
pack();
133
setVisible(true);
134
}
135
136
private void start()
137
{
138
try
139
{
140
ss = new ServerSocket(Integer.parseInt(port));
141
started = true;
142
}
143
catch(BindException be)
144
{
145
javax.swing.JOptionPane.showMessageDialog(null, "port is useing by others");
146
}
147
catch(IOException e)
148
{
149
javax.swing.JOptionPane.showMessageDialog(null, "connect server fail");
150
}
151
try
152
{
153
while(started)
154
{
155
Socket s = ss.accept();
156
ClientImpl cr = new ClientImpl(s);
157
new Thread(cr).start();
158
clientList.add(cr);
159
System.out.println("System Info:" + s.getInetAddress() + "connect successfully");
160
}
161
}
162
catch(IOException e)
163
{
164
System.out.println("Client closed!");
165
}
166
}
167
168
class ClientImpl implements Runnable
169
{
170
private Socket s;
171
172
private DataInputStream dis = null;
173
174
private DataOutputStream dos = null;
175
176
boolean bConnect = false;
177
178
public ClientImpl(Socket s)
179
{
180
this.s = s;
181
try
182
{
183
dis = new DataInputStream(s.getInputStream());
184
dos = new DataOutputStream(s.getOutputStream());
185
bConnect = true;
186
}
187
catch(IOException e)
188
{
189
e.printStackTrace();
190
}
191
}
192
193
private void send(String str)
194
{
195
try
196
{
197
dos.writeUTF(str);
198
}
199
catch(IOException e)
200
{
201
e.printStackTrace();
202
}
203
204
}
205
206
public void run()
207
{
208
ClientImpl cr = null;
209
try
210
{
211
while(bConnect)
212
{
213
String str = dis.readUTF();
214
System.out.println(str);
215
for(int i = 0; i < clientList.size(); i++)
216
{
217
cr = (ClientImpl) clientList.get(i);
218
cr.send(str);
219
}
220
}
221
}
222
catch(Exception e)
223
{
224
clientList.remove(cr);
225
System.out.println(s.getInetAddress() + "has leaved");
226
}
227
finally
228
{
229
try
230
{
231
if (dis != null)
232
dis.close();
233
if (dos != null)
234
dos.close();
235
if (s != null)
236
{
237
s.close();
238
s = null;
239
}
240
}
241
catch(IOException io)
242
{
243
io.printStackTrace();
244
}
245
}
246
}
247
}
248
249
class StartClickListener implements Runnable, ActionListener
250
{
251
public void actionPerformed(ActionEvent e)
252
{
253
new Thread(this).start();
254
}
255
256
public void run()
257
{
258
start();
259
}
260
}
261
}
262
客戶端代碼:
1
package com.test.talk;
2
3
import java.awt.BorderLayout;
4
import java.awt.FlowLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.awt.event.WindowAdapter;
8
import java.awt.event.WindowEvent;
9
import java.io.DataInputStream;
10
import java.io.DataOutputStream;
11
import java.io.IOException;
12
import java.net.Socket;
13
import java.net.UnknownHostException;
14
15
import javax.swing.JButton;
16
import javax.swing.JFrame;
17
import javax.swing.JLabel;
18
import javax.swing.JPanel;
19
import javax.swing.JTextArea;
20
import javax.swing.JTextField;
21
22
/** *//**
23
* Client
24
*
25
* @author hx0272
26
*
27
*/
28
public class Client
29

{
30
public static void main(String[] args)
31
{
32
ClientFrame frame = new ClientFrame();
33
}
34
}
35
36
class ClientFrame extends JFrame
37

{
38
private JPanel mainPanel;
39
40
private JPanel headPanel;
41
42
private JPanel footPanel;
43
44
private JTextArea showArea;
45
46
private JTextField inputTxt;
47
48
private JLabel portLbl;
49
50
private JLabel ipLbl;
51
52
private JLabel nameLbl;
53
54
private JTextField portTxt;
55
56
private JTextField ipTxt;
57
58
private JTextField nameTxt;
59
60
private JButton submitBtn;
61
62
private JButton nameBtn;
63
64
private JButton loginBtn;
65
66
private Socket clientSocket = null;
67
68
private DataOutputStream dos = null;
69
70
private DataInputStream dis = null;
71
72
private boolean bConnect = false;
73
74
private String name = "";
75
76
public ClientFrame()
77
{
78
init();
79
}
80
81
private void init()
82
{
83
//main panel Begin
84
mainPanel = new JPanel();
85
showArea = new JTextArea(15, 80);
86
showArea.setEditable(false);
87
mainPanel.add(showArea);
88
getContentPane().add(mainPanel, BorderLayout.CENTER);
89
//main panel End
90
91
//head panel Begin
92
headPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
93
portLbl = new JLabel("PORT");
94
portTxt = new JTextField(7);
95
ipLbl = new JLabel("IP");
96
ipTxt = new JTextField(25);
97
98
nameLbl = new JLabel("name");
99
100
nameTxt = new JTextField(15);
101
102
nameBtn = new JButton("OK");
103
104
loginBtn = new JButton("login");
105
nameBtn.addActionListener(new ActionListener()
106
{
107
public void actionPerformed(ActionEvent e)
108
{
109
String tmp = nameTxt.getText();
110
if (tmp != null || !tmp.equals(""))
111
{
112
int id = javax.swing.JOptionPane.showConfirmDialog(null, "yes or no", "choose yes",
113
javax.swing.JOptionPane.OK_OPTION);
114
if (id == 0)
115
{
116
name = nameTxt.getText();
117
loginBtn.setEnabled(true);
118
nameBtn.setEnabled(false);
119
}
120
}
121
}
122
});
123
headPanel.add(portLbl);
124
headPanel.add(portTxt);
125
headPanel.add(ipLbl);
126
headPanel.add(ipTxt);
127
128
headPanel.add(loginBtn);
129
headPanel.add(nameLbl);
130
headPanel.add(nameTxt);
131
headPanel.add(nameBtn);
132
loginBtn.setEnabled(false);
133
loginBtn.addActionListener(new ButtonClickAction());
134
getContentPane().add(headPanel, BorderLayout.NORTH);
135
//head panel End
136
137
//foot panel Begin
138
footPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
139
inputTxt = new JTextField(70);
140
submitBtn = new JButton("submit");
141
142
footPanel.add(inputTxt);
143
footPanel.add(submitBtn);
144
145
submitBtn.addActionListener(new ButtonClickAction());
146
submitBtn.setEnabled(false);
147
getContentPane().add(footPanel, BorderLayout.SOUTH);
148
//foot panel End
149
150
addWindowListener(new WindowAdapter()
151
{
152
public void windowClosing(WindowEvent e)
153
{
154
disConnect();
155
System.exit(0);
156
}
157
});
158
159
this.setSize(300, 300);
160
setLocation(100, 100);
161
pack();
162
setVisible(true);
163
}
164
165
/** *//**
166
* login button / submit button action listener
167
*
168
* @author hx0272
169
*
170
*/
171
class ButtonClickAction implements ActionListener
172
{
173
public void actionPerformed(ActionEvent e)
174
{
175
if ("submit".equals(e.getActionCommand()))
176
{
177
String str = inputTxt.getText().trim();
178
inputTxt.setText("");
179
sendToServer(str);
180
}
181
else if ("login".equals(e.getActionCommand()))
182
{
183
connect();
184
}
185
}
186
}
187
188
/** *//**
189
* enter be inputted event
190
*
191
* @author hx0272
192
*/
193
class EnterClickAction implements ActionListener
194
{
195
public void actionPerformed(ActionEvent e)
196
{
197
String str = inputTxt.getText().trim();
198
inputTxt.setText("");
199
sendToServer(str);
200
}
201
}
202
203
/** *//**
204
* send message to server
205
*
206
* @author hx0272
207
*/
208
private void sendToServer(String str)
209
{
210
try
211
{
212
dos.writeUTF(name + ":" + str);
213
dos.flush();
214
}
215
catch(IOException e)
216
{
217
e.printStackTrace();
218
}
219
}
220
221
/** *//**
222
* close resource
223
*
224
* @author hx0272
225
*/
226
private void disConnect()
227
{
228
try
229
{
230
// clientSocket.close();
231
//
232
// dos.close();
233
234
bConnect = false;
235
}
236
catch(Exception e)
237
{
238
e.printStackTrace();
239
}
240
}
241
242
/** *//**
243
* receive message from server
244
*
245
* @author hx0272
246
*/
247
private class Receive implements Runnable
248
{
249
public void run()
250
{
251
try
252
{
253
while(bConnect)
254
{
255
String str = dis.readUTF();
256
showArea.setText(showArea.getText() + str + '\n');
257
}
258
}
259
catch(IOException e)
260
{
261
javax.swing.JOptionPane.showMessageDialog(null, "connect server error");
262
}
263
264
}
265
}
266
267
/** *//**
268
* connection begin
269
*
270
* @author hx0272
271
*/
272
private void connect()
273
{
274
try
275
{
276
if (ipTxt.getText().matches("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}") && portTxt.getText().matches("\\d+"))
277
{
278
clientSocket = new Socket(ipTxt.getText(), Integer.parseInt(portTxt.getText()));
279
dos = new DataOutputStream(clientSocket.getOutputStream());
280
dis = new DataInputStream(clientSocket.getInputStream());
281
bConnect = true;
282
new Thread(new Receive()).start();
283
System.out.println("I am coming");
284
javax.swing.JOptionPane.showMessageDialog(null, "connect server success");
285
submitBtn.setEnabled(true);
286
inputTxt.addActionListener(new EnterClickAction());
287
}
288
else
289
{
290
javax.swing.JOptionPane.showMessageDialog(null, "port or ip be inputted is illegal");
291
}
292
}
293
catch(UnknownHostException uhe)
294
{
295
javax.swing.JOptionPane.showMessageDialog(null, "connect server error");
296
}
297
catch(IOException e)
298
{
299
javax.swing.JOptionPane.showMessageDialog(null, "connect server error");
300
}
301
}
302
}
303
使用說明:
1.先運(yùn)行服務(wù)器端程序,設(shè)定好端口號(hào)
2.點(diǎn)擊Start按鈕,運(yùn)行服務(wù)器
3.運(yùn)行客戶端程序
4.設(shè)定好昵稱
5.設(shè)定與服務(wù)器端匹配的端口號(hào)和服務(wù)器的ip地址
6.點(diǎn)擊Login按鈕,登錄到聊天系統(tǒng)
7.可以聊天了。
----2008年11月24日