如何编写一个基础web聊天室并逐步增强其功能?

更新于
2026-10-04 12:44:51
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计372个文字,预计阅读时间需要2分钟。

多线程聊天服务器简化版:javaimport java.io.*;import java.net.*;import java.util.*;

public class ChatServer { List clients=new ArrayList();

public static void main(String[] args) { new ChatServer().start(); }

public void start() { try { ServerSocket serverSocket=new ServerSocket(1234); System.out.println(服务器启动,等待客户端连接...);

while (true) { Socket clientSocket=serverSocket.accept(); clients.add(new PrintWriter(clientSocket.getOutputStream(), true));

new Thread(new ClientHandler(clientSocket)).start(); } } catch (IOException e) { e.printStackTrace(); } }

private class ClientHandler implements Runnable { private Socket clientSocket;

public ClientHandler(Socket socket) { this.clientSocket=socket; }

@Override public void run() { try { BufferedReader in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; while ((inputLine=in.readLine()) !=null) { System.out.println(收到: + inputLine); for (PrintWriter client : clients) { client.println(服务器: + inputLine); } } } catch (IOException e) { e.printStackTrace(); } finally { try { clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

多线程聊天

import java.io.*; import java.net.*; import java.util.*; public class HeartServer { List clients = new ArrayList (); public static void main(String[] args) { new HeartServer().start(); } public void start(){ try { boolean iConnect = false; ServerSocket ss = new ServerSocket(1720); iConnect = true; while(iConnect){ System.out.println("绑定服务器端口成功!"); Socket s = ss.accept(); ClientThread currentClient = new ClientThread(s);//创建线程引用 System.out.println("发现客户端!"); clients.add(currentClient);//把当前客户端加入集合 new Thread(currentClient).start(); System.out.println("客户端进程已经启动!"); } } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } } class ClientThread implements Runnable { private Socket s; private DataInputStream dis; private DataOutputStream dos; private String str; private boolean iConnect = false; ClientThread(Socket s){ this.s = s; iConnect = true; } public void run(){ System.out.println("run方法启动了!"); try { while(iConnect){ System.out.println("RUN方法中的while循环启动,正在等待客户端的发送消息..."); dis = new DataInputStream(s.getInputStream()); str = dis.readUTF(); System.out.println(str); for(int i=0; i

本文共计372个文字,预计阅读时间需要2分钟。

多线程聊天服务器简化版:javaimport java.io.*;import java.net.*;import java.util.*;

public class ChatServer { List clients=new ArrayList();

public static void main(String[] args) { new ChatServer().start(); }

public void start() { try { ServerSocket serverSocket=new ServerSocket(1234); System.out.println(服务器启动,等待客户端连接...);

while (true) { Socket clientSocket=serverSocket.accept(); clients.add(new PrintWriter(clientSocket.getOutputStream(), true));

new Thread(new ClientHandler(clientSocket)).start(); } } catch (IOException e) { e.printStackTrace(); } }

private class ClientHandler implements Runnable { private Socket clientSocket;

public ClientHandler(Socket socket) { this.clientSocket=socket; }

@Override public void run() { try { BufferedReader in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; while ((inputLine=in.readLine()) !=null) { System.out.println(收到: + inputLine); for (PrintWriter client : clients) { client.println(服务器: + inputLine); } } } catch (IOException e) { e.printStackTrace(); } finally { try { clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

多线程聊天

import java.io.*; import java.net.*; import java.util.*; public class HeartServer { List clients = new ArrayList (); public static void main(String[] args) { new HeartServer().start(); } public void start(){ try { boolean iConnect = false; ServerSocket ss = new ServerSocket(1720); iConnect = true; while(iConnect){ System.out.println("绑定服务器端口成功!"); Socket s = ss.accept(); ClientThread currentClient = new ClientThread(s);//创建线程引用 System.out.println("发现客户端!"); clients.add(currentClient);//把当前客户端加入集合 new Thread(currentClient).start(); System.out.println("客户端进程已经启动!"); } } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } } class ClientThread implements Runnable { private Socket s; private DataInputStream dis; private DataOutputStream dos; private String str; private boolean iConnect = false; ClientThread(Socket s){ this.s = s; iConnect = true; } public void run(){ System.out.println("run方法启动了!"); try { while(iConnect){ System.out.println("RUN方法中的while循环启动,正在等待客户端的发送消息..."); dis = new DataInputStream(s.getInputStream()); str = dis.readUTF(); System.out.println(str); for(int i=0; i