这个是因为帮朋友做作业记录一下,现场学习嗄,都忘了。好了不多说了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author: create_By:
* @Data:Created in 2019/12/24 23:26
* @Version:
* @Acton: QQ服务系统
*/
public class QQServerSystem {
private JFrame frame; // 窗口
private Container container; // 创中的容器对象
public JTextArea txtList; // 文本列表框
public JTextField txtMsg; // 文本发送框
public JButton btn; // 发送按钮
public String addMsg = "未连接";
public OutputStream os; //发送消息的
public QQServerSystem() {
frame = new JFrame("九哥的QQ服务端");
frame.setBounds(400, 300, 800, 600); //设置窗口大小位置
frame.setLayout(new BorderLayout()); //设置样式
container = frame.getContentPane(); //窗口生成容器
txtList = new JTextArea(5, 20); //生成文本域的大小
container.add(txtList, BorderLayout.CENTER); //把文本加入容器中,并设置布局
JPanel txtPanel = new JPanel();
txtPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
txtMsg = new JTextField(60); //设置消息文本行高
btn = new JButton("发送");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//实现给客户端发送消息的功能
//1、点击发送获取消息框中的信息
String msgText = txtMsg.getText();
//2、处理文本
if (!"".equals(msgText) && msgText != null) { //判断是有文字的才会做处理
//1、显示到自己的连天窗口
txtList.append("我: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
txtList.append("\r\n");
txtList.append(msgText);
txtList.append("\r\n");
txtMsg.setText("");
//2、发送
byte[] sendBuf = msgText.getBytes();
try {
if (os != null) {
os.write(sendBuf);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
txtPanel.add(txtMsg); //消息框加入面板中
txtPanel.add(btn); //按钮加入面板中
container.add(txtPanel, BorderLayout.SOUTH); //把面板加入容器中,并设置布局
startServerThread();
refreshMsgList();
}
/* 刷新消息列表 */
private void refreshMsgList() {
String txt = txtList.getText();
txtList.setText(txt + addMsg + "\n");
}
/* 启动线程 */
private void startServerThread() {
new ServerListenThread(this).start();
}
/* 启动面板 */
public void start() {
frame.setVisible(true); //设置可以显示
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author: create_By:
* @Data:Created in 2019/12/24 23:47
* @Version:
* @Acton: 继承线程让其成为线程类
*/
public class ServerListenThread extends Thread {
private ServerSocket serverSocket;
private Socket socket;
private InputStream is;
public QQServerSystem qqs;
public ServerListenThread(QQServerSystem qqs) {
super();
this.qqs = qqs;
}
@Override
public void run() {
//实现监听8888端口,接收客户端发送的消息,并显示到界面
try {
serverSocket = new ServerSocket(8888, 1000); //创建服务端套接字并设置端口
socket = serverSocket.accept(); //创建套接字
is = socket.getInputStream(); //获取输入流
qqs.os = socket.getOutputStream(); //获取输出流
while (true) {
if (socket.isConnected()) {
byte[] buff = new byte[1024]; //设置一个临时缓冲区
int len = is.read(buff); //从输入流读取字节长度
byte[] eBuff = new byte[len]; //根据实际长度,定义一个输入缓冲区
System.arraycopy(buff, 0, eBuff, 0, len); //拷贝数据
String text = new String(eBuff); //把字节转换字符
qqs.txtList.append("对方: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
qqs.txtList.append("\r\n");
qqs.txtList.append(text);
qqs.txtList.append("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ServerMain {
public static void main(String[] args) {
QQServerSystem qs = new QQServerSystem();
qs.start();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author: create_By:
* @Data:Created in 2019/12/24 23:56
* @Version:
* @Acton: QQ客户端系统
*/
public class QQClientSystem{
private JFrame frame; //窗口
private Container container; //创中的容器对象
public JTextArea txtList; //文本列表框
public JTextField txtMsg; //文本发送框
public JButton btn; //发送按钮
public String addMsg = "未连接";
public OutputStream os; //用于发送信息。
public QQClientSystem() {
frame = new JFrame("九哥的QQ客户端");
frame.setBounds(400, 300, 800, 600); // 设置窗口大小和位置
frame.setLayout(new BorderLayout());
container = frame.getContentPane();
txtList = new JTextArea(5, 20);
container.add(txtList, BorderLayout.CENTER);
JPanel txtPanel = new JPanel();
txtPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
txtMsg = new JTextField(60);
btn = new JButton("发送");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 实现给服务器端发送消息的部分
//1、点击发送获取消息框中的信息
String msgText = txtMsg.getText();
//2、处理文本
if(!"".equals(msgText) && msgText != null){ //判断是有文字的才会做处理
//2、发送
byte[] sendBuf = msgText.getBytes();
try {
if (os != null){
os.write(sendBuf);
}
} catch (IOException e1) {
e1.printStackTrace();
}
txtList.append("我: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
txtList.append("\r\n");
txtList.append(msgText);
txtList.append("\r\n");
txtMsg.setText("");
}
}
});
txtPanel.add(txtMsg);
txtPanel.add(btn);
container.add(txtPanel, BorderLayout.SOUTH);
startRequestThread();
refreshMsgList();
}
public void startRequestThread() {
new ClientRequestThread(this).start();
}
public void refreshMsgList() {
String str = txtList.getText();
txtList.setText(str + addMsg + "\n");
}
public void start(){
frame.setVisible(true);
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author: create_By:
* @Data:Created in 2019/12/24 23:58
* @Version:
* @Acton:
*/
public class ClientRequestThread extends Thread {
private Socket socket;
private InputStream is;
QQClientSystem qqc;
public ClientRequestThread(QQClientSystem qqc) {
this.qqc = qqc;
}
@Override
public void run() {
// 实现连接服务器的功能,并可以接收服务器端发送的消息,显示到程序界面
try {
socket = new Socket("localhost", 8888);
is = socket.getInputStream();
qqc.os = socket.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
if (socket.isConnected()) { //已经连接才做以下处理
byte[] buff = new byte[1024]; //设置一个临时缓冲区
int len = 0; //从输入流读取字节长度
try {
len = is.read(buff);
} catch (IOException e) {
e.printStackTrace();
}
byte[] eBuff = new byte[len]; //根据实际长度,定义一个输入缓冲区
System.arraycopy(buff, 0, eBuff, 0, len); //拷贝数据
String text = new String(eBuff); //把字节转换字符
qqc.txtList.append("对方: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
qqc.txtList.append("\r\n");
qqc.txtList.append(text);
qqc.txtList.append("\r\n");
}
}
}
}
public class ClientMain {
public static void main(String[] args) {
QQClientSystem qc = new QQClientSystem();
qc.start();
}
}
这个直接复制到自己的代码中运行即可,然后后续的优化自己改下就行了。因为相对简洁,我们只需要学习部分基础功能和知识即可。如果本章对你有所帮助,请点个赞收藏一下,蟹蟹。