public class ChatClient extends Frame {
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnection = false;
Socket s = null; // 服务器连接对象
private String userName = "潜水人员";
private boolean hasUsername = false;
TextField tfTxt = new TextField(); // 添加一个文本框
TextArea taContent = new TextArea(); // 添加一个文件域
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatClient().launchFrame();
}
public void launchFrame() {
this.setLocation(300, 400); // 设置窗体的位置
this.setSize(300, 300); // 设置窗体的大小
add(tfTxt, BorderLayout.SOUTH); // 往窗体中添加文本框 位置为窗体的确南方
add(taContent, BorderLayout.NORTH); // 往窗体中添加文件域 位置为窗体的确北方
pack(); // 自到调整窗体的位置
taContent.setText("您的昵称:");
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnect(); // 释放资源
System.exit(0); // 正常终止程序
}
});
tfTxt.addActionListener(new TFlistener());
this.setVisible(true);
connect(); // 调用连接服务器的方法
new Thread(new RecvThread()).start(); // 启动线程
}
// 连接服务器
private void connect() {
try {
// s = new Socket("127.0.0.1", 8888);
s = new Socket("192.168.0.5", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("content");
bConnection = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放资源
*/
public void disconnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 添加一个程序响应事件
private class TFlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim(); // 得到tfTxt文本框中的内容
// taContent.setText(str); //设置taContent文件域中的内容
tfTxt.setText(""); // 设置tfTxt文本框中的内容
try {
if (hasUsername == false) {
userName = "".equals(str) ? userName : str;
// dos.writeUTF(userName + "上线了!!!!!!");
taContent.setText(taContent.getText() + userName + "/n");
hasUsername = true;
} else {
dos.writeUTF("【" + userName + "】说:" + str);
}
dos.flush();
// dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while (bConnection) {
if (hasUsername == true) {
String str = dis.readUTF();
// System.out.println(str);
taContent.setText(taContent.getText() + str + "/n");
}
}
} catch (SocketException e) {
System.out.println("退出了!byebye!");
} catch (EOFException e) {
System.out.println("推出了! bye!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
ChatServer chatServer = new ChatServer();
chatServer.start();
}
public void start() {
try {
ss = new ServerSocket(8888); // 8888表示的是端口名
started = true;
} catch (BindException ex) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序!关重新运行服务器!");
System.exit(0);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept(); // 表示接收客戶端
Client c = new Client(s);
System.out.println("a Client contend!");
new Thread(c).start();
clients.add(c); // 把对象添加到集合中去
// dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s; // 代表与客户端的连接
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnection = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnection = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException es) {
clients.remove(this);
System.out.println("对方退出了!我从list里面去除了!");
// es.printStackTrace();
}
}
public void run() {
try {
while (bConnection) {
String str = dis.readUTF();
System.out.println(str);
for (int i = 0; i < clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
/*
* for(Iterator<Client> it =
* clients.iterator();it.hasNext();) { Client c = it.next();
* c.send(str); }
*/
/*
* Iterator<Client> it = clients.iterator();
* while(it.hasNext()) { Client c = it.next(); c.send(str); }
*/
}
} 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 (s != null)
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}