xml-rpc实践(cc)

xml-rpc规范:
http://xmlrpc.scripting.com/spec.html

下文转载自(没有亲自试):
http://blog.csdn.net/fm6060/article/details/5906627

xml-rpc实践

用了两天的时间做了一个简单的xml-rpc的测试,这个例子里server用java实现,client分别用java和c实现
1.装备java包和c的.h(include)、.so(lib)
(1)在 http://ws.apache.org/xmlrpc/ 下载java server及client最新版本的jar包(apache-xmlrpc-current-bin.zip)
(2)需到 http://jakarta.apache.org/commons/index.html 下载如下程序包: commons-codec-1.3
(通用编码 / 解码算法实现,可参考 http://www.devx.com/Java/Article/29795/1954?pf=true 或 http://jakarta.apache.org/commons/codec/userguide.html 来获得该软件包的详细信息)
commons-httpclient-3.0.1 ( HTTP 协议的客户端编程工具包,详细介绍见 http://www-128.ibm.com/developerworks/cn/opensource/os-httpclient/ )
(3)http://xmlrpc-c.sourceforge.net/下载xmlrpc-c-1.06.41.tgz,该包需要到linux下安装,安装方法如下:
A、在/tmp/用tar -zxvf xmlrpc-c-1.06.41.tgz命令解压缩后,进入到解开的目录中,
B、#./configure
C、#make
D、#make install
相关库文件会拷贝到/usr/local/lib下,头文件会拷贝到/usr/loca/include下。

(4)从curl.haxx.se下载最新版的curl库(如curl-7.21.1.tar.gz)
同上执行解压
安装的步骤也同上
B、#./configure
C、#make
D、#make install

2.建立server
(1)新建类 Calcloator
package xml.test;

public class Calcloator {

public int add(int i1, int i2) {
  System.out.println ("###########");
        return i1 + i2;
    }

    public int subtract(int i1, int i2) {
        return i1 - i2;
    }

}


(2)建立类AreaServer
package xml.test;

import org.apache.xmlrpc.common.TypeConverterFactoryImpl;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;
import java.io.IOException;

public class AreaServer {
 
public static void main(String[] args) {
   try{
    startServer();
   }catch(Exception e){
   e.printStackTrace();
     }

    }


public static void startServer() throws Exception{
  System.out.println("begin start ");
 
  WebServer server = new WebServer(8081);
  XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
  System.out.println("aaaaaa "); 
  PropertyHandlerMapping phm = new PropertyHandlerMapping();
  phm.addHandler("Calcloator",Calcloator.class);
 
  xmlRpcServer.setHandlerMapping(phm);

  XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false); 
 
 
        server.start();
}
}

调用AreaServer启动server

3、java的client
package test;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.apache.xmlrpc.client.util.ClientFactory;



public class ClientTest {

public static void main(String[] args) throws Exception {
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://localhost:8081/"));
        config.setEnabledForExtensions(true); 
        config.setConnectionTimeout(60 * 1000);
        config.setReplyTimeout(60 * 1000);

        XmlRpcClient client = new XmlRpcClient();
     
        // use Commons HttpClient as transport
        client.setTransportFactory(
            new XmlRpcCommonsTransportFactory(client));
        // set configuration
        client.setConfig(config);

        // make the a regular call
        Object[] params = new Object[]
            { new Integer(2), new Integer(3) };
        Integer result = (Integer) client.execute("Calcloator.subtract", params);
        System.out.println("2 + 3 = " + result);
     
        // make a call using dynamic proxy
      

}
}
每一次调用client都会执行server的指定方法

4、c的client
#include<iostream>
#include<fstream>
using namespace std;
#include<xmlrpc-c/base.h>
#include<xmlrpc-c/client.h>
#define NAME "Xmlrpc-c Test Client"
#define VERSION "1.0.0.0"
void die_if_fault(xmlrpc_env* const envP)
{
        if(envP->fault_occurred)
        {  
                cout<<"xml-rpc fault:"<<envP->fault_string<<" code:"<<envP->fault_code<<endl;
                exit(1);
        }  
}
int main(int argc, char* argv[])
{
       cout<< "aaaaa"<<endl;
        xmlrpc_env env;
        xmlrpc_value* resultP;
        xmlrpc_int32 sum,x;
        const char* const serverUrl = "http://192.168.177.193:8081/";
        const char* const methodName = "Calcloator.add";
        cout << "aaaaa"<<endl;
        xmlrpc_env_init(&env);
        cout << "11111"<<endl;       
        xmlrpc_client_init2(&env,XMLRPC_CLIENT_NO_FLAGS,NAME,VERSION,NULL,0);
        cout << "22222"<<endl;               
        die_if_fault(&env);
        cout << "33333"<<endl;                       
        resultP = xmlrpc_client_call(&env,serverUrl,methodName,"(ii)",(xmlrpc_int32)atoi(argv[1]),(xmlrpc_int32)atoi(argv[2]));
        cout << "bbbb" <<endl;
        die_if_fault(&env);
        cout << "ccccc" <<endl;       
        //xmlrpc_read_int(&env,resultP,&sum);
        //xmlrpc_decompose_value(&env,resultP,"(ii)",&sum);
     xmlrpc_read_int(&env, resultP, &sum);       
        cout << "ddddd" <<endl;       
               
        die_if_fault(&env);
        cout<<"sum:"<<sum<<endl;
        cout<<"x:"<<x<<endl;
       
        xmlrpc_DECREF(resultP);
        xmlrpc_env_clean(&env);
        xmlrpc_client_cleanup();
       
        return 0;
}


编译命令
g++ -g xmlrpcclienttest.c -lxmlrpc_client -lpthread  -lxmlrpc -lxmlrpc_xmlparse -lxmlrpc_xmltok -o aa.o
(这明明是个c的程序,变成c++是因为之前用gcc无论如何都编译不过去)
执行 ./aa.o 1 2
如果提提示找不到路径则
#export LD_LIBRARY_PATH=/usr/local/lib

你可能感兴趣的:(xml)