首先让我们从 Hello World 开始。

服务器

  1. package hprose.tcphelloexam;
  2. import hprose.server.HproseTcpServer;
  3. public class TCPHelloServer {
  4. public static String hello(String name) {
  5. return "Hello " + name + "!";
  6. }
  7. public static void main(String[] args) throws Exception {
  8. HproseTcpServer server = new HproseTcpServer("tcp://localhost:4321");
  9. server.add("hello", TCPHelloServer.class);
  10. server.start();
  11. System.out.println("START");
  12. System.in.read();
  13. server.stop();
  14. System.out.println("STOP");
  15. }
  16. }

客户端

  1. package hprose.tcphelloexam;
  2. import hprose.client.HproseTcpClient;
  3. interface IHello {
  4. String hello(String name);
  5. }
  6. public class TCPHelloClient {
  7. public static void main(String[] args) throws Throwable {
  8. System.out.println("START");
  9. HproseTcpClient client = new HproseTcpClient("tcp://localhost:4321");
  10. IHello helloClient = client.useService(IHello.class);
  11. System.out.println(helloClient.hello("World"));
  12. System.out.println("END");
  13. }
  14. }