Java套接字客户端连接和断开连接问题

时间:2012-06-15 19:21:10

标签: java client serversocket

我制作了这个剧本:

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();
    }

}

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

2 个答案:

答案 0 :(得分: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();

    }

}

答案 1 :(得分:0)

将try / catch块移动到'而#39;环。不是说它'将成为一个goot服务器,位应该在客户端断开连接。