下面是转载的一篇文章,在ubuntu下试验成功。转自:http://bbs.php100.com/apps.php?q=diary&uid=56398&a=detail&did=1265
服务端实现步骤:
1. 创建socket套接字,并在某个端口打开;
2. 定义并注册服务端方法;
3. 运行服务端。
客户端实现步骤:
1. 调用服务端注册的方法;
2. 处理返回信息。
如下为一个XmlRpc的代码实现:
#######
server.h
#######
#ifndef SERVER_H_
#define SERVER_H_
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
class XmlrpcServer
{
private:
class MyTestMethod: public xmlrpc_c::method
{
public:
MyTestMethod()
{
this->_signature = "A:ii";
this->_help = "This method adds two integers together";
}
void execute(xmlrpc_c::paramList const& paramList,xmlrpc_c::value * const retval)
{
const int addend(paramList.getInt(0));
const int adder(paramList.getInt(1));
*retval = (xmlrpc_c::value_int)(addend+adder);
}
};
private:
xmlrpc_c::registry addIntRegistry;
xmlrpc_c::serverAbyss* AbyssServer;
int port;
int socket_fd;
public:
void regester_xml_method();
int setupServer();
int setupSocket();
public:
XmlrpcServer(int _port):port(_port),socket_fd(-1){};
};
#endif
#######
server.cpp
#######
#include "server.h"
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
#include <sys/signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
void XmlrpcServer::regester_xml_method()
{
xmlrpc_c::methodPtr add_int(new XmlrpcServer::MyTestMethod);
addIntRegistry.addMethod("add.int",add_int);
}
int XmlrpcServer::setupServer()
{
AbyssServer = new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt()
.registryP(&addIntRegistry).socketFd(socket_fd));
cout<< "Xmlrpc server is running......"<< endl;
AbyssServer->run();
cout<< "Xmlrpc server stopped"<< endl;
return 0;
}
int XmlrpcServer::setupSocket()
{
int rc;
int yes = 1;
struct sockaddr_in rm_addr;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == socket_fd)
{
cout<< "Can not open server socket: " << endl;
return -1;
}
rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (-1 == rc)
{
cout << "Can not set socket options: " <<endl;
close(socket_fd);
return -1;
}
rm_addr.sin_family = AF_INET;
rm_addr.sin_port = htons(port);
rm_addr.sin_addr.s_addr = INADDR_ANY;
rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
if (-1 == rc)
{
cout << "Can not bind to port " << port <<endl;
close(socket_fd);
return -1;
}
return 0;
}
##########
serverMain.cpp
##########
#include "server.h"
#include <iostream>
using namespace std;
int main(void)
{
XmlrpcServer myServer(2655);
if(myServer.setupSocket() != 0)
return -1;
myServer.regester_xml_method();
if(myServer.setupServer() != 0)
{
cout<< "cannot setup RPC Server"<< endl;
return -1;
}
return 0;
}
########
clientMain.cpp
########
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc,char **argv)
{
int a,b,int_result;
xmlrpc_c::value result;
if(argc != 3)
{
cout<< "please input Two interger numbers: "<< endl;
cin >>a >>b;
}
else
{
a = atoi((argv[1]));
b = atoi((argv[2]));
}
xmlrpc_c::clientSimple myClient;
myClient.call("http://localhost:2655/RPC2","add.int","ii",&result,a,b);
int_result = (xmlrpc_c::value_int)(result);
cout<< "sum of " <<a<<" and "<< b<< " is " << int_result<< endl;
return 0;
}
用gcc编译器编译,编译命令为:
服务端:
gcc -Wall -g server.cpp serverMain.cpp -o server.deamon -lstdc++ -lxmlrpc++ -lxmlrpc_server_abyss++ -lxmlrpc_server
客户端:
gcc -g -Wall -o add_int clientMain.cpp -lstdc++ -lxmlrpc_client++
测试:
可执行文件生成成功后,运行 " ./server.deamon "
然后执行 " ./add_int 1 2 " 得输出结果:sum of 1 and 2 is 3
执行 ./add_int 1456 456465 3
please input Two interger numbers:
4 9
sum of 4 and 9 is 13
运行成功!
服务端实现步骤:
1. 创建socket套接字,并在某个端口打开;
2. 定义并注册服务端方法;
3. 运行服务端。
客户端实现步骤:
1. 调用服务端注册的方法;
2. 处理返回信息。
如下为一个XmlRpc的代码实现:
#######
server.h
#######
#ifndef SERVER_H_
#define SERVER_H_
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
class XmlrpcServer
{
private:
class MyTestMethod: public xmlrpc_c::method
{
public:
MyTestMethod()
{
this->_signature = "A:ii";
this->_help = "This method adds two integers together";
}
void execute(xmlrpc_c::paramList const& paramList,xmlrpc_c::value * const retval)
{
const int addend(paramList.getInt(0));
const int adder(paramList.getInt(1));
*retval = (xmlrpc_c::value_int)(addend+adder);
}
};
private:
xmlrpc_c::registry addIntRegistry;
xmlrpc_c::serverAbyss* AbyssServer;
int port;
int socket_fd;
public:
void regester_xml_method();
int setupServer();
int setupSocket();
public:
XmlrpcServer(int _port):port(_port),socket_fd(-1){};
};
#endif
#######
server.cpp
#######
#include "server.h"
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
#include <sys/signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
void XmlrpcServer::regester_xml_method()
{
xmlrpc_c::methodPtr add_int(new XmlrpcServer::MyTestMethod);
addIntRegistry.addMethod("add.int",add_int);
}
int XmlrpcServer::setupServer()
{
AbyssServer = new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt()
.registryP(&addIntRegistry).socketFd(socket_fd));
cout<< "Xmlrpc server is running......"<< endl;
AbyssServer->run();
cout<< "Xmlrpc server stopped"<< endl;
return 0;
}
int XmlrpcServer::setupSocket()
{
int rc;
int yes = 1;
struct sockaddr_in rm_addr;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == socket_fd)
{
cout<< "Can not open server socket: " << endl;
return -1;
}
rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (-1 == rc)
{
cout << "Can not set socket options: " <<endl;
close(socket_fd);
return -1;
}
rm_addr.sin_family = AF_INET;
rm_addr.sin_port = htons(port);
rm_addr.sin_addr.s_addr = INADDR_ANY;
rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
if (-1 == rc)
{
cout << "Can not bind to port " << port <<endl;
close(socket_fd);
return -1;
}
return 0;
}
##########
serverMain.cpp
##########
#include "server.h"
#include <iostream>
using namespace std;
int main(void)
{
XmlrpcServer myServer(2655);
if(myServer.setupSocket() != 0)
return -1;
myServer.regester_xml_method();
if(myServer.setupServer() != 0)
{
cout<< "cannot setup RPC Server"<< endl;
return -1;
}
return 0;
}
########
clientMain.cpp
########
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc,char **argv)
{
int a,b,int_result;
xmlrpc_c::value result;
if(argc != 3)
{
cout<< "please input Two interger numbers: "<< endl;
cin >>a >>b;
}
else
{
a = atoi((argv[1]));
b = atoi((argv[2]));
}
xmlrpc_c::clientSimple myClient;
myClient.call("http://localhost:2655/RPC2","add.int","ii",&result,a,b);
int_result = (xmlrpc_c::value_int)(result);
cout<< "sum of " <<a<<" and "<< b<< " is " << int_result<< endl;
return 0;
}
用gcc编译器编译,编译命令为:
服务端:
gcc -Wall -g server.cpp serverMain.cpp -o server.deamon -lstdc++ -lxmlrpc++ -lxmlrpc_server_abyss++ -lxmlrpc_server
客户端:
gcc -g -Wall -o add_int clientMain.cpp -lstdc++ -lxmlrpc_client++
测试:
可执行文件生成成功后,运行 " ./server.deamon "
然后执行 " ./add_int 1 2 " 得输出结果:sum of 1 and 2 is 3
执行 ./add_int 1456 456465 3
please input Two interger numbers:
4 9
sum of 4 and 9 is 13
运行成功!