【发布时间】:2018-04-06 08:15:18
【问题描述】:
我有 2 个 java 应用程序通过 LAN(wifi 网络)相互连接
第一个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);
}
}
}
第二个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);
}
}
}
发生的情况是,客户端应用程序使用服务器 IP 地址通过 LAN 向服务器应用程序发送消息 - 服务器应用程序有一个方法调用 zzz() 所以我想要的是如何让客户端应用程序调用此方法(如果可能的话)
谢谢
【问题讨论】:
-
@MichalLonski 如何让“obj”指示给 ServerApp ??