如何添加当前在Java套接字服务器上运行的程序列表?

问题描述 投票:0回答:2

我已经设置了Java套接字服务器。我希望客户端能够输入“正在运行的进程”,这将导致服务器打印在服务器上运行的所有程序。服务器代码如下:

// Java implementation of Server side
// It contains two classes : Server and ClientHandler
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

// Server class
public class Server
{
public static void main(String[] args) throws IOException
{
 // server is listening on port 5056
    ServerSocket ss = new ServerSocket(5056);

    // running infinite loop for getting
    // client request
    while (true)
    {
        Socket s = null;

        try
        {
            // socket object to receive incoming client requests
            s = ss.accept();

            System.out.println("A new client is connected : " + s);

            // obtaining input and out streams
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

            System.out.println("Assigning new thread for this client");

            // create a new thread object
            Thread t = new ClientHandler(s, dis, dos);

            // Invoking the start() method
            t.start();

        }
        catch (Exception e){
            s.close();
            e.printStackTrace();
        }
    }
}
 }



 // ClientHandler class
 class ClientHandler extends Thread
 {
DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");
DateFormat fortime = new SimpleDateFormat("hh:mm:ss");
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;


 // Constructor
public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)
{
    this.s = s;
    this.dis = dis;
    this.dos = dos;
}

@Override
public void run()
{
    RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();
    String received;
    String toreturn;
    long uptime;
    int socket = 0;
    while (true)
    {
        try {

            // Ask user what he wants
            dos.writeUTF("What do you want?[Date | Time | Uptime | Memory Use | Netstat | Current 
 Users]..\n"+
                    "Type Exit to terminate connection.");

            // receive the answer from client
            received = dis.readUTF();

            if(received.equals("Exit"))
            {
                System.out.println("Client " + this.s + " sends exit...");
                System.out.println("Closing this connection.");
                this.s.close();
                System.out.println("Connection closed");
                break;
            }
 // creating Date object
            Date date = new Date();

 // write on output stream based on the
 // answer from the client
            switch (received) {

                case "Date" :
                    toreturn = fordate.format(date);
                    dos.writeUTF(toreturn);
                    break;

                case "Time" :
                    toreturn = fortime.format(date);
                    dos.writeUTF(toreturn);
                    break;

                case "Uptime" :
                    uptime = runtimeMX.getUptime();
                    dos.writeUTF(uptime + " ms");
                    break;

                case "Memory Use" :
                    long memory = Runtime.getRuntime().totalMemory() - 
  Runtime.getRuntime().freeMemory();
                    dos.writeUTF(memory + " bytes");
                    break;

                case "Netstat" :
                    s.getInputStream();
                    dos.writeUTF( s+ " bytes");
                    break;

                case "Current Users" :
                    s.getInputStream();
                    dos.writeUTF( s+ " bytes");
                    break;

                default:
                    dos.writeUTF("Invalid input");
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try
    {
  // closing resources
        this.dis.close();
        this.dos.close();

    }catch(IOException e){
        e.printStackTrace();
    }
}
}

我希望添加一个名为“运行进程”的案例文件。如果用户输入“正在运行的进程”,则将向他们显示服务器上当前正在运行的程序。

java serversocket socketserver single-threaded
2个回答
0
投票

您必须使用系统调用来实现这一点,由于对代码可移植性的负面影响,我建议您尽量避免在代码中使用它:

           Process proc = Runtime.getRuntime().exec("<command calling task manager to list processes (OS-specific)>");

           InputStream is = proc.getInputStream();
           InputStreamReader isreader= new InputStreamReader(is);
           BufferedReader br = new BufferedReader(isreader);
           String line = null;
           while ((line = br.readLine()) != null)
           {
               System.out.println(line);
               //parse the line according to your needs 
           }

0
投票

请参阅此堆栈溢出线程:

© www.soinside.com 2019 - 2024. All rights reserved.