package com.dh.ch13;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ChatClient extends JFrame {
Socket socket;
PrintWriter out;
BufferedReader in;
JPanel p;
JScrollPane sp;
JTextArea txtContent;
JLabel lblName,lblSend;
JTextField txtName,txtSend;
JButton btnSend;
public ChatClient() {
super("客户聊天");
txtContent = new JTextArea();
// 设置文本域只读
txtContent.setEditable(false);
sp = new JScrollPane(txtContent);
lblName=new JLabel("姓名:");
txtName=new JTextField(5);
lblSend = new JLabel("请输入:");
txtSend = new JTextField(20);
btnSend = new JButton("发送");
p = new JPanel();
p.add(lblName);
p.add(txtName);
p.add(lblSend);
p.add(txtSend);
p.add(btnSend);
this.add(p, BorderLayout.SOUTH);
this.add(sp);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
// 创建一个套接字
socket = new Socket("127.0.0.1", 1218);
// 创建一个往套接字中写数据的管道,即输出流,给服务器发送信息
out = new PrintWriter(socket.getOutputStream());
// 创建一个从套接字读数据的管道,即输入流,读服务器的返回信息
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("没有找到主机或主机未打开。");
} catch (IOException e) {
e.printStackTrace();
}
// 注册监听
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户输入的文本
String strName=txtName.getText();
String strMsg = txtSend.getText();
if (!strMsg.equals("")) {
// 通过输出流将数据发送给服务器
out.println(strName+" 说:"+strMsg);
out.flush();
// 清空文本框
txtSend.setText("");
}
}
});
// 启动线程
new GetMsgFromServer().start();
}
// 接收服务器的返回信息
class GetMsgFromServer extends Thread {
public void run() {
while (this.isAlive()) {
try {
String strMsg = in.readLine();
if (strMsg != null) {
// 在文本域中显示聊天信息
txtContent.append(strMsg+"\n");
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
ChatClient f = new ChatClient();
f.setVisible(true);
}
}
package com.dh.ch13;
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
ServerSocket serverSocket;
ArrayList<BufferedReader> ins=new ArrayList<BufferedReader>();
ArrayList<PrintWriter> outs=new ArrayList<PrintWriter>();
LinkedList<String> msgList=new LinkedList<String>();
public ChatServer(){
try {
serverSocket=new ServerSocket(1218);
} catch (IOException e) {
e.printStackTrace();
}
//创建AcceptSocketThread线程,并启动
new AcceptSocketThread().start();
//创建SendMsgToClient线程,并启动
new SendMsgToClient().start();
System.out.println("Server Start...");
}
//接收客户端套接字线程
class AcceptSocketThread extends Thread{
public void run(){
while(this.isAlive()){
try {
//接收套接字
Socket socket=serverSocket.accept();
if(socket!=null){
BufferedReader in=new BufferedReader(new
InputStreamReader(socket.getInputStream()));
ins.add(in);
outs.add(new PrintWriter(socket.getOutputStream()));
//开启一个线程接收客户端的聊天信息
new GetMsgFromClient(in).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 接收客户的聊天信息的线程
class GetMsgFromClient extends Thread {
BufferedReader in;
public GetMsgFromClient(BufferedReader in){
this.in=in;
}
public void run() {
while (this.isAlive()) {
try {
String strMsg = in.readLine();
if (strMsg != null) {
msgList.addFirst(strMsg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//给所有客户发送聊天信息的线程
class SendMsgToClient extends Thread{
public void run(){
while(this.isAlive()){
try {
if(!msgList.isEmpty()){
String s=msgList.removeLast();
for(int i=0;i<outs.size();i++){
outs.get(i).println(s);
outs.get(i).flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new ChatServer();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f021196209c859e1dcd2eaf01a503613.png)