第三方公司提供根据axis来生成wsdl文件,如何使用C++语言开发服务端和客户端程序?需要使用gsoap工具。gsoap下载地址如下:
https://sourceforge.net/projects/gsoap2/
下载工具包gsoap_2.8.54.zip解压后在目录\gsoap-2.8\gsoap\bin\win32下可以看到工具soapcpp2.exe和wsdl2h.exe。
wsdl2h.exe 的使用方法如下:
wsdl2h -o 头文件名 WSDL文件名或URL
wsdl2h常用选项
· -o 文件名,指定输出头文件
· -n 名空间前缀 代替默认的ns
· -c 产生纯C代码,否则是C++代码
· -s 不要使用STL代码
· -t 文件名,指定type map文件,默认为typemap.dat
· -e 禁止为enum成员加上名空间前缀
soapcpp2.exe 的使用方法如下:
常用选项
· -C 仅生成客户端代码
· -S 仅生成服务器端代码
· -L 不要产生soapClientLib.c和soapServerLib.c文件
· -c 产生纯C代码,否则是C++代码(与头文件有关)
· -I 指定import路径(见上文)
· -x 不要产生XML示例文件
· -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
1、使用wsdl2h生成头文件,运行命令如下:
C:\work\gsoap-2.8\gsoap\bin\win32>wsdl2h -o UtcsService.h UtcsService.wsdl
2、 使用soapcpp2生成客户端代码,命令如下:
soapcpp2.exe UtcsService.h
报错如下:
需要使用-I指定import路径,命令如下:
soapcpp2.exe UtcsService.h -IC:\work\gsoap-2.8\gsoap\import -C
其中生成的xml文件可以删除,最后得到的文件如下:
添加-L选项运行命令如下:
soapcpp2.exe UtcsService.h -IC:\work\gsoap-2.8\gsoap\import -C -L
最后得到的文件如下:
3、 编写客户端
建立一个win32 console project,将生成的文件soapC.cpp、soapClient.cpp、soapH.h、soapStub.h、UtcsServiceSoap11Binding.nsmap等5个文件添加到工程中。
另外需要在工程中添加stdsoap2.h,stdsoap2.cpp ,该文件在C:\work\gsoap-2.8\gsoap目录下。
由于stdsoap2.cpp ,soapC.cpp 不需要预编译。
需要修改工程预编译选项。
实现main函数
需要包含如下头文件:
Main函数实现如下:
int _tmain(int argc, _TCHAR* argv[])
{
struct soap clientSOAP;
soap_init(&clientSOAP);
char* soap_endpoint = "http://33.90.174.52:5439/axis2/services/UtcsService.UtcsServiceHttpSoap11Endpoint/";
_ns1__GetCrossParam in;
_ns1__GetCrossParamResponse out;
string strCrossID = "310313";
ns2__CrossParam param;
param.crossID = &strCrossID;
in.tblInCrossParam.push_back(¶m);
if(soap_call___ns1__GetCrossParam(&clientSOAP, soap_endpoint, NULL, &in, out) == SOAP_OK)
{
printf("success\n");
}
else
{
printf("Error\n");
}
soap_destroy(&clientSOAP);
soap_end(&clientSOAP);
soap_done(&clientSOAP);
return 0;
}
编译完成即生成对应该wsdl的客户端。
4、 编写服务端
使用一下命令生成服务端相关文件:
soapcpp2.exe UtcsService.h -IC:\work\gsoap-2.8\gsoap\import -S -L
建立一个win32 console project,将生成的文件soapC.cpp、soapServer.cpp、soapH.h、soapStub.h、UtcsServiceSoap11Binding.nsmap等5个文件添加到工程中。
另外需要在工程中添加stdsoap2.h,stdsoap2.cpp ,该文件在C:\work\gsoap-2.8\gsoap目录下。
由于stdsoap2.cpp ,soapC.cpp 不需要预编译。
需要修改工程预编译选项,同客户端。
实现main函数
需要包含如下头文件:
Main函数实现如下:
int _tmain(int argc, _TCHAR* argv[])
{
int m, s;
struct soap server_soap;
soap_init(&server_soap);
//soap_set_namespaces(&add_soap, add_namespaces);
//if (argc < 2)
//{
// printf("usage: %s \n", argv[0]);
// exit(1);
//}
//else
{
//m = soap_bind(&server_soap, NULL, atoi(argv[1]), 100);
m = soap_bind(&server_soap, NULL, 5439, 100);
if (m < 0)
{
soap_print_fault(&server_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{
s = soap_accept(&server_soap);
if (s < 0)
{
soap_print_fault(&server_soap, stderr);
exit(-1);
}
//fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
soap_serve(&server_soap);//该句说明该server的服务
soap_end(&server_soap);
}
}
return 0;
}
服务函数接口如下:
int __cdecl __ns1__PushCrossState(struct soap *soap,class _ns1__PushCrossState *,class _ns1__PushCrossStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossTrafficData(struct soap *soap,class _ns1__PushCrossTrafficData *,class _ns1__PushCrossTrafficDataResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossPlan(struct soap *soap,class _ns1__PushCrossPlan *,class _ns1__PushCrossPlanResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__SetTimeOut(struct soap *soap,class _ns1__SetTimeOut *,class _ns1__SetTimeOutResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossStage(struct soap *soap,class _ns1__PushCrossStage *,class _ns1__PushCrossStageResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRegionParam(struct soap *soap,class _ns1__PushRegionParam *,class _ns1__PushRegionParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSysInfo(struct soap *soap,class _ns1__PushSysInfo *,class _ns1__PushSysInfoResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushPhaseParam(struct soap *soap,class _ns1__PushPhaseParam *,class _ns1__PushPhaseParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossParam(struct soap *soap,class _ns1__PushCrossParam *,class _ns1__PushCrossParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSignalControler(struct soap *soap,class _ns1__PushSignalControler *,class _ns1__PushSignalControlerResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSubRegionParam(struct soap *soap,class _ns1__PushSubRegionParam *,class _ns1__PushSubRegionParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushStageParam(struct soap *soap,class _ns1__PushStageParam *,class _ns1__PushStageParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossCycle(struct soap *soap,class _ns1__PushCrossCycle *,class _ns1__PushCrossCycleResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__GetTimeServer(struct soap *soap,class _ns1__GetTimeServer *,class _ns1__GetTimeServerResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossControlMode(struct soap *soap,class _ns1__PushCrossControlMode *,class _ns1__PushCrossControlModeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__HeartBeat(struct soap *soap)
{
return SOAP_OK;
}
int __cdecl __ns1__PushPlanParam(struct soap *soap,class _ns1__PushPlanParam *,class _ns1__PushPlanParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Login(struct soap *soap,class _ns1__Login *,class _ns1__LoginResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushDetParam(struct soap *soap,class _ns1__PushDetParam *,class _ns1__PushDetParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRegionState(struct soap *soap,class _ns1__PushRegionState *,class _ns1__PushRegionStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushLampGroup(struct soap *soap,class _ns1__PushLampGroup *,class _ns1__PushLampGroupResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Notify(struct soap *soap,class _ns1__Notify *,class _ns1__NotifyResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Logout(struct soap *soap,class _ns1__Logout *,class _ns1__LogoutResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushLaneParam(struct soap *soap,class _ns1__PushLaneParam *,class _ns1__PushLaneParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSysState(struct soap *soap,class _ns1__PushSysState *,class _ns1__PushSysStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Subscribe(struct soap *soap,class _ns1__Subscribe *,class _ns1__SubscribeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__UnSubscribe(struct soap *soap,class _ns1__UnSubscribe *,class _ns1__UnSubscribeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSignalControlerError(struct soap *soap,class _ns1__PushSignalControlerError *,class _ns1__PushSignalControlerErrorResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossPhaseLampStatus(struct soap *soap,class _ns1__PushCrossPhaseLampStatus *,class _ns1__PushCrossPhaseLampStatusResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRTInfoEx(struct soap *soap,class _ns1__PushRTInfoEx *ns1__PushRTInfoEx,class _ns1__PushRTInfoExResponse &)
{
for(int i=0; i<ns1__PushRTInfoEx->tblRTInfoEx.size();i++)
{
printf("ACSID:%s, time:%s\n",ns1__PushRTInfoEx->tblRTInfoEx.at(i)->dwACSID->c_str(),(ns1__PushRTInfoEx->tblRTInfoEx.at(i)->achDataTime).at(0).c_str());
}
return SOAP_OK;
}
int __cdecl __ns1__pushCrossStageEx(struct soap *soap,class _ns1__pushCrossStageEx *,class _ns1__pushCrossStageExResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushEdgeSpeedInfoEx(struct soap *soap,class _ns1__PushEdgeSpeedInfoEx *,class _ns1__PushEdgeSpeedInfoExResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossState_(struct soap *soap,class _ns1__PushCrossState *,class _ns1__PushCrossStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossTrafficData_(struct soap *soap,class _ns1__PushCrossTrafficData *,class _ns1__PushCrossTrafficDataResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossPlan_(struct soap *soap,class _ns1__PushCrossPlan *,class _ns1__PushCrossPlanResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__SetTimeOut_(struct soap *soap,class _ns1__SetTimeOut *,class _ns1__SetTimeOutResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossStage_(struct soap *soap,class _ns1__PushCrossStage *,class _ns1__PushCrossStageResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRegionParam_(struct soap *soap,class _ns1__PushRegionParam *,class _ns1__PushRegionParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSysInfo_(struct soap *soap,class _ns1__PushSysInfo *,class _ns1__PushSysInfoResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushPhaseParam_(struct soap *soap,class _ns1__PushPhaseParam *,class _ns1__PushPhaseParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossParam_(struct soap *soap,class _ns1__PushCrossParam *,class _ns1__PushCrossParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSignalControler_(struct soap *soap,class _ns1__PushSignalControler *,class _ns1__PushSignalControlerResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSubRegionParam_(struct soap *soap,class _ns1__PushSubRegionParam *,class _ns1__PushSubRegionParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushStageParam_(struct soap *soap,class _ns1__PushStageParam *,class _ns1__PushStageParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossCycle_(struct soap *soap,class _ns1__PushCrossCycle *,class _ns1__PushCrossCycleResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__GetTimeServer_(struct soap *soap,class _ns1__GetTimeServer *,class _ns1__GetTimeServerResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossControlMode_(struct soap *soap,class _ns1__PushCrossControlMode *,class _ns1__PushCrossControlModeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__HeartBeat_(struct soap *soap)
{
return SOAP_OK;
}
int __cdecl __ns1__PushPlanParam_(struct soap *soap,class _ns1__PushPlanParam *,class _ns1__PushPlanParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Login_(struct soap *soap,class _ns1__Login *,class _ns1__LoginResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushDetParam_(struct soap *soap,class _ns1__PushDetParam *,class _ns1__PushDetParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRegionState_(struct soap *soap,class _ns1__PushRegionState *,class _ns1__PushRegionStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushLampGroup_(struct soap *soap,class _ns1__PushLampGroup *,class _ns1__PushLampGroupResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Notify_(struct soap *soap,class _ns1__Notify *,class _ns1__NotifyResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Logout_(struct soap *soap,class _ns1__Logout *,class _ns1__LogoutResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushLaneParam_(struct soap *soap,class _ns1__PushLaneParam *,class _ns1__PushLaneParamResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSysState_(struct soap *soap,class _ns1__PushSysState *,class _ns1__PushSysStateResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__Subscribe_(struct soap *soap,class _ns1__Subscribe *,class _ns1__SubscribeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__UnSubscribe_(struct soap *soap,class _ns1__UnSubscribe *,class _ns1__UnSubscribeResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushSignalControlerError_(struct soap *soap,class _ns1__PushSignalControlerError *,class _ns1__PushSignalControlerErrorResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushCrossPhaseLampStatus_(struct soap *soap,class _ns1__PushCrossPhaseLampStatus *,class _ns1__PushCrossPhaseLampStatusResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushRTInfoEx_(struct soap *soap,class _ns1__PushRTInfoEx *,class _ns1__PushRTInfoExResponse &)
{
return SOAP_OK;
}
int __cdecl __ns1__PushEdgeSpeedInfoEx_(struct soap *soap,class _ns1__PushEdgeSpeedInfoEx *,class _ns1__PushEdgeSpeedInfoExResponse &)
{
return SOAP_OK;
}
其中__ns1__PushRTInfoEx已经实现。