得到了有关服务器 - 客户端的问题:爪哇 - 约插座:如何更改线程无需重新启动服务器
当服务器正在运行,如果我想在ServerThread一些变化,例如,字符串“欢迎您! “到“你好!”,是否可以执行,而无需重新启动服务器?现在,当我更改“欢迎!”时到别的地方,它仍然打印出“欢迎!”给客户。我必须关闭eclipse并重新启动服务器才能使用此新字符串。有什么办法可以解决这个问题吗?多谢!
服务器:
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();
}
}
ServerThread:
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();
}
}
}
客户:
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();
}
}
这个问题是为什么IDE的有时是新开发一个危险的工具的例子。 @Ferry,如果你将你编写的代码在Eclipse之外进行一段时间的演示,你将学习到很多关于Java的知识。然后回到这里并重新制定你的问题。 – Perception