熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

java多線程控制台聊天室

2022-06-13   來源: Java高級技術 

  package tfthread;

  import javaioBufferedReader;

  import javaioIOException;

  import javaioInputStreamReader;

  import javaioPrintWriter;

  import ServerSocket;

  import Socket;

  import javautilArrayList;

  import javautilList;

  import ncurrentExecutorService;

  import ncurrentExecutors;

  public class ChatServer {

  private static final int PORT = ; // 端口號

  private static List<Socket> list = new ArrayList<Socket>(); // 保存連接對象

  private ExecutorService exec;

  private ServerSocket server;

  public static void main(String[] args) {

  new ChatServer();

  }

  public ChatServer() {

  try {

  server = new ServerSocket(PORT);

  exec = ExecutorsnewCachedThreadPool();

  Systemoutprintln(服務器已啟動!);

  Socket client = null;

  while (true) {

  client = serveraccept(); // 接收客戶連接

  listadd(client);

  execexecute(new ChatTask(client));

  }

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  static class ChatTask implements Runnable {

  private Socket socket;

  private BufferedReader br;

  private PrintWriter pw;

  private String msg;

  public ChatTask(Socket socket) throws IOException {

  thissocket = socket;

  br = new BufferedReader(new InputStreamReader(socket

  getInputStream()));

  msg = + thissocketgetInetAddress() + 】進入聊天室!當前聊天室有【

  + listsize() + 】人;

  sendMessage();

  }

  public void run() {

  try {

  while ((msg = brreadLine()) != null) {

  if (msgtrim()equals(bye)) {

  listremove(socket);

  brclose();

  pwclose();

  msg = + socketgetInetAddress() + 】離開聊天室!當前聊天室有【

  + listsize() + 】人;

  socketclose();

  sendMessage();

  break;

  } else {

  msg = + socketgetInetAddress() + 】說 + msg;

  sendMessage();

  }

  }

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  /**

  * 群發消息給聊天室的所有人

  */

  private void sendMessage() throws IOException {

  Systemoutprintln(msg);

  for (Socket client : list) {

  pw = new PrintWriter(clientgetOutputStream() true);

  pwprintln(msg);

  }

  }

  }

  }

  

  package tfthread;

  import javaioBufferedReader;

  import javaioInputStreamReader;

  import javaioPrintWriter;

  import Socket;

  import ncurrentExecutorService;

  import ncurrentExecutors;

  public class ChatClient {

  private static final int PORT = ;

  private static ExecutorService exec = ExecutorsnewCachedThreadPool();

  public static void main(String[] args) throws Exception {

  new ChatClient();

  }

  public ChatClient() {

  try {

  Socket socket = new Socket( PORT);

  execexecute(new Sender(socket));

  Systemout

  println( + socketgetInetAddress() + 】您好歡迎來到阿飛聊天室!);

  BufferedReader br = new BufferedReader(new InputStreamReader(socket

  getInputStream()));

  String msg;

  while ((msg = brreadLine()) != null) {

  Systemoutprintln(msg);

  }

  } catch (Exception e) {

  }

  }

  /**

  * 客戶端線程獲取控制台輸入消息

  */

  static class Sender implements Runnable {

  private Socket socket;

  public Sender(Socket socket) {

  thissocket = socket;

  }

  public void run() {

  try {

  BufferedReader br = new BufferedReader(new InputStreamReader(

  Systemin));

  PrintWriter pw = new PrintWriter(socketgetOutputStream() true);

  String msg;

  while (true) {

  msg = brreadLine();

  pwprintln(msg);

  if (msgtrim()equals(bye)) {

  pwclose();

  brclose();

  execshutdownNow();

  break;

  }

  }

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27478.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.