一对一聊天

2023-12-14 18:09:36

服务端

 
  1. package 一对一用户;

  2. import java.awt.BorderLayout;

  3. import java.io.BufferedReader;

  4. import java.io.IOException;

  5. import java.io.InputStreamReader;

  6. import java.io.PrintWriter;

  7. import java.net.ServerSocket;

  8. import java.net.Socket;

  9. import java.util.Vector;

  10. import javax.swing.JFrame;

  11. import javax.swing.JScrollPane;

  12. import javax.swing.JTextArea;

  13. public class fuwuchuangkou extends JFrame {

  14. private JTextArea ta_info;

  15. private ServerSocket server; // 声明ServerSocket对象

  16. private Socket socket; // 声明Socket对象socket

  17. private Vector<Socket> vector = new Vector<Socket>();// 用于存储连接到服务器的客户端套接字对象

  18. public void createSocket() {

  19. try {

  20. server = new ServerSocket(1978);

  21. while (true) {

  22. ta_info.append("等待新客户连接......\n");

  23. socket = server.accept();// 创建套接字对象

  24. vector.add(socket);// 将套接字对象添加到向量对象中

  25. ta_info.append("客户端连接成功。" + socket + "\n");

  26. new ServerThread(socket).start();// 创建并启动线程对象

  27. }

  28. } catch (IOException e) {

  29. e.printStackTrace();

  30. }

  31. }

  32. class ServerThread extends Thread {

  33. Socket socket;

  34. public ServerThread(Socket socket) {

  35. this.socket = socket;

  36. }

  37. public void run() {

  38. try {

  39. BufferedReader in = new BufferedReader(new InputStreamReader(

  40. socket.getInputStream()));// 创建输入流对象

  41. while (true) {

  42. String info = in.readLine();// 读取信息

  43. for (Socket s : vector) {// 遍历所有客户端套接字对象

  44. if (s != socket) {// 如果不是发送信息的套接字对象

  45. PrintWriter out = new PrintWriter(s

  46. .getOutputStream(), true);// 创建输出流对象

  47. out.println(info);// 发送信息

  48. out.flush();// 刷新输出缓冲区

  49. }

  50. }

  51. }

  52. } catch (IOException e) {

  53. ta_info.append(socket + "已经退出。\n");

  54. vector.remove(socket);// 移除退出的客户端套接字

  55. }

  56. }

  57. }

  58. public static void main(String args[]) {

  59. fuwuchuangkou frame = new fuwuchuangkou();

  60. frame.setVisible(true);

  61. frame.createSocket();

  62. }

  63. /**

  64. * Create the frame

  65. */

  66. public fuwuchuangkou() {

  67. super();

  68. setTitle("一对一");

  69. setBounds(100, 100, 385, 266);

  70. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  71. final JScrollPane scrollPane = new JScrollPane();

  72. getContentPane().add(scrollPane, BorderLayout.CENTER);

  73. ta_info = new JTextArea();

  74. scrollPane.setViewportView(ta_info);

  75. }

  76. }

