import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class ChatRoomServer { public static void main(String[] args) { ServerSocket ss = null; MySocket mySocket = null; try { ss = new ServerSocket(8000); while (true) { Socket socket = ss.accept(); mySocket = new MySocket(socket); Service s = new Service(mySocket); Thread t = new Thread(s); t.start(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != ss) { ss.close(); } } catch (IOException e) { e.printStackTrace(); } mySocket.close(); } } } class Service implements Runnable { private MySocket mySocket; private static List<MySocket> clientList = new ArrayList<MySocket>(); private String ip; public Service(MySocket mySocket) { super(); this.mySocket = mySocket; clientList.add(mySocket); ip = mySocket.getIp(); try { MySocket client; int count = clientList.size(); for (int i = 0; i < count; i++) { client = clientList.get(i); client.send("欢迎" + ip + "上线..."); } } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { String line = null; try { while (true) { line = mySocket.receive(); if (null == line || line.length() == 0) continue; MySocket client; int count = clientList.size(); for (int i = 0; i < count; i++) { client = clientList.get(i); client.send(ip + "说:" + line); } } } catch (IOException e) { e.printStackTrace(); } } }


dapeng0011 LV15
2024年7月18日
微信网友_7004855557083136 LV1
2024年5月22日
Ji123455 LV8
2023年9月21日
cksndh LV4
2023年8月16日
yybb7435100 LV2
2023年8月7日
buhuia LV4
2023年6月7日
林间听风 LV10
2023年4月7日
linmou LV8
2023年3月19日
gzryue LV6
2023年3月8日
heqian LV17
2023年1月10日