gpt4 book ai didi

java - TCP服务器客户端,客户端仅在服务器套接字关闭时打印传入的字符串

转载 作者:行者123 更新时间:2023-12-01 16:31:52 25 4
gpt4 key购买 nike

对于一个学校项目,我们需要将服务器连接到客户端并从服务器获取统计信息。我们希望将统计信息打印到客户端的命令行中。服务器运行命令,将结果发送到客户端,客户端在其控制台中打印结果。但问题是客户端只有在serversocket关闭时才开始打印结果。

我们在 Ubuntu 虚拟机上运行服务器代码,在 Windows 笔记本电脑上运行客户端代码。当我们在本地运行这两个类时,它们会按照我们的意愿行事。但是当我们在虚拟机上运行服务器端时,客户端会等待套接字关闭。

客户端代码:

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


public class Client {
public static void main(String[] args) throws IOException, InterruptedException {

try{
Socket s = new Socket("localhost", 80);
s.setTcpNoDelay(true);
PrintWriter pr = new PrintWriter(s.getOutputStream());
pr.println("hello");
pr.flush();

InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in);

String str = bf.readLine();
System.out.println("server: " + str);

//todo dit moet in server.java gebeuren. Geen reden om elke keer hetzelfde commando door te sturen: wel extra veiligheidsrisicos
while (true){
String resultaat = bf.readLine();
System.out.println(resultaat);
Thread.sleep(1000);
}
}
catch(Exception e){
System.out.println("can't connect");
}
}
}

服务器代码:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {
public static void main(String[] args) throws IOException, InterruptedException {

ServerSocket ss = new ServerSocket(80);
Socket s = ss.accept();
s.setTcpNoDelay(true);

System.out.println("client connected");
try {
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in);
String welcome = bf.readLine();
System.out.println("Client: " + welcome);

PrintWriter pr = new PrintWriter(s.getOutputStream());
//grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}' ; uptime -p ; df /dev/sda1
String cmd = "ping www.google.com";

pr.println("Welcome client");
pr.flush();

//todo commando's in deze class zetten en om de zoveel tijd de resultaten doorsturen. Dus niet meer reageren op commando's
while (true) {
ArrayList<String> resultaat = runCommand(cmd);
for (String str : resultaat
) {
pr.println(str);
pr.flush();
}
Thread.sleep(1000);
}
}
catch (InterruptedException e){
s.close();
}
}

private static ArrayList<String> runCommand(String str) {
ArrayList<String> resultaten = new ArrayList<>();
try {
Process process = Runtime.getRuntime().exec(str);
BufferedReader ret = new BufferedReader(new InputStreamReader(process.getInputStream())); //vertaald bytes naar karakters van het eerste bericht van de client
String line; //output van bufferedreader

while ((line = ret.readLine()) != null) {
String resultaat = ret.readLine();
System.out.println(resultaat);
resultaten.add(resultaat);
System.out.println(ret.readLine());
}
System.out.println(resultaten);
} catch (IOException e) {
e.printStackTrace();
resultaten.add(e.getMessage());
}
return resultaten;
}
}

最佳答案

您将结果放在服务器的数组列表中,这就是它永远不会发送到客户端的原因,试试这个

public class Server {

public static void main(String[] args) throws IOException, InterruptedException {

ServerSocket ss = new ServerSocket(8000);
Socket s = ss.accept();
s.setTcpNoDelay(true);

System.out.println("client connected");
try {
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in);
String welcome = bf.readLine();
System.out.println("Client: " + welcome);

PrintWriter pr = new PrintWriter(s.getOutputStream());
// grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage
// "%"}' ; uptime -p ; df /dev/sda1
String cmd = "ping www.google.com";

pr.println("Welcome client");
pr.flush();

Process process = Runtime.getRuntime().exec(cmd);
BufferedReader ret = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (ret.readLine() != null) {
pr.println(ret.readLine());
pr.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
ss.close();
}
}
}

关于java - TCP服务器客户端,客户端仅在服务器套接字关闭时打印传入的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62023087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com