The overall communication process is as follows:
Package search; import Java. io. inputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. io. outputstream; import Java. lang. reflect. method; import java.net. serversocket; import java.net. socket; import Java. util. hashmap; import Java. util. map; public class simpleserver {private map remoteobjects = new hashmap (); Public void register (string classname, object remoteobject) {remoteobjects. put (classname, remoteobject);} public void Service () throws exception {serversocket = new serversocket (8003); system. out. println ("server has started"); While (true) {Socket socket = serversocket. accept (); inputstream in = socket. getinputstream (); objectinputstream oin = new objectinputstream (in); call = (CALL) oin. readobject (); system. out. println (CALL); outputstream out = socket. getoutputstream (); objectoutputstream oout = new objectoutputstream (out); Call = invoke (CALL); oout. writeobject (CALL); oin. close (); oout. close (); socket. close () ;}} private call invoke (call) {object result; try {string classname = call. getclassname (); string methodname = call. getmethodname (); object [] Params = call. getparams (); Class classtype = Class. forname (classname); Class [] paramtypes = call. getparamtypes (); Method method = classtype. getmethod (methodname, paramtypes); object remoteobject = remoteobjects. get (classname); If (remoteobject = NULL) {Throw new exception (the remote object of classname + "does not exist");} else {result = method. invoke (remoteobject, Params) ;}} catch (exception e) {result = E;} call. setresult (result); return call;} public static void main (string [] ARGs) throws exception {simpleserver Server = new simpleserver (); server. register ("search. helloservice ", new helloserviceimpl (); server. service ();}}
package search;import java.util.Date;public class HelloServiceImpl implements HelloService{public String echo(String msg) {return "echo "+msg;}public Date getTime() {return new Date();}}
package search;import java.util.Date;public interface HelloService {public String echo(String msg);public Date getTime();}
Package search; import Java. io. serializable; public class call implements serializable {private string classname; // indicates the class name or interface name private string methodname; // indicates the method name private class [] paramtypes; // indicates the method parameter type private object [] Params; // indicates the method parameter value // indicates the method execution result private object result; public call () {} public call (string classname, string methodname, class [] paramtypes, object [] Params) {This. classname = classname; this. methodname = methodname; this. paramtypes = paramtypes; this. params = Params;} Public String getclassname () {return classname;} public void setclassname (string classname) {This. classname = classname;} Public String getmethodname () {return methodname;} public void setmethodname (string methodname) {This. methodname = methodname;} public class [] getparamtypes () {return paramtypes;} public void setparamtypes (class [] paramtypes) {This. paramtypes = paramtypes;} public object [] getparams () {return Params;} public void setparams (object [] Params) {This. params = Params;} public object getresult () {return result;} public void setresult (Object result) {This. result = result;} Public String tostring () {return "classname =" + classname + "methodname =" + methodname ;}}
package search;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;public class SimpleClient {public void invoke(){try{Socket socket=new Socket("localhost",8003);InputStream in=socket.getInputStream();OutputStream out=socket.getOutputStream();ObjectOutputStream oout=new ObjectOutputStream(out);Call call=new Call("search.HelloService","echo",new Class[]{String.class},new Object[]{"hello"});System.out.println(call);oout.writeObject(call);ObjectInputStream oin=new ObjectInputStream(in);call=(Call)oin.readObject();System.out.println(call.getResult());oin.close();oout.close();socket.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[]args){new SimpleClient().invoke();}}