Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:44:42+00:00 2026-06-13T01:44:42+00:00

I am using Oracle’s Knock Knock example for programming a server and client whereby

  • 0

I am using Oracle’s Knock Knock example for programming a server and client whereby when the client connects to the server it receives a Knock Knock joke to interact with. Everything is running great.

My question though is when the client is done and disconnects itself from the server the server program terminates too. How if the server terminated can I have it auto run again so that it can accept another client’s request without having to manually restart the server side?

Below is my code where the comment shows where I’m stuck. I am using two centos vm’s running on vmware player.

import java.net.*;
import java.io.*;

public class KnockKnockServer {
    public static void main(String[] args) throws IOException {
    server(); 
    /*HERE IS WHERE I AM STUCK
     * If I call server() above in my main method everything runs great.
     * However, I would like to put a timer or something inside main so that 
     * every second it checks to see if server() is still running and if not
     * then start it up again.
     */
    }


    public static void server() throws IOException{
    ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        KnockKnockProtocol kkp = new KnockKnockProtocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:44:43+00:00Added an answer on June 13, 2026 at 1:44 am

    You could move the connection handling code to an extra thread (lets call it KnockKnockHandler):

    public class KnockKnockHandler extends Thread {
        private Socket socket;
        public KnockKnockWorker(Socket socket) {
           this.socket = socket;
        }
        public void run() {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(
                socket.getInputStream()));
            String inputLine, outputLine;
            KnockKnockProtocol kkp = new KnockKnockProtocol();
    
            outputLine = kkp.processInput(null);
            out.println(outputLine);
    
            while ((inputLine = in.readLine()) != null) {
                 outputLine = kkp.processInput(inputLine);
                 out.println(outputLine);
                 if (outputLine.equals("Bye."))
                    break;
            }
            out.close();
            in.close();
            socket.close();
        }
    }
    

    Once somebody connects to your server you create a new instance of KnockKnockHandler and start it:

    try {
        clientSocket = serverSocket.accept();
        KnockKnockHandler handler = new KnockKnockHandler(clientSocket);
        handler.start();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }
    

    If you take this into a loop your server will never shut down (except an exception is thrown or the JVM is terminated 😉 ):

    while(true) {
        try {
            clientSocket = serverSocket.accept();
            KnockKnockHandler handler = new KnockKnockHandler(clientSocket);
            handler.start();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
    }
    

    If a client connects your server will start a new Thread and go into the next iteration of the loop and wait for the next client to connect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently using Oracle/BEA/Plumtree ALUI Portal 6.1, and am trying to display a client's IP
I'm using Oracle 9.2 with Weblogic 8 server. I'm getting the Data from a
I am using Oracle.DataAccess.Client to work with Oracle database in my ASP.Net application. There
i am using Oracle Application server 10.1.2.0.2. I am trying to connect an oracle
Using Oracle 10gR2, I need to produce something like the following pseudo-example from data
Using Oracle, how can I find index names and creation dates from systables/information_schema? How
Using oracle database. Here's how i think the SQLException happens... Say i have two
when using oracle forms to generate md5 hash, i get result that is different
I'm using Oracle 11g Schema I want to select the month and day from
I am using Oracle 11GR2 version. I exported a dump successfully using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.