0

我做了这个脚本:

public class Server {
    ServerSocket serv = null;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;
    Socket conn = null;

    public Server() { 
        setLogger(getClass());
        setupSocketServer();
        listen();
    }

    public void listen() {
        try {
            while (true) {
                conn = serv.accept();
                getLogger().log(new LogRecord(Level.INFO, "Connection established from: " + conn.getInetAddress().getHostAddress()));
                out = new ObjectOutputStream(conn.getOutputStream());
                in = new ObjectInputStream(conn.getInputStream());
            }
        }
        catch (IOException ex) {
            getLogger().log(new LogRecord(Level.SEVERE, "Connection dropped from: " + conn.getInetAddress().getHostAddress()));
        } 
    }

    public void setupSocketServer() {
        try {
            serv = new ServerSocket(Config.PORT_NUMBER, Config.MAX_CONNECTIONS);
            getLogger().log(new LogRecord(Level.INFO, "Starting Server on: " + serv.getInetAddress().getHostAddress() + ":" + serv.getLocalPort()));
        }
        catch (IOException e) {
            getLogger().log(new LogRecord(Level.SEVERE, "Socket can not connect to host address"));
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        new Server();
    }

}

但是每当我打开我的客户端连接,然后再次关闭它并尝试重新打开时,服务器已经关闭了。我希望能够保持无限连接,允许多人连接。我该怎么做呢?

4

2 回答 2

1

为您的服务器尝试此代码,它由多个客户端组成,服务器将始终保持监听。

public class ServerTest {

    ServerSocket s;

    public void go() {

        try {
            s = new ServerSocket(44457);

            while (true) {

                Socket incoming = s.accept();
                Thread t = new Thread(new MyCon(incoming));
                t.start();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    class MyCon implements Runnable {

        Socket incoming;

        public MyCon(Socket incoming) {

            this.incoming = incoming;
        }

        @Override
        public void run() {

            try {
                PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                        true);
                InputStreamReader isr = new InputStreamReader(
                        incoming.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String inp = null;

                boolean isDone = true;

                System.out.println("TYPE : BYE");
                System.out.println();
                while (isDone && ((inp = br.readLine()) != null)) {

                    System.out.println(inp);
                    if (inp.trim().equals("BYE")) {
                        System.out
                                .println("THANKS FOR CONNECTING...Bye for now");
                        isDone = false;
                        s.close();
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                try {
                    s.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {

        new ServerTest().go();

    }

}
于 2012-06-16T02:48:34.317 回答
0

将 try/catch 块移动到“while”循环中。并不是说它会成为一个好的服务器,位应该在客户端断开连接后仍然存在。

于 2012-06-15T20:24:38.973 回答