问题描述:
服务器启动后 不侦听客户端的连接,为什么客户端连接时不报错???
SecurityManager 这个类的 checkAccept() 方法是不是能从客户端检查服务器是否在侦听?? 怎么用这方法?
项目中遇到问题:
网络出现故障后,客户端重新连接服务器,Socket socket = new Socket("192.168.1.88", 8888); 如果这里不报错的话,
客户端就会建立流向服务器发数据,读数据,这时会报错然后又重新连接,这样网络通以后服务器端就同时受到很多客户端连接,怎么处理这问题?
服务端:public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
                        Thread.sleep(10000);//睡眠10秒后开始侦听客户端连接
while(true) {
// Socket socket = server.accept();
// System.out.println(System.currentTimeMillis() + "  " + socket + "连接");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();

}客户端:public static void main(String[] args) {
try {
                        int i = 3;
while(i-- > 0) {
Socket socket = new Socket("192.168.1.88", 8888);
System.out.println(socket);
                        Thread.sleep(2000);
                        }
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}在服务器启动后 马上启动客户端, 等服务器启动10秒后连续收到3个客户端连接

解决方案 »

  1.   

    在服务器端用上一个LIST容器装客户端对象,在服务器启动的函数里while去不断连接客户端然后加到LIST容器对象中,开辟一个专门接受客户端连接的线程处理客户端的连接以后的事,就这样。希望楼主满意,把分给我:
    如import java.io.*;
    import java.net.*;
    import java.util.*;public class ChatServer {
    boolean started = false;
    ServerSocket ss = null;

    List<Client> clients = new ArrayList<Client>();

    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("端口使用中....");
    System.out.println("请关掉相关程序并重新运行服务器!");
    System.exit(0);
    } 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();
    clients.add(c);
    //dis.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

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

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

    public void send(String str) {
    try {
    dos.writeUTF(str);
    } catch (IOException e) {
    clients.remove(this);
    System.out.println("对方退出了!我从List里面去掉了!");
    //e.printStackTrace();
    }
    }

    public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();
    System.out.println(str);
    for(int i=0; i<clients.size(); i++) {
    Client c = clients.get(i);
    c.send(str);
    //System.out.println(" a string send !");
    }
    /*
    for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
    Client c = it.next();
    c.send(str);
    }
    */
    /*
    Iterator<Client> it = clients.iterator();
    while(it.hasNext()) {
    Client c = it.next();
    c.send(str);
    }
    */
    }
    } catch (EOFException e) {
    System.out.println("Client closed!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(dis != null) dis.close();
    if(dos != null) dos.close();
    if(s != null)  {
    s.close();
    //s = null;
    }

    } catch (IOException e1) {
    e1.printStackTrace();
    }


    }
    }

    }
    }
      

  2.   

    不能,SecurityManager 是安全管理器,需要在 java 安全策略文件中指定 java 应用具有哪些权限,或者不具有哪些权限。安全管理器的 checkAccept 并不是监听是否有客户端连接过来,而是确定是否有连接主机、端口的权限。这些权限是在 java 安全策略文件中指定的。Java 安全策略文件属于 Java 安全中重要的组成部分,在安全中非常重要,关于 Java 安全策略文件具体怎么配置资料也不是特别的多,可以从 JDK 文档上查阅:Default Policy Implementation and Policy File Syntax
    http://download.oracle.com/javase/6/docs/technotes/guides/security/PolicyFiles.htmlSecurity Managers and the Java SE JDK
    http://download.oracle.com/javase/6/docs/technotes/guides/security/smPortGuide.htmlPolicy Permissions
    http://download.oracle.com/javase/6/docs/technotes/guides/security/permissions.htmlmore...
    http://download.oracle.com/javase/6/docs/technotes/guides/security/index.html
      

  3.   

    SecurityManager 这个不能解决问题, 那我该怎么解决重连的问题啊?
    现在重连的时候 服务端偶尔会出现 同一时间点 同一个客户端的多次连接,但是客户端日志显示没有同一时间连多次。