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

//服务器

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);
        }

    }

最佳答案

您在同一writeObject上多次调用ObjectOutputStream-但是在阅读代码中,您只调用一次readObject

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

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


我怀疑现在到达流末尾时将引发异常-您可能应该有一些标记值(例如null)来指示“数据结束”。 (我认为ObjectInputStream不能为您做到这一点。)

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

从根本上讲,我认为您需要更仔细地考虑服务器与客户端之间的协议-考虑服务器是如何知道客户端何时完成一组输出以及何时应发送下一个进程名称的,等等。然后尝试重新设计代码,以使您只包装一次任何特定的流(尽管当然可以包装该包装器了)。最后,在一个方向上使用ObjectInputStream / ObjectOutputStream似乎很奇怪,而在另一个方向上仅使用纯文本。国际海事组织(IMO)在这样的程度上协议应该很少不对称。

08-04 17:29