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:

 

“目錄瀏覽”   should open

“高級設置”->常規”

  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 WSDL from 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:

<servlet-name>MonitorServlet</servlet-name>
<servlet-class>monitor.servlet. MonitorServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
<servlet-name>MonitorServlet</servlet-name>
<url-pattern>/MonitorServlet</url-pattern>
</servlet-mapping>

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章