Java多线程实现聊天客户端和服务器

 更新时间:2021年08月26日 11:42:49   作者:White_Idiot  
这篇文章主要为大家详细介绍了Java多线程聊天客户端和服务器实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java聊天室代码,供大家参考,具体内容如下

主要涉及知识

  • Java中GUI程序的编写,包括事件监听机制。
  • Java的网络通信编程,ServerSocket,Socket类的使用。
  • Java中多线程的编程,Thread类,Runnable接口的使用。

源代码

客户端

package project1; 
 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
 
import javax.swing.*; 
 
public class Client { 
 private JFrame clientFrame; 
 private JLabel IPLabel; 
 private JLabel PortLabel; 
 private JLabel sayLabel; 
 private JLabel nicknameLabel; 
 private JTextField IPText; 
 private JTextField PortText; 
 private JTextField nicknameText; 
 private JTextField sayText; 
 private JButton connectButton; 
 private JButton nicknameButton; 
 private JButton sayButton; 
 private JPanel jPanelNorth; 
 private JPanel jPanelSouth0; 
 private JPanel jPanelSouth1; 
 private JPanel jPanelSouth2; 
 private JTextArea clientTextArea; 
 private JScrollPane scroller; 
 private BufferedReader reader; 
 private PrintWriter writer; 
 private String nickname; 
 
 public static void main(String args[]) { 
  Client aClient = new Client(); 
  aClient.startUp(); 
 } 
 
 // 初始化组件 
 public Client() { 
  nickname = "客户端"; 
 
  clientFrame = new JFrame(); 
  jPanelNorth = new JPanel(); 
  IPLabel = new JLabel("服务器IP", JLabel.LEFT); 
  IPText = new JTextField(10); 
  PortLabel = new JLabel("服务器端口", JLabel.LEFT); 
  PortText = new JTextField(10); 
  connectButton = new JButton("连接"); 
  clientTextArea = new JTextArea(); 
  scroller = new JScrollPane(clientTextArea); 
  jPanelSouth0 = new JPanel(); 
  jPanelSouth1 = new JPanel(); 
  jPanelSouth2 = new JPanel(); 
  nicknameLabel = new JLabel("昵称", JLabel.LEFT); 
  nicknameText = new JTextField(nickname, 30); 
  nicknameButton = new JButton("确认"); 
  sayLabel = new JLabel("消息", JLabel.LEFT); 
  sayText = new JTextField(30); 
  sayButton = new JButton("确认"); 
 } 
 
 // 构建GUI 
 private void buildGUI() { 
  // 窗口的设置 
  clientFrame.setTitle("客户端"); 
  clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  clientFrame.setSize(550, 550); 
 
  // 北区的组件 
  jPanelNorth.add(IPLabel); 
  jPanelNorth.add(IPText); 
  jPanelNorth.add(PortLabel); 
  jPanelNorth.add(PortText); 
  jPanelNorth.add(connectButton); 
  clientFrame.getContentPane().add(BorderLayout.NORTH, jPanelNorth); 
 
  // 中间的组件 
  clientTextArea.setFocusable(false); 
  scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
  scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
  clientFrame.getContentPane().add(BorderLayout.CENTER, scroller); 
 
  // 南区的组件 
  jPanelSouth1.add(nicknameLabel); 
  jPanelSouth1.add(nicknameText); 
  jPanelSouth1.add(nicknameButton); 
  jPanelSouth2.add(sayLabel); 
  jPanelSouth2.add(sayText); 
  jPanelSouth2.add(sayButton); 
  jPanelSouth0.setLayout(new BoxLayout(jPanelSouth0, BoxLayout.Y_AXIS)); 
  jPanelSouth0.add(jPanelSouth1); 
  jPanelSouth0.add(jPanelSouth2); 
  clientFrame.getContentPane().add(BorderLayout.SOUTH, jPanelSouth0); 
 
  // 设置窗口可见 
  clientFrame.setVisible(true); 
 } 
 
