服务端:
package cs;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;/*** @author zzs* @date 2021/9/22 20:49*/
public class Server extends JFrame implements ActionListener, KeyListener {//文本域private JTextArea jta;//滚动条private JScrollPane jsp;//面板private JPanel jp;//文本框private JTextField jtf;//按钮private JButton jb;//输出流private BufferedWriter bw = null;//服务端的端口号private static int serverPort;static {Properties properties = new Properties();try {properties.load(new FileReader("chat.properties"));//赋值serverPort = Integer.parseInt(properties.getProperty("serverPort"));} catch (IOException e) {e.printStackTrace();}}public Server() {//初始化组件jta = new JTextArea();//文本域默认不可编辑jta.setEnabled(false);//将文本域添加到滚动条中实现滚动jsp = new JScrollPane(jta);jp = new JPanel();jtf = new JTextField(20);jb = new JButton("发送");//将文本框和按钮添加到面板中jp.add(jtf);jp.add(jb);//将滚动条和面板添加到窗体中this.add(jsp, BorderLayout.CENTER);//放中间位置this.add(jp,BorderLayout.SOUTH);//放于底部//设置标题,大小,位置,关闭,可见this.setTitle("服务端");this.setSize(400,400);this.setLocation(300,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭则退出this.setVisible(true);/*****************服务端开始*******************///给发送按钮绑定监听事件,需要继承ActionListener接口jb.addActionListener(this);//给文本框绑定一个回车键事件,KeyListener接口jtf.addKeyListener(this);try {//1.创建SocketServerSocket serverSocket = new ServerSocket(serverPort);//2.等待客户端连接Socket socket = serverSocket.accept();//3.获取输入流,读数据InputStream is = socket.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));//4.获取输出流,写数据OutputStream os = socket.getOutputStream();bw = new BufferedWriter(new OutputStreamWriter(os));//循环读取数据String line = null;while ((line = br.readLine())!=null){//将读取到的数据置于文本域中jta.append(line+System.lineSeparator());//内容后面接上换行}//5.关闭Socket通道serverSocket.close();} catch (IOException e) {e.printStackTrace();}/*****************服务端结束*******************/}
/*** @Author zzs* @Description //TODO 监听按钮事件* @Date 21:49 2021/9/22* @Param [e]* @return void**/@Overridepublic void actionPerformed(ActionEvent e) {sendData();}
/*** @Author zzs* @Description //TODO 文本框回车键事件,3个方法,这里只需重写keyPressed方法* @Date 22:30 2021/9/22* @Param [e]* @return void**/@Overridepublic void keyPressed(KeyEvent e) {//回车键if (e.getKeyCode()==KeyEvent.VK_ENTER){//发送数据到socket通道中sendData();}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}public void sendData(){//1.获取文本框中发送的内容String text = jtf.getText();//2.拼接发送的数据内容text = "服务端: "+text;//3.在服务端文本域中显示jta.append(text+System.lineSeparator());//4.发送数据try {bw.write(text);bw.newLine();//换行bw.flush();//刷新//5.清空文本框内容jtf.setText("");} catch (IOException ex) {ex.printStackTrace();}}public static void main(String[] args) {new Server();}
}
客户端:
package cs;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.Socket;
import java.util.Properties;/*** @author zzs* @date 2021/9/22 20:49*/
public class Client extends JFrame implements ActionListener, KeyListener {//文本域private JTextArea jta;//滚动条private JScrollPane jsp;//面板private JPanel jp;//文本框private JTextField jtf;//按钮private JButton jb;//输出流private BufferedWriter bw = null;//客户端的ip , 端口号private static String clientIP;private static int clientPort;static {Properties properties = new Properties();try {properties.load(new FileReader("chat.properties"));clientIP = properties.getProperty("clientIP");clientPort = Integer.parseInt(properties.getProperty("clientPort"));} catch (IOException e) {e.printStackTrace();}}public Client() {//初始化组件jta = new JTextArea();//文本域默认不可编辑jta.setEnabled(false);//将文本域添加到滚动条中实现滚动jsp = new JScrollPane(jta);jp = new JPanel();jtf = new JTextField(20);jb = new JButton("发送");//将文本框和按钮添加到面板中jp.add(jtf);jp.add(jb);//将滚动条和面板添加到窗体中this.add(jsp, BorderLayout.CENTER);//放中间位置this.add(jp,BorderLayout.SOUTH);//放于底部//设置标题,大小,位置,关闭,可见this.setTitle("客户端");this.setSize(400,400);this.setLocation(700,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭则退出this.setVisible(true);/*****************客户端开始*******************///发送按钮绑定监听事件jb.addActionListener(this);//给文本框绑定回车键事件jtf.addKeyListener(this);try {//1.创建SocketSocket socket = new Socket(clientIP, clientPort);//2.获取输入流,读数据InputStream is = socket.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));//3.获取输出流,写数据OutputStream os = socket.getOutputStream();bw = new BufferedWriter(new OutputStreamWriter(os));//循环读取数据String line = null;while ((line = br.readLine())!=null){jta.append(line+System.lineSeparator());}//4.关闭Socket通道socket.close();} catch (IOException e) {e.printStackTrace();}/*****************客户端结束*******************/}
/*** @Author zzs* @Description //TODO 客户端发送按钮监听事件* @Date 22:02 2021/9/22* @Param [e]* @return void**/@Overridepublic void actionPerformed(ActionEvent e) {sendData();}
/*** @Author zzs* @Description //TODO 客户端给文本框绑定一个回车键事件* @Date 22:39 2021/9/22* @Param [e]* @return void**/@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode()==KeyEvent.VK_ENTER){sendData();}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}public void sendData(){//1.获取文本框中发送的数据内容String text = jtf.getText();//2.拼接发送的内容text = "客户端: "+ text;//3.在客户端文本域中显示jta.append(text+System.lineSeparator());try {//4.发送数据bw.write(text);//不换行和刷新,服务端会收不到信息bw.newLine();bw.flush();//5.清空文本框jtf.setText("");} catch (IOException ex) {ex.printStackTrace();}}public static void main(String[] args) {new Client();}
}
运行结果: