Ryan Glenn
Ryan Glenn

Reputation: 1416

Java socket client connection and disconnection issue

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?

Upvotes: 0

Views: 558

Answers (2)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

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

    }

}

Upvotes: 1

kofemann
kofemann

Reputation: 4413

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

Upvotes: 0

Related Questions