客户端

 
  1. package 一对一用户;

  2. import java.awt.BorderLayout;

  3. import java.awt.Dimension;

  4. import java.awt.EventQueue;

  5. import java.awt.event.ActionEvent;

  6. import java.awt.event.ActionListener;

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10. import java.io.PrintWriter;

  11. import java.net.Socket;

  12. import java.net.UnknownHostException;

  13. import javax.swing.JButton;

  14. import javax.swing.JFrame;

  15. import javax.swing.JLabel;

  16. import javax.swing.JPanel;

  17. import javax.swing.JScrollPane;

  18. import javax.swing.JTextArea;

  19. import javax.swing.JTextField;

  20. public class kehuchuangkou extends JFrame {

  21. private JTextArea ta_info;

  22. private JTextField tf_send;

  23. PrintWriter out;// 声明输出流对象

  24. /**

  25. * Launch the application

  26. *

  27. * @param args

  28. */

  29. public static void main(String args[]) {

  30. EventQueue.invokeLater(new Runnable() {

  31. public void run() {

  32. try {

  33. kehuchuangkou frame = new kehuchuangkou();

  34. frame.setVisible(true);

  35. frame.createClientSocket();

  36. } catch (Exception e) {

  37. e.printStackTrace();

  38. }

  39. }

  40. });

  41. }

  42. public void createClientSocket() {

  43. try {

  44. Socket socket = new Socket("127.0.0.1", 1978);// 连接本地1978端口

  45. out = new PrintWriter(socket.getOutputStream(), true);// 创建输出流对象

  46. new ClientThread(socket).start();// 创建并启动线程对象

  47. } catch (UnknownHostException e) {

  48. e.printStackTrace();

  49. } catch (IOException e) {

  50. e.printStackTrace();

  51. }

  52. }

  53. class ClientThread extends Thread {

  54. Socket socket;

  55. public ClientThread(Socket socket) {

  56. this.socket = socket;

  57. }

  58. public void run() {

  59. try {

  60. BufferedReader in = new BufferedReader(new InputStreamReader(

  61. socket.getInputStream()));// 创建输入流对象

  62. while (true) {

  63. String info = in.readLine();// 读取信息

  64. ta_info.append(info + "\n");// 在文本域中显示信息

  65. if (info.equals("88")) {

  66. break;// 结束线程

  67. }

  68. }

  69. } catch (IOException e) {

  70. e.printStackTrace();

  71. }

  72. }

  73. }

  74. private void send() {

  75. String info = tf_send.getText();// 获得输入的信息

  76. if (info.equals("")) {

  77. return;// 如果没输入信息则返回,即不发送

  78. }

  79. if (info.equals("88")) {

  80. System.exit(0);// 如果没输入信息是88,则退出

  81. }

  82. out.println(info);// 发送信息

  83. out.flush();// 刷新输出缓冲区

  84. tf_send.setText(null);// 清空文本框

  85. }

  86. public kehuchuangkou() { //创建面板

  87. super();

  88. setTitle("一对一");

  89. setBounds(100, 100, 385, 266);

  90. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  91. final JPanel panel = new JPanel();

  92. getContentPane().add(panel, BorderLayout.SOUTH);

  93. final JLabel label = new JLabel();

  94. label.setText("输入聊天内容:");

  95. panel.add(label);

  96. tf_send = new JTextField();

  97. tf_send.addActionListener(new ActionListener() {

  98. public void actionPerformed(final ActionEvent e) {

  99. send();

  100. }

  101. });

  102. tf_send.setPreferredSize(new Dimension(180, 25));

  103. panel.add(tf_send);

  104. final JButton button = new JButton();

  105. button.addActionListener(new ActionListener() {

  106. public void actionPerformed(final ActionEvent e) {

  107. send();

  108. }

  109. });

  110. button.setText("发 送");

  111. panel.add(button);

  112. final JScrollPane scrollPane = new JScrollPane();

  113. getContentPane().add(scrollPane, BorderLayout.CENTER);

  114. ta_info = new JTextArea();

  115. scrollPane.setViewportView(ta_info);

  116. }

  117. }

 
  1. package 一对一用户;

  2. import java.awt.BorderLayout;

  3. import java.awt.Dimension;

  4. import java.awt.EventQueue;

  5. import java.awt.event.ActionEvent;

  6. import java.awt.event.ActionListener;

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10. import java.io.PrintWriter;

  11. import java.net.Socket;

  12. import java.net.UnknownHostException;

  13. import javax.swing.JButton;

  14. import javax.swing.JFrame;

  15. import javax.swing.JLabel;

  16. import javax.swing.JPanel;

  17. import javax.swing.JScrollPane;

  18. import javax.swing.JTextArea;

  19. import javax.swing.JTextField;

  20. public class kehu1 extends JFrame {

  21. private JTextArea ta_info;

  22. private JTextField tf_send;

  23. PrintWriter out;// 声明输出流对象

  24. /**

  25. * Launch the application

  26. *

  27. * @param args

  28. */

  29. public static void main(String args[]) {

  30. EventQueue.invokeLater(new Runnable() {

  31. public void run() {

  32. try {

  33. kehuchuangkou frame = new kehuchuangkou();

  34. frame.setVisible(true);

  35. frame.createClientSocket();

  36. } catch (Exception e) {

  37. e.printStackTrace();

  38. }

  39. }

  40. });

  41. }

  42. public void createClientSocket() {

  43. try {

  44. Socket socket = new Socket("127.0.0.1", 1978);// 连接本地1978端口

  45. out = new PrintWriter(socket.getOutputStream(), true);// 创建输出流对象

  46. new ClientThread(socket).start();// 创建并启动线程对象

  47. } catch (UnknownHostException e) {

  48. e.printStackTrace();

  49. } catch (IOException e) {

  50. e.printStackTrace();

  51. }

  52. }

  53. class ClientThread extends Thread {

  54. Socket socket;

  55. public ClientThread(Socket socket) {

  56. this.socket = socket;

  57. }

  58. public void run() {

  59. try {

  60. BufferedReader in = new BufferedReader(new InputStreamReader(

  61. socket.getInputStream()));// 创建输入流对象

  62. while (true) {

  63. String info = in.readLine();// 读取信息

  64. ta_info.append(info + "\n");// 在文本域中显示信息

  65. if (info.equals("88")) {

  66. break;// 结束线程

  67. }

  68. }

  69. } catch (IOException e) {

  70. e.printStackTrace();

  71. }

  72. }

  73. }

  74. private void send() {

  75. String info = tf_send.getText();// 获得输入的信息

  76. if (info.equals("")) {

  77. return;// 如果没输入信息则返回,即不发送

  78. }

  79. if (info.equals("88")) {

  80. System.exit(0);// 如果没输入信息是88,则退出

  81. }

  82. out.println(info);// 发送信息

  83. out.flush();// 刷新输出缓冲区

  84. tf_send.setText(null);// 清空文本框

  85. }

  86. public kehu1() { //创建面板

  87. super();

  88. setTitle("一对一");

  89. setBounds(100, 100, 385, 266);

  90. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  91. final JPanel panel = new JPanel();

  92. getContentPane().add(panel, BorderLayout.SOUTH);

  93. final JLabel label = new JLabel();

  94. label.setText("输入聊天内容:");

  95. panel.add(label);

  96. tf_send = new JTextField();

  97. tf_send.addActionListener(new ActionListener() {

  98. public void actionPerformed(final ActionEvent e) {

  99. send();

  100. }

  101. });

  102. tf_send.setPreferredSize(new Dimension(180, 25));

  103. panel.add(tf_send);

  104. final JButton button = new JButton();

  105. button.addActionListener(new ActionListener() {

  106. public void actionPerformed(final ActionEvent e) {

  107. send();

  108. }

  109. });

  110. button.setText("发 送");

  111. panel.add(button);

  112. final JScrollPane scrollPane = new JScrollPane();

  113. getContentPane().add(scrollPane, BorderLayout.CENTER);

  114. ta_info = new JTextArea();

  115. scrollPane.setViewportView(ta_info);

  116. }

  117. }

运行结果如下:

文章来源:https://blog.csdn.net/ycq8888/article/details/134852448
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。