1
//服务器部分
import java.io.DataInputStream;
2 import java.io.DataOutputStream;
3 import java.io.EOFException;
4 import java.io.IOException;
5 import java.net.BindException;
6 import java.net.ServerSocket;
7 import java.net.Socket;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 public class ChatServer {
12
13 public static boolean isRun = false;
14
15 ServerSocket serverSocket = null;
16
17 List<ServerThread>st = new ArrayList<>();
18
19 public static void main(String[] args) {
20 new ChatServer().start();
21 }
22
23 public void start() {
24
25 try {
26 serverSocket = new ServerSocket(8888);
27 isRun = true;
28 } catch (BindException e) {
29 System.out.println("端口使用中");
30 System.exit(0);
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34
35 try {
36
37 while (isRun) {
38
39 Socket socket = serverSocket.accept();
40 ServerThread serverThread = new ServerThread(socket);
41 new Thread(serverThread).start();
42 st.add(serverThread);
43 // dis.close();
44 }
45 } catch (IOException e) {
46 e.printStackTrace();
47 } finally {
48 try {
49 serverSocket.close();
50 } catch (IOException e) {
51 e.printStackTrace();
52 }
53 }
54 }
55
56 class ServerThread implements Runnable {
57
58 private Socket s;
59 private DataInputStream dis = null;
60 private DataOutputStream dos = null;
61 private boolean bConnected = false;
62
63 public ServerThread(Socket s) {
64 super();
65 this.s = s;
66 try {
67 dis = new DataInputStream(s.getInputStream());
68 dos = new DataOutputStream(s.getOutputStream());
69 bConnected = true;
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73
74 }
75
76
77 public void send(String str){
78 try {
79 dos.writeUTF(str);
80 } catch (IOException e) {
81 st.remove(this);
82
83 }
84 }
85
86 @Override
87 public void run() {
88
89 try {
90 while (bConnected) {
91 String str = dis.readUTF();
92 System.out.println(str);
93 for(int i = 0 ; i < st.size() ; i ++){
94 ServerThread serverThread1 = st.get(i);
95 serverThread1.send(str);
96 }
97 }
98 }catch (EOFException e) {
99 System.out.println("client closed");
100 } catch (IOException e) {
101 e.printStackTrace();
102 }finally {
103 try {
104 if (s != null){
105 s.close();
106 s = null;
107 }
108 if (dis != null)
109 dis.close();
110 if(dos != null)
111 dos.close();
112 } catch (IOException e1) {
113 e1.printStackTrace();
114 }
115
116
117
118 }
119
120 }
121
122 }
123
124 }
//客户端部分
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class ChatClient extends Frame {
DataOutputStream dos = null;
Socket socket;
TextField inputChat = new TextField();
TextArea showChat = new TextArea();
DataInputStream dis = null;
private static boolean isRun = false;
Thread tRecv = new Thread(new ClientThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
setSize(300, 300);
add(inputChat, BorderLayout.SOUTH);
add(showChat, BorderLayout.NORTH);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
inputChat.addActionListener(new TextFieldListener());
setVisible(true);
connectToServer();
tRecv.start();
}
private void connectToServer() {
try {
socket = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
isRun = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
//关闭各种流之前必须先关闭接受线程
}
private class ClientThread implements Runnable{
@Override
public void run() {
try {
while(isRun){
String str = dis.readUTF();
System.out.println(str);
showChat.setText(showChat.getText() + str +'\n');
}
}catch (SocketException e) {
System.out.println("bye");
}catch (EOFException e) {
}catch (IOException e) {
e.printStackTrace();
}
}
}
private class TextFieldListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String s = inputChat.getText().trim();
// showChat.setText(s);
inputChat.setText("");
try {
dos.writeUTF(s);
dos.flush();
// dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}