[英]calling method on other java application via lan
i have 2 java applications connected to each other via LAN (wifi network) 我有2个Java应用程序通过LAN(WiFi网络)相互连接
the first one ServerApp.java 第一个ServerApp.java
public class ServerApp {
public static void zzz(){
System.out.println("hi");
}
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str =(String)dis.readUTF();
System.out.print("message : "+str);
ss.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
the second one ClientApp.java 第二个ClientApp.java
public class ClientApp {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
System.out.print("send message to the server ?[y/n]:");
String inputString=in.next();
if ("y".equals(inputString)) {
Socket s= new Socket("192.168.20.125", 6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("hellow server\n");
dout.writeUTF("zzz");
dout.flush();
dout.close();
s.close();
} else if ("n".equals(inputString)) {
System.out.println("exit");
} else {
System.out.println("error: you should enter a valid value");
}
} catch (IOException e) {
System.out.println(e);
}
}
}
what happens is, the client app send a message to the server app via LAN using the server IP address - the server app have a method call zzz() so all I want is how do I make the client app call this method ( if possible ) 发生的情况是,客户端应用程序使用服务器IP地址通过LAN向服务器应用程序发送消息-服务器应用程序具有方法调用zzz(),因此,我想要做的就是如何使客户端应用程序调用此方法(如果可能的话) )
thanks 谢谢
@MichalLonski how to I make the "obj" indicate to the ServerApp @MichalLonski如何使“ obj”指示给ServerApp
As it is static
method you have to point ServerApp.class
, like below: 由于它是static
方法,因此必须指向ServerApp.class
,如下所示:
public class ServerApp {
public static void zzz() {
System.out.println("hi");
}
public static void main(String[] args) throws Exception {
String methodName = "zzz";
java.lang.reflect.Method method = ServerApp.class.getMethod(methodName);
method.invoke(ServerApp.class);
}
}
You can change it to use not static, but instance methods. 您可以将其更改为不使用静态方法,而是使用实例方法。 In order to do that you have to create an instance of ServerApp class, like this: 为此,您必须创建ServerApp类的实例,如下所示:
public class ServerApp {
public void foo() {
System.out.println("Hello there from non static method!");
}
public static void main(String[] args) throws Exception {
String methodName = "foo";
ServerApp app = new ServerApp();
java.lang.reflect.Method method = app.getClass().getMethod(methodName);
method.invoke(app);
}
}
Edit : 编辑 :
If you want to specify also the class of which method you want to call, you can do it this way: 如果还要指定要调用的方法的类,则可以通过以下方式进行操作:
package com.example;
class Foo {
public static void bar() {
System.out.println("Hello there.");
}
}
public class ServerApp {
public static void main(String[] args) throws Exception {
//read the class and method name from the socket
String className = "com.example.Foo";
String methodName = "bar";
Class<?> clazz = Class.forName(className);
clazz.getMethod(methodName).invoke(clazz);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.