如何让我的服务器打印来自客户端 class 的输入?
How do I get my server to print the input from my client class?
我正在创建一个客户端服务器程序,它可以工作,但是我希望我的服务器能够输出来自我的客户端的输入 类,但我不知道该怎么做?显然它会链接到我的 input/output 流,但我不知道该怎么做。
您最好使用 DataInputStream
和 DataOutputStream
在服务器和客户端之间进行通信。
在您的服务器 class 中实施 Runnable
并将 run()
方法添加到其中。在while (true)
循环中,从Client接收数据,输出如下:
public void run() {
while (true) {
try {
String line = dis.readUTF();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在您的客户端 class 中,执行如下操作:
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
dos.writeUTF(line);
希望这对您有所帮助。
我正在创建一个客户端服务器程序,它可以工作,但是我希望我的服务器能够输出来自我的客户端的输入 类,但我不知道该怎么做?显然它会链接到我的 input/output 流,但我不知道该怎么做。
您最好使用 DataInputStream
和 DataOutputStream
在服务器和客户端之间进行通信。
在您的服务器 class 中实施 Runnable
并将 run()
方法添加到其中。在while (true)
循环中,从Client接收数据,输出如下:
public void run() {
while (true) {
try {
String line = dis.readUTF();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在您的客户端 class 中,执行如下操作:
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
dos.writeUTF(line);
希望这对您有所帮助。