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

时间:2015-04-23 11:29:04

标签: java string client server lan

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

//服务器

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

    }

1 个答案:

答案 0 :(得分:1)

您在同一个writeObject上多次致电ObjectOutputStream - 但在阅读代码中,您只需拨打readObject 一次

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

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

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

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

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