gsoap c java,Linux C+gsoap/Win C#服务器端与Java客户端通信

Server: Linux, C + gsoap; Windows,C#  (192.168.1.2 Linux  192.168.1.3 Win)

Client: JS +  ExtJS 4  +  Javaservlet  (192.168.1.4 Windows)

Interface: Server ←→ WebService(WSDL)←→ Client servlet ←→Client ExtJS

Tools:

1.     gsoap-2.8

2.     axis2-1.6.2

一、Server Side

1.     Linux (Fedora 17  32 or 64)

(1)   set $GSOAP = /gsoap/; set$MONITOR = /project/monitor/

(2)   install gsoap-2.8 to $GSOAP

(3)   build C project in $MONITOR, coding…

(4)   copy stdsoap2.h & stdsoap2.cfrom gsoap source dir(gsoap-2.8/gsoap/) to $MONITOR

(5)   C-code in $MONITOR/monitorWS.h

//gsoapns service name: hardInfo

//gsoapns service namespace: http://monitor/hardInfo.wsdl

//gsoapns service location: http://monitor

//gsoapns service executable: hardInfo.cgi

//gsoapns service encoding: encoded

//gsoapns schema namespace: urn:hardInfo

int ns__NetInfo(char **result);

int ns__CPUInfo(char **result);

int ns__DiskInfo(char **result);

int ns__MemoryInfo(char **result);

ALERT: when return the result json strings, must use soap_malloc().

……

getDiskInfoStr(tmpdisk);

char *disk= (char*)soap_malloc(soap, strlen(tmpdisk)+1);

strncpy(disk, tmpp,strlen(tmpdisk)+1);

*result =disk;

return SOAP_OK;

……

(6)   bash: $GSOAP/bin/soapcpp2 -c -S-x $MONITOR/monitorWS.h stdsoap2.h stdsoap2.c

-c, generate c source code only

-S, generate server-side code only

-x, don't generate sample XML message files

(7)   compile C project.

monitorWS: $(objects) soapServer.c soapC.c stdsoap2.c  (Makefile)

run  monitorWS

(8)   in client browser

http ://192.168.1.2 or http://192.168.1.2:8081/?wsdl        → get the WSDL XML.

2.     Windows (Win7  32 or Server 2003  64)

(1)   VS2010,.Net C#,build ASP.NETWeb Service Application project and coding.

ALERT:when use PerformanceCounter class, must try catch exception:

try {

PerformanceCounter pcCpuLoad = newPerformanceCounter("Processor", "% Processor Time","_Total");

cpu.AverageUsage= pcCpuLoad.NextValue().ToString("N2") + "%";

}catch{}

Or, IIS will refuse this action via web service.

ALERT:web service response in json string format without xml structure:

[WebService(Namespace = "http://xxx.org/")]

[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

public class monitorWS : System.Web.Services.WebService

{

[WebMethod]

[SoapDocumentMethodAttribute(Action "http://monitor/diskJsonStr",

RequestNamespace = "http://monitor/T",

ResponseNamespace = "http://monitor/T",

Use = SoapBindingUse.Literal)]

public string diskJsonStr()

{

GetHardwareInfo getHardwareInfo = newGetHardwareInfo("disk");

return getHardwareInfo.getInfo(true).ToString();

}

…….

}

(2)   Setup IIS 7.5, publish the WebService:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

“目录浏览”   should open

0818b9ca8b590ca3270a3433284dd417.png

“高级设置”->常规”

0818b9ca8b590ca3270a3433284dd417.png

run web service.

(3)   in client browser

http ://192.168.1.3 or http ://192.168.1.3:8081/monitorWS.asmx?wsdl      → just test web service is OK.

二、Client Side

1.     create java class via WSDLfrom Linux platform server side webservice:

(1)   install java & axis2-1.6.2,set environment path: AXIS2_HOME & JAVA_HOME

(2)   copy WSDL XML to axis2-1.6.2 dir, the CMD run:

# axis2-1.6.2/wsdl2java.bat -uri .\xxx.wsdl -p monitor.client-o .

-uri, WSDL XML file

-p, java package

-o, output path

get two java class files: MonitorWSCallbackHandler.java & MonitorWSStub.java

(3)   create java servlet to call web service on Linux platform server side:

String url = “http ://192.168.1.2”;

hardStub = new HardInfoStub(url);

HardInfoStub.DiskInfo DiskInfoInfoReq = new HardInfoStub.DiskInfo();

HardInfoStub.DiskInfoResponse DiskInfoRes = new HardInfoStub.DiskInfoResponse();

if (hardStub != null){

DiskInfoRes = hardStub.diskInfo(DiskInfoInfoReq);

}

returnDiskInfoRes.getResult();

//HardInfoStub in MonitorWSStub.java

2.     create java servlet to call webservice on Windows platform server side:

//import java.net.MalformedURLException;

//import javax.xml.namespace.QName;

//import javax.xml.rpc.ServiceException;

//import org.apache.axis.client.Call;

//import org.apache.axis.client.Service;

String service_url ="http://localhost:8081/monitorWS.asmx";

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(service_url));

call.setOperationName(new QName("http://monitor/T","diskJsonStr"));

call.addParameter(new QName("http://monitor/T","name"),

org.apache.axis.encoding.XMLType.XSD_STRING,

javax.xml.rpc.ParameterMode.IN);

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

call.setUseSOAPAction(true);

call.setSOAPActionURI("http://monitor/diskJsonStr");

String res = call.invoke(new Object[] { "diskJsonStr"}).toString();

return res.toString();

cannot use the same method as calling gsoap web service.  axis2 will alert error.

3.     run servlet

servlet config:

MonitorServlet

monitor.servlet. MonitorServlet

1

MonitorServlet

/MonitorServlet

4.     extjs + js Web Page callservlet:

http://localhost:8080/MonitorServlet?type=disk&ip=192.168.1.2&port=80&os=Linux32

http://localhost:8080/MonitorServlet   →  java servlet

type=disk&ip=192.168.1.2&port=80&os=Linux32  →  server side

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值