 // 客户端运行 
 public void startUp() { 
  buildGUI(); 
 
  // 接收服务器消息的线程 
  Runnable incomingReader = new Runnable() { 
   @Override 
   public void run() { 
    String message; 
    try { 
     while ((message = reader.readLine()) != null) { 
      clientTextArea.append(message + "\n"); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
   } 
  }; 
 
  // 监听Connect按钮,实现服务器的连接 
  connectButton.addActionListener(new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    String aServerIP = IPText.getText(); 
    String aServerPort = PortText.getText(); 
 
    if (aServerIP.equals("") || aServerPort.equals("")) { 
     JOptionPane.showMessageDialog(clientFrame, "请输入 完整的 IP和端口!"); 
    } else { 
     try { 
      @SuppressWarnings("resource") 
      Socket clientSocket = new Socket(aServerIP, Integer.parseInt(aServerPort)); 
      InputStreamReader streamReader = new InputStreamReader(clientSocket.getInputStream()); 
      reader = new BufferedReader(streamReader); 
      writer = new PrintWriter(clientSocket.getOutputStream()); 
 
      clientTextArea.append("服务器已连接...\n"); 
 
      Thread readerThread = new Thread(incomingReader); 
      readerThread.start(); 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(clientFrame, "连接不上服务器!\n请确认 IP 和 端口 输入正确。"); 
     } 
    } 
   } 
  }); 
 
  // 监听nickname,设置昵称 
  ActionListener nicknameListener = new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    String aText = nicknameText.getText(); 
    if (!aText.equals("")) { 
     nickname = aText; 
    } 
   } 
  }; 
  nicknameButton.addActionListener(nicknameListener); 
  nicknameText.addActionListener(nicknameListener); 
  nicknameText.addFocusListener(new FocusListener() { 
   @Override 
   public void focusGained(FocusEvent e) { 
   } 
 
   @Override 
   public void focusLost(FocusEvent e) { 
    String aText = nicknameText.getText(); 
    if (!aText.equals("")) { 
     nickname = aText; 
    } 
   } 
  }); 
 
  // 发送消息到服务器 
  ActionListener SayListener = new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    String aText = sayText.getText(); 
    if (aText.equals("")) { 
     JOptionPane.showMessageDialog(clientFrame, "内容不能为空!"); 
    } else { 
     try { 
      writer.println(nickname + ":" + aText); 
      writer.flush(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     sayText.setText(""); 
    } 
   } 
  }; 
  sayButton.addActionListener(SayListener); 
  sayText.addActionListener(SayListener); 
 
 } 
 
} 

服务器

package project1; 
 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.util.ArrayList; 
import java.util.Iterator; 
import javax.swing.*; 
 
public class Server { 
 private JFrame serverFrame; 
 private JLabel portLabel; 
 private JLabel sayLabel; 
 private JLabel nicknameLabel; 
 private JTextField portText; 
 private JTextField sayText; 
 private JTextField nicknameText; 
 private JButton startButton; 
 private JButton sayButton; 
 private JButton nicknameButton; 
 private JPanel jPanelNorth; 
 private JPanel jPanelSouth0; 
 private JPanel jPanelSouth1; 
 private JPanel jPanelSouth2; 
 private JScrollPane scroller; 
 private JTextArea serverTextArea; 
 private ArrayList<PrintWriter> clientOutputStreams; 
 private String nickname; 
 
 public static void main(String[] args) { 
  Server aServer = new Server(); 
  aServer.startUp(); 
 } 
 
 // 初始化组件 
 public Server() { 
  nickname = "服务器"; 
 
  serverFrame = new JFrame(); 
  jPanelNorth = new JPanel(); 
  portLabel = new JLabel("端口", JLabel.LEFT); 
  portText = new JTextField(30); 
  startButton = new JButton("开始"); 
  serverTextArea = new JTextArea(); 
  scroller = new JScrollPane(serverTextArea); 
  nicknameLabel = new JLabel("昵称", JLabel.LEFT); 
  nicknameText = new JTextField(nickname, 30); 
  nicknameButton = new JButton("确认"); 
  jPanelSouth0 = new JPanel(); 
  jPanelSouth1 = new JPanel(); 
  jPanelSouth2 = new JPanel(); 
  sayLabel = new JLabel("消息", JLabel.LEFT); 
  sayText = new JTextField(30); 
  sayButton = new JButton("确认"); 
 } 
 
