简体   繁体   English

将命令发送到正在运行的Java进程

[英]Send commands to a running java process

I am making an HTTP server for a networking class that I am attending,I have made the server and now I want to be able to run the server in a terminal (say the name of the executable is myserver) and by opening another terminal I want to be able to issue commands to the already running process of the server,so instead of typing myserver to start the server ,I want to be able to type for example myserver --quit and the quit command will be send to the already running instance of the server,just like adb for android works. 我正在为正在参加的网络课程制作HTTP服务器,已经完成了服务器的创建,现在我希望能够在终端中运行服务器(例如,可执行文件的名称为myserver),然后打开另一个终端我希望能够向服务器已经运行的进程发出命令,所以与其键入myserver来启动服务器, myserver --quit输入myserver --quit并将quit命令发送到已经运行的服务器服务器实例,就像适用于android的adb一样。

So the flow of events should be something like that: 因此,事件流应该是这样的:

Terminal 1: 1号航站楼:

user$:myserver
server is running and listening

Terminal 2: 2号航站楼:

user$:myserver
server is already running
user$:myserver --quit
server has stopped

Whats the general idea behind that? 这背后的一般想法是什么?

You can achieve this with Sockets. 您可以使用套接字来实现。 The first terminal is the Server and the second one is the client. 第一个终端是服务器,第二个终端是客户端。 Then you can send/receive messages and handle them in the server. 然后,您可以发送/接收消息并在服务器中处理它们。 Look at Oracle's example of client-server http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html 查看Oracle的客户端服务器示例http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM