Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - about socket: how to change thread without rebooting server
    primarykey
    data
    text
    <p>Got a problem about server-client:</p> <p>When server is running, if I want to make some change in ServerThread, for example, the string "Welcome!" to "Hello!", is that possible to execute that without restart the server? For now, when I change "Welcome!" to something else, it still print out "Welcome!" to client. I have to close eclipse and restart server for this new string working. Is there any way to solve that problem? Thx alot!</p> <p>Server:</p> <pre><code>import java.net.*; import java.io.*; public class ATMServer { private static int connectionPort = 8989; public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; try { serverSocket = new ServerSocket(connectionPort); } catch (IOException e) { System.err.println("Could not listen on port: " + connectionPort); System.exit(1); } System.out.println("Bank started listening on port: " + connectionPort); while (listening) new ATMServerThread(serverSocket.accept()).start(); serverSocket.close(); } } </code></pre> <p>ServerThread:</p> <pre><code>import java.io.*; import java.net.*; public class ATMServerThread extends Thread { private Socket socket = null; private BufferedReader in; PrintWriter out; public ATMServerThread(Socket socket) { super("ATMServerThread"); this.socket = socket; } public void run(){ try { out = new PrintWriter(socket.getOutputStream(), true); out.println("Welcome!"); } catch (IOException e){ e.printStackTrace(); } } } </code></pre> <p>Client: </p> <pre><code>import java.io.*; import java.net.*; import java.util.Scanner; public class Client { private static int connectionPort = 8989; public static void main(String[] args) throws IOException { Socket ATMSocket = null; PrintWriter out = null; BufferedReader in = null; String adress = ""; try { adress = "127.0.0.1"; } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Missing argument ip-adress"); System.exit(1); } try { ATMSocket = new Socket(adress, connectionPort); out = new PrintWriter(ATMSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader (ATMSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Unknown host: " +adress); System.exit(1); } catch (IOException e) { System.err.println("Couldn't open connection to " + adress); System.exit(1); } System.out.println(in.readLine()); out.close(); in.close(); ATMSocket.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload