做个聊天室的程序 c/s模式 运行的时候聊天的时候没什么问题
但是有用户退出时别的用户就不能正常的聊天了 抛出异常是SocketException Software caused connection abort: socket write error

解决方案 »

  1.   

    服务器端的:import java.io.*;
    import java.net.*;
    import java.util.*;public class ServerControl implements ButtonListener_Server
    {
    private boolean isstart = false;
    private Server s = null;
    private ServerSocket server ;
    private Collection user_client = new Vector();


    public ServerControl()
    {
    s =  new Server();
    s.startButtonListener(this);
    s.quitButtonListener(this);
    s.ShowFrame();
    }

    public void startButton()
    {
    if(isstart == false)
    {
    try
    {
    server = new ServerSocket(8000);
    new Thread(new StartConnect()).start();
    isstart = true;
    }
    catch(IOException e)
    {
    System.out.println("ERROR3!");
    }

    }
    else
    {
    s.append_text_system("服务器已开启!"+"\n");
    }
    }
    public void quitButton()
    {
    System.exit(0);
    }

    public void send_ALL_MSG(Socket _socket, String _message) throws IOException 
    {
    for(Iterator it = user_client.iterator();it.hasNext();)
    {
    ClientConnect c = (ClientConnect)it.next();
    if(c.equals(_socket)==false)
    {
    c.send_message(_message);
    //System.out.println("!!!!!!!!!!!");
    }
    }

    }
    public void send_user_name(Socket _socket) throws IOException 
    {
    String user_msg = "USER_NAME/";
    ClientConnect uc = null;
    for(Iterator it = user_client.iterator();it.hasNext();)
    {
    ClientConnect c = (ClientConnect)it.next();
    if(c.equals(_socket)==false)
    {
    user_msg = user_msg+c.get_user_name()+"|";
    }
    else
    {
    uc = c;
    }
    }
    //s.append_text_system(user_msg);
    uc.send_message(user_msg);
    System.out.println(user_msg);
    }
    class StartConnect implements Runnable
    {
    public void run()
    {
    try
    {
    s.set_text_system("欢迎进入聊天室...."+"\n");
    while (true)
    {
    Socket client =server.accept();
    ClientConnect cc = new ClientConnect(client);
    new Thread(cc).start(); 
    user_client.add(cc);


    //System.out.println("Server Thread!");
    }
    }
    catch(IOException e)
    {
    System.out.println("ERROR1!");
    }
    }
    }

    class ClientConnect implements Runnable
    {
    private Socket client = null;
    private String str = null;
    private String message = new String("");
    private String user_name = new String("");
    private String user_ip = new String("");


    public ClientConnect(Socket _client)
    {
    //Thread Thread1 =new Thread(this);
    //Thread1.start();
    this.client =  _client;
    } public void set_user_name(String _id)
    {
    this.user_name = _id;
    }
    public String get_user_name()
    {
    return this.user_name;
    }
    public void set_user_ip(String _ip)
    {
    this.user_ip = _ip;
    }
    public String get_user_ip()
    {
    return this.user_ip;
    }

    public boolean equals(Socket s)
    {
    if(this.client == s) 
    return true;
    else 
    return false;
    }
    public void send_message(String _message) throws IOException 
    {
    DataOutputStream out = new DataOutputStream(client.getOutputStream());
    out.writeUTF(_message);
    System.out.println("发送的消息为:"+_message);
    }

    public void run()
    {
    try
    {

    DataInputStream in = new DataInputStream(client.getInputStream());
    str = in.readUTF(in);
    //System.out.print(str.substring(str.indexOf(":")+1));
    while(str!= null && str.length()!=0)
    {

    //s.append_text_system(str+"\n");
    //s.append_text_system(str.substring(0,str.indexOf("/")));
    if(str.substring(0,str.indexOf("/")).equals("NEW"))
    {
    //System.out.println(str.substring(0,str.indexOf("/")));
    this.set_user_ip(client.getInetAddress().getHostAddress());
    this.set_user_name(str.substring(str.indexOf("/")+1,str.indexOf(":")));
    //System.out.println(client.getInetAddress().getHostAddress());
    //System.out.println(str.substring(str.indexOf("/")+1,str.indexOf(":")));
    //System.out.println(this.get_user_name());
    send_ALL_MSG(this.client,"NEW"+"/"+str.substring(str.indexOf("/")+1));
    send_user_name(this.client);
    s.append_text_system("新用户:"+str.substring(str.indexOf("/")+1,str.indexOf(":"))+"已登陆"+"\n");


    }
    else if(str.substring(0,str.indexOf("/")).equals("CLOSE"))
    {
    System.out.println(str.substring(0,str.indexOf("/")));
    send_ALL_MSG(this.client,"CLOSE"+"/"+str.substring(str.indexOf("/")+1));
    user_client.remove(this.client);
    System.out.println("用户退出!");

    //in.close();
    //client.close();

    //System.out.println((str.substring(0,str.indexOf("/"))));
    }
    else if(str.substring(0,str.indexOf("/")).equals("ALL"))
    {
    send_ALL_MSG(this.client,"ALL"+"/"+this.get_user_name()+str.substring(str.indexOf(":")));
    //System.out.println((str.substring(0,str.indexOf("/"))));
    }
    else if(str.substring(0,str.indexOf("/")).equals("MSG"))
    {
    String accept_name = str.substring(str.indexOf("/")+1,str.indexOf(":"));
    String msg = this.get_user_name()+str.substring(str.indexOf(":"));
    for(Iterator it = user_client.iterator();it.hasNext();)
    {
    ClientConnect c = (ClientConnect)it.next();
    if(c.get_user_name().equals(accept_name))
    {
    c.send_message("MSG"+"/"+msg);
    }
    }

    System.out.println((str.substring(0,str.indexOf("/"))));
    }
    else 
    {
    s.append_text_system("发送消息有误!"+"\n");
    }
    str = in.readUTF(in);
    }
    }
    catch(IOException e)
    {
    System.out.println("ERROR2!");
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args)
    {
    ServerControl sc = new ServerControl();
    }
    }