---
title: "RPC 远程过程调用-非webservices"
author: "navy0168"
date: "2010-07-28T16:19:16Z"
tags: ["XML"]
categories: ["编程语言"]
slug: "rpc-webservices"
draft: false
lastmod: "2011-05-09T16:28:39Z"
id: 723499
url: "https://navylee.iteye.com/blog/723499"
---

服务器端
public class Server {

	public static void main(String[] args) throws IOException {
		// XML解析器
		XmlRpc.setDriver(SAXParser.class);
		// 监听端口
		WebServer server = new WebServer(8888);
		// 业务处理类
		server.addHandler("hello", new ServerHandler());
		// 启动
		server.start();
		System.out.println("服务器已经启动:127.0.0.1:8888");
	}
}


客户端
public class Client {

	public static void main(String args[]) throws XmlRpcException, IOException {
		// XML解析器
		XmlRpc.setDriver(SAXParser.class);
		// 定位服务器
		XmlRpcClient client = new XmlRpcClient("http://127.0.0.1:8888");
		// 请求参数
		Vector params = new Vector();
		params.addElement("javafun"); //如果传递多个值 则 params.addElement("另一个"); 
		// 发出请求
		String result = (String) client.execute("hello.sayHello", params);
		System.out.println("服务器的返回值: " + result);
	}
}


public class ServerHandler {
	public String sayHello(String name) {
		System.out.println("收到客户端请求:" + name);
		return "Hello," + name;
	}
}
