同上 谢谢帮忙

解决方案 »

  1.   

    //这里是服务器端 客户端放在另一个帖了 这是以前学socket时候写的 你看行不
    import java.net.*;
    import java.io.*;
    import java.util.*;
    public class MyChatRoomServer {
    public static void main(String[] args)throws Exception {
    ServerSocket ss = new ServerSocket(8000);
    System.out.println("Starting...");
    List s = new ArrayList();
    while(true){
    Socket socket=ss.accept();
    System.out.println("connect");
    s.add(socket);
    new ChatRoomServer(s,socket).start();
    }
    }
    }class ChatRoomServer extends Thread{
    private BufferedReader in;
    private static PrintWriter out;
    private Socket socket;
    private List s;
    private boolean isStart=true;
    public ChatRoomServer( List s,Socket socket) {
    this.socket = socket;
    this.s=s;
    try {
    in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public void run(){
    while(isStart){
    try {
    String str;
    if((str=in.readLine())!=null){
    if(str.equals("78987")){
    isStart=false;
    socket.close();
    s.remove(socket);
    System.out.println("exiting");
    }else
    print(s,str);

    }catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    static void print(Collection c,String str){
    Iterator it = c.iterator();
    while(it.hasNext()){
    Object o = it.next();
    Socket soc =(Socket)o;
    try {
    out=new PrintWriter(soc.getOutputStream());
    } catch (IOException e) {
    e.printStackTrace();
    }
    out.println(str); 
    out.flush();
    }
    }
    }
      

  2.   

    感谢CodeFactory_yy()