21xrx.com
2024-11-10 00:36:47 Sunday
登录
文章检索 我的文章 写文章
Java多线程网络聊天室代码实现
2023-07-08 15:59:50 深夜i     --     --
Java 多线程 网络聊天室 代码实现

网络聊天室是目前非常流行的一种社交方式,Java多线程可以很好的实现网络聊天室的功能。下面我们来看看Java多线程网络聊天室的代码实现。

1. 服务器端代码

服务器端实现主要包括两部分,一部分是建立网络连接,另一部分是进行多线程处理。

网络连接的建立使用Socket类进行,可以通过ServerSocket类监听客户端的请求。每当一个客户端连接到服务器时,服务器端都会开启一个新的线程来为该客户端提供服务。


import java.io.*;

import java.net.*;

public class Server {

  public static void main(String[] args) {

    try (

      ServerSocket serverSocket = new ServerSocket(1234);

    ) {

      System.out.println("Server started.");

      int counter = 0;

      while (true) {

        Socket clientSocket = serverSocket.accept();

        counter++;

        System.out.println("Client " + counter + " connected.");

        ClientHandler clientHandler = new ClientHandler(clientSocket, counter);

        new Thread(clientHandler).start();

      }

    } catch (IOException e) {

      System.out.println("Exception caught when trying to listen on port or listening for a connection");

      System.out.println(e.getMessage());

    }

  }

}

class ClientHandler implements Runnable {

  private final Socket clientSocket;

  private final int clientNumber;

  public ClientHandler(Socket socket, int clientNumber)

    this.clientSocket = socket;

    this.clientNumber = clientNumber;

  

  public void run() {

    try (

      PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    ) {

      String inputLine;

      while ((inputLine = in.readLine()) != null) {

        System.out.println("Client " + clientNumber + " sent: " + inputLine);

        out.println(inputLine);

      }

    } catch (IOException e) {

      System.out.println("Exception caught when trying to communicate with client " + clientNumber);

      System.out.println(e.getMessage());

    }

  }

}

2. 客户端代码

客户端的代码相对于服务器端来说比较简单,只需要建立连接后发送和接收消息即可。


import java.io.*;

import java.net.Socket;

import java.util.Scanner;

public class Client {

  public static void main(String[] args) {

    try (

      Socket socket = new Socket("localhost", 1234);

      PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

      Scanner scanner = new Scanner(System.in);

    ) {

      String inputLine;

      String outputLine;

      while (true) {

        System.out.println("Enter message: ");

        outputLine = scanner.nextLine();

        out.println(outputLine);

        if ((inputLine = in.readLine()) != null) {

          System.out.println("Server replied: " + inputLine);

        }

      }

    } catch (IOException e) {

      System.out.println("Exception caught when trying to communicate with server");

      System.out.println(e.getMessage());

    }

  }

}

以上就是Java多线程网络聊天室的代码实现,通过多线程的方式,可以实现同时为多个客户端提供服务。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复