// 服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
Socket socket = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中。。。。。");
System.out.println("请关掉相关程序,并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
socket = ss.accept();
Client c = new Client(socket);
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
class Client implements Runnable {
private Socket socket = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket socket) {
bConnected = true;
this.socket = socket;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
悔晌 dos.writeUTF(str);
}catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了,我从List里面去掉了!");
}
}
@Override
public void run() {
try {
旦虚 while(bConnected) {
String s = dis.readUTF();
System.out.println(s);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(s);
}
}
} catch (EOFException e) {
System.out.println("client closed!");
} catch (IOException e) {
模前燃 e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(socket != null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//客户端
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bConnected = false;
Thread tRecv = new Thread(new Server());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setBounds(400, 300, 300, 300);
setVisible(true);
this.setTitle("Chat Client");
this.add(tfTxt, BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
this.pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
setVisible(false);
System.exit(0);
}
});
tfTxt.addActionListener(new TFMonitor());
connect();
tRecv.start();
}
public void connect() {
try {
socket = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(this.socket.getInputStream());
System.out.println("Connected!");
bConnected = 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();
}
/*
try {
bConnected = false;
tRecv.join();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
private class TFMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try {
//System.out.println(socket);
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class Server implements Runnable {
@Override
public void run() {
try {
while(bConnected) {
String s = dis.readUTF();
//System.out.println(s);
taContent.setText(taContent.getText() + s + '\n');
}
} catch (IOException e) {
System.out.println("talk over,byebye");
//e.printStackTrace();
}
}
}
}