 // 构建GUI 
 private void buildGUI() { 
  // 窗口的设置 
  serverFrame.setTitle("服务器"); 
  serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  serverFrame.setSize(550, 550); 
 
  // 北区的组件 
  jPanelNorth.add(portLabel); 
  jPanelNorth.add(portText); 
  jPanelNorth.add(startButton); 
  serverFrame.getContentPane().add(BorderLayout.NORTH, jPanelNorth); 
 
  // 中间的组件 
  serverTextArea.setFocusable(false); 
  scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
  scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
  serverFrame.getContentPane().add(BorderLayout.CENTER, scroller); 
 
  // 南区的组件 
  jPanelSouth1.add(nicknameLabel); 
  jPanelSouth1.add(nicknameText); 
  jPanelSouth1.add(nicknameButton); 
  jPanelSouth2.add(sayLabel); 
  jPanelSouth2.add(sayText); 
  jPanelSouth2.add(sayButton); 
  jPanelSouth0.setLayout(new BoxLayout(jPanelSouth0, BoxLayout.Y_AXIS)); 
  jPanelSouth0.add(jPanelSouth1); 
  jPanelSouth0.add(jPanelSouth2); 
  serverFrame.getContentPane().add(BorderLayout.SOUTH, jPanelSouth0); 
 
  // 设置窗口可见 
  serverFrame.setVisible(true); 
 } 
 
 // 服务器运行 
 public void startUp() { 
  buildGUI(); 
 
  // 监听Start按钮,建立端口 
  ActionListener startListener = new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    clientOutputStreams = new ArrayList<PrintWriter>(); 
    String aPort = portText.getText(); 
 
    if (aPort.equals("")) { 
     JOptionPane.showMessageDialog(serverFrame, "请输入正确的端口号!"); 
    } else { 
     try { 
      // 等待客户端连接的线程 
      Runnable serverRunnable = new Runnable() { 
       @Override 
       public void run() { 
        ServerSocket serverSocket; 
        try { 
         serverSocket = new ServerSocket(Integer.parseInt(aPort)); 
         serverTextArea.append("正在等待客户端连接...\n"); 
         while (true) { 
          Socket clientSocket = serverSocket.accept(); 
          serverTextArea.append("客户端已连接...\n"); 
 
          PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); 
          clientOutputStreams.add(writer); 
 
          Thread t = new Thread(new ClientHandler(clientSocket)); 
          t.start(); 
         } 
        } catch (NumberFormatException | IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
      Thread serverThread = new Thread(serverRunnable); 
      serverThread.start(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
   } 
  }; 
  startButton.addActionListener(startListener); 
  portText.addActionListener(startListener); 
 
  // 监听nickname,设置昵称 
  ActionListener nicknameListener = new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    String aText = nicknameText.getText(); 
    if (!aText.equals("")) { 
     nickname = aText; 
    } 
   } 
  }; 
  nicknameButton.addActionListener(nicknameListener); 
  nicknameText.addActionListener(nicknameListener); 
  nicknameText.addFocusListener(new FocusListener() { 
   @Override 
   public void focusGained(FocusEvent e) { 
   } 
 
   @Override 
   public void focusLost(FocusEvent e) { 
    String aText = nicknameText.getText(); 
    if (!aText.equals("")) { 
     nickname = aText; 
    } 
   } 
  }); 
 
  // 监听Say按钮,发送消息 
  ActionListener SayListener = new ActionListener() { 
   @Override 
   public void actionPerformed(ActionEvent e) { 
    String aText = sayText.getText(); 
    if (!aText.equals("")) { 
     aText = nickname + ":" + aText; 
     sendToEveryClient(aText); 
     serverTextArea.append(aText + "\n"); 
     sayText.setText(""); 
    } else { 
     JOptionPane.showMessageDialog(serverFrame, "内容不能为空!"); 
    } 
   } 
  }; 
  sayButton.addActionListener(SayListener); 
  sayText.addActionListener(SayListener); 
 } 
 
 // 多客户端的线程 
 public class ClientHandler implements Runnable { 
  BufferedReader bReader; 
  Socket aSocket; 
 
  public ClientHandler(Socket clientSocket) { 
   try { 
    aSocket = clientSocket; 
    InputStreamReader isReader = new InputStreamReader(aSocket.getInputStream()); 
    bReader = new BufferedReader(isReader); 
   } catch (Exception ex) { 
    ex.printStackTrace(); 
   } 
  } 
 
  @Override 
  public void run() { 
   String message; 
   try { 
    while ((message = bReader.readLine()) != null) { 
     sendToEveryClient(message); 
     serverTextArea.append(message + "\n"); 
    } 
   } catch (Exception ex) { 
    ex.printStackTrace(); 
   } 
  } 
 } 
 
 // 发送消息给所有客户端的方法 
 private void sendToEveryClient(String message) { 
  Iterator<PrintWriter> it = clientOutputStreams.iterator(); 
  while (it.hasNext()) { 
   try { 
    PrintWriter writer = (PrintWriter) it.next(); 
    writer.println(message); 
    writer.flush(); 
   } catch (Exception ex) { 
    ex.printStackTrace(); 
   } 
  } 
 } 
 
} 

GUI运行截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java设计模式之装饰器模式(Decorator)

