当前位置 : 主页 > 编程语言 > c++ >

web聊天室的简单代码,后面加强功能

来源:互联网 收集:自由互联 发布时间:2021-07-03
多线程聊天 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 iConn
多线程聊天
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
  
 
网友评论