gpt4 book ai didi

java - 从客户端传输到服务器时字符串被截断 - Java

转载 作者:行者123 更新时间:2023-11-30 11:04:58 31 4
gpt4 key购买 nike

我正在尝试将客户端系统的详细信息发送到服务器系统,但服务器没有收到任何信息。一旦到达第一行,它就会停止打印。有人帮我解决了这个问题。

//服务器

void connect_clients() throws ClassNotFoundException, InterruptedException
{
try {
ServerSocket listener = new ServerSocket(7700);
jButton1.setText("Server Running!");
jButton1.setEnabled(false);
ObjectInputStream ois; // = new ObjectInputStream(socket.getInputStream());
while (true) {

socket = listener.accept();
socketList.add(socket);


ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message Received: " + message); */*/*this is not printing everything

}

}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}

//客户端

void connect_server() throws IOException
{
try {
// TODO code application logic here
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
s = new Socket(serverAddress, 7700);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = null;
BufferedReader input;
String answer;
while(true){

input =
new BufferedReader(new InputStreamReader(s.getInputStream()));

DataOutputStream dOut = new DataOutputStream(s.getOutputStream());


answer = input.readLine();
System.out.println(answer);
if(answer != null)
{
oos.reset();
String line = "";
String command = "powershell.exe Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | \n" +
"Format-Table –AutoSize";
// Executing the command
Process powerShellProcess = Runtime.getRuntime().exec(command);
// Getting the results
powerShellProcess.getOutputStream().close();

System.out.println("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
byte[] mybytearray = new byte[(int) line.length()];
oos.writeObject(mybytearray);

}

stdout.close();
System.out.println("Standard Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {

oos.writeObject(line);
System.out.println("fdg"+line);
//printing the output to a file --start
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)))) {
out.println(line);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e);
}

}
stderr.close();
System.out.println("Done");
}
}
}

catch (ConnectException e) {

JOptionPane.showMessageDialog(null, e);
}
catch (SocketException e) {

JOptionPane.showMessageDialog(null, e);
}

}

最佳答案

您在同一 ObjectOutputStream 上多次调用 writeObject - 但在读取 代码中,您只调用了 读取对象 一次

您需要循环读取代码,例如

while (true) {
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
}

现在当它到达流的末尾时会抛出异常,我怀疑 - 您可能应该有一些标记值(例如 null)来指示“数据结束”。 (我不认为 ObjectInputStream 只是为您做这件事。)

此外,在您的客户端代码中,您正在创建多个 InputStreamReader 对象来包装同一个套接字的 InputStream - 这是一个坏主意。

从根本上说,我认为您需要更仔细地考虑服务器和客户端之间的协议(protocol) - 考虑服务器如何知道客户端何时完成一组输出,以及何时发送下一组输出进程名称等。然后尝试重新设计您的代码,以便您只包装任何特定流一次(当然,该包装器随后可以自行包装)。最后,在一个方向上使用 ObjectInputStream/ObjectOutputStream,而在另一个方向上只使用纯文本似乎很奇怪。 IMO,协议(protocol)很少会不对称到那种程度。

关于java - 从客户端传输到服务器时字符串被截断 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822111/

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