    java设计模式之装饰器模式(Decorator)

    这篇文章主要为大家详细介绍了java设计模式之装饰器模式Decorator,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • SpringBoot微信扫码支付的实现示例

    SpringBoot微信扫码支付的实现示例

    这篇文章主要介绍了SpringBoot微信扫码支付的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java 处理超大数类型之BigInteger案例详解

    Java 处理超大数类型之BigInteger案例详解

    这篇文章主要介绍了Java 处理超大数类型之BigInteger案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • myeclipse8.5优化技巧详解

    myeclipse8.5优化技巧详解

    本文详细介绍了myeclipse8.5优化技巧,在这里和大家分享一下,希望对大家有用。
    2017-09-09
  • SpringAOP 设置注入的实现步骤

    SpringAOP 设置注入的实现步骤

    这篇文章主要介绍了SpringAOP 设置注入的实现步骤,帮助大家更好的理解和学习使用Spring框架,感兴趣的朋友可以了解下
    2021-05-05
  • java多线程join()方法的作用和实现原理解析(应用场景)

    java多线程join()方法的作用和实现原理解析(应用场景)

    join方法主要是用于将当前线程挂起,等待其他线程结束后在执行当前线程,本文通过应用场景分析代码示例讲解java多线程join()方法的作用和实现原理,感兴趣的朋友一起看看吧
    2021-07-07
  • Java中上传图片压缩处理的方法示例

    Java中上传图片压缩处理的方法示例

    本篇文章主要介绍了Java中图片压缩处理的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • java实现计算器模板及源码

    java实现计算器模板及源码

    这篇文章主要为大家详细介绍了java实现计算器模板及源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java的集合LinkedHashSet详解

    Java的集合LinkedHashSet详解

    这篇文章主要介绍了Java的集合LinkedHashSet详解,LinkedHashSet介于HashSet和TreeSet之间,它也是一个hash表,但是同时维护了一个双链表来记录插入的顺序,需要的朋友可以参考下
    2023-09-09
  • IDEA手动添加junit4时出现的问题与解决方法

    IDEA手动添加junit4时出现的问题与解决方法

    这篇文章主要给大家介绍了关于IDEA手动添加junit4时出现的问题与解决方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论