有多个客户端连上服务器,我想在服务器这里短开其中一个客户端的连接,而保留其他客户端的连接,该如何做???

解决方案 »

  1.   

    什么客户? 是数据库的客户, 还是WEB的客户端, 还是代理服务器的客户?
      

  2.   

    我做了一个socket方面的通讯,如下代码,现想在下面代码来处理断开客户端的连接:public class ChatServer {
    boolean started = false;
    ServerSocket ss = null;

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

    public void start() {
    try {
    ss = new ServerSocket(8888);
    started = true;
    } catch (BindException e) {
    System.out.println("端口使用中....");
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {

    while(started) {
    Socket s = ss.accept();
    Client c = new Client(s);
    System.out.println("a client connected!");
    new Thread(c).start();
    //dis.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    class Client implements Runnable {
    private Socket s;
    private DataInputStream dis = null;
    private boolean bConnected = false;

    public Client(Socket s) {
    this.s = s;
    try {
    dis = new DataInputStream(s.getInputStream());
    bConnected = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();
    System.out.println(str);
    }
    } catch (EOFException e) {
    System.out.println("Client closed!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(dis != null) dis.close();
    if(s != null) s.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }

    }
    }