'how to send data to C# client in real time from java server

I'm working on a project where the c# client will send a command to the java server and the java server will execute the command in the connected mobile phone and send the output to the c# client. My existing code sends the data to the server and the server executes it in the device and gives the output in the terminal. But here I want to send the data as soon as I receive it from the connected device. If I execute the ls command then the client should get each filename in real-time. My code to receive data from client

 ServerSocket ss = new ServerSocket(3000);
      
      Socket s = null;
          
        try 
        {
            // socket object to receive incoming client requests
            s = ss.accept();

             scanner = new Scanner(s.getInputStream());
             pw = new PrintWriter(s.getOutputStream(), true) ;
             dataFromClient = scanner.nextLine();
             //pw.write("thanks") // this is working

           }

to send data I created a method

 public static void senddata(String toreturn)
   {
    pw.write(toreturn);
    
   }

and call it from here like this

Runtime run = Runtime.getRuntime();
    Process pr = run.exec(cmd);
    pr.waitFor();
    
    BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = "";
    while ((line=buf.readLine())!=null) {
    ClientHandler2.senddata(line);   //sending data
        output=output+System.lineSeparator()+line;
        previuseline=line;
                
    
    }

But I don't receive anything from the server at the client. I really don't know how to achieve this as my output is always a minimum of 100 to 500 lines. please suggest any idea on the same.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source