-4

我是如何在另一个 java 程序中启动一个程序的新手!我不知道怎么称呼它。第一个问题是我必须在一个新线程中进行吗?第二个问题是如何调用新程序。我要调用的程序是“wondershaper”。我使用 ubuntu 12.04 运行这个程序并在命令行中编写。“sudo wondershaper eth0 10000 1000”。我如何在通用程序中编写它?我有一个服务器,我想处理它的速率!这就是我使用它的原因。所以我有一个多线程服务器,代码是

class Client extends Thread {
    private Socket connectionSocket;


    public Client(Socket c) throws IOException {
        connectionSocket = c;
    }

    public void run() {

        String path = "C:/pao/new2/";

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();


        try {

            String fileToSendStr = readFile();
            File fileToSend = null;
            for (File f : listOfFiles)

            {               
                if (f.getName().equals(fileToSendStr)) {
                    fileToSend = f;
                    break;
                }
            }
            //System.out.println("Connescting to Client to recieve the part " +fileToSendStr);
            if (fileToSend == null) {

            }
            System.out.println("Sending the chunk to Client " + fileToSendStr + "to the client: " +connectionSocket.getRemoteSocketAddress().toString());
            java.util.Date date= new java.util.Date();
             System.out.println(new Timestamp(date.getTime()));
            long length = fileToSend.length();
            byte [] longBytes = new byte[8];
            ByteBuffer bbuffer = ByteBuffer.wrap(longBytes);
            bbuffer.putLong(length);
            connectionSocket.getOutputStream().write(longBytes);

            BufferedOutputStream bout = new BufferedOutputStream(connectionSocket.getOutputStream());
            BufferedInputStream bain = new BufferedInputStream(new FileInputStream(fileToSend));

            byte buffer [] = new byte [1024];
            int i = 0;
            while((i = bain.read(buffer, 0, 1024)) >= 0){

                bout.write(buffer, 0, i);


            }



            System.out.println("chunk sended");
            java.util.Date date1= new java.util.Date();
             System.out.println(new Timestamp(date1.getTime()));
            bout.close();
            bain.close();


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

    }

    private String readFile() throws IOException {

        BufferedReader r = new BufferedReader(new InputStreamReader(
                connectionSocket.getInputStream()));
        return r.readLine();

    }
}

所以当我阅读客户端发送给我的 readFile 时。如果是速率字符串,则启动“wondershaper”并将速率放入“sudo wondershaper eth0 10000 速率”并启动程序

4

1 回答 1

1

Process aProcess = Runtime.getRuntime().exec("cmd");//你可以在这里传递任何进程

您还可以阅读该程序的输出。

InputStream is = aProcess.getInputStream();

Ps:您可以传递任何过程以及参数,但您不能传递诸如>>,2>或|之类的东西 或通配符如 *

——来自评论

于 2013-07-23T19:37:24.043 回答