0

I made this script:

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

}

But whenever I open my client connection, then close it again and try to re-open, the server has already closed out. I want to be able to keep an infinite connection which allows multiple people to connect. How would I go about doing this?

1
  • you probably want to do a while(true) {Socket s = serv.accept(); (new Thread(new MyController(s))).start();} design paradigm Commented Jun 15, 2012 at 19:24

2 Answers 2

1

Try this code for your server, its made up for multiple client, and the server will remain listening always.

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

    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Move try/catch block into 'while' loop. Not that it' will make a goot server, bit should survive client disconnects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.