當前位置:首頁 » 編程軟體 » linux編譯gsoap

linux編譯gsoap

發布時間: 2022-08-14 22:24:58

Ⅰ 跪求大神指導:linux下使用gsoap生成C++代碼訪問WebService編譯出錯

找人來幫助你解決問題,這將是幫助吧!

Ⅱ linux gsoap輸出中文是什麼編碼

[1]在gsoap中,如果遇到編碼問題,首先利用下面這個函數設置gsoap的接收的編碼格式。如下例,把gsoap編碼設置為UTF-8的格式:
soap_set_mode(p_IASServer->soap, SOAP_C_UTFSTRING);
因此,您只需要把編碼轉換為UTF-8後傳遞給gsoap即可。
[2]最近寫WebService程序時,遇到了一個很棘手的問題:通過SOAP介面傳輸中文字元時出現亂碼。
我是用C++編寫服務端,框架代碼由gSoap自動生成;SOAP介面對外開放,允許使用C++,Java和C#中任意一種語言編寫客戶端。gSoap默認的編碼方式是UTF-8,用gSoap自動生成的客戶端代碼編寫出來的程序傳輸中文字元時不會出現任何問題,但用Java或C#傳輸過來的中文全部變成了亂碼。

客戶端/服務端中文亂碼問題:
soap_init(&soap);
//set charset utf-8
soap_set_mode(&soap, SOAP_C_UTFSTRING);

Ⅲ 有用gsoap在arm-linux下實現soap協議的嗎

接下來我結合自己的實踐與理解,講講VC用gsoap下編寫webService和客戶端程序,有不對的地方還請大家指正,謝謝。 我以網上出現的實現一個簡單的加法函數為例,講講我在操作過程中遇到的問題。 一 伺服器端 1.首先編寫 add.h文件: 1//gsoap ns s...

Ⅳ Linux下使用gsoap返回字元串出錯,新手求教

應該把bufout指針指向的的字元串,轉變成string,然後再輸出

Ⅳ linux下用怎麼編譯gsoap伺服器程序

理論上是可以的,但是需要在Linux伺服器上搭建.NET平台,微軟號稱.NET平台是可移植到Linux的,但是貌似現在很少有人這么干。 後來有一個Mono項目,據說可以很好的使C#程序運行在Linux平台,具體樓主可以谷歌網路之,這方面的資料還是有不少的。

Ⅵ 怎麼使用gsoap

接下來我結合自己的實踐與理解,講講VC用gsoap下編寫webService和客戶端程序,有不對的地方還請大家指正,謝謝。
我以網上出現的實現一個簡單的加法函數為例,講講我在操作過程中遇到的問題。


伺服器端
1.首先編寫 add.h文件:

1//gsoap ns service name: add
2//gsoap ns service namespace: http://localhost/add.wsdl
3//gsoap ns service location: http://localhost
4//gsoap ns service executable: add.cgi
5//gsoap ns service encoding: encoded
6//gsoap ns schema namespace: urn:add
7
8int ns__add( int num1, int num2, int* sum );
9

2.用gsoap/bin目錄下的soapcpp2.exe程序,生成一些文件。可以把soapcpp2.exe拷貝到一add.h目錄下,用cmd執行soapcpp2.exe
add.h就可以,在這個目錄下會自動生成許多將來有用的文件,如add.namap,soapH.h,soapC.cpp,soapClient.cpp,soapServer.cpp等文件。soapcpp2.exe可以帶參數執行,具體執行soapcpp2.exe
-h查看。

3.新建一個win32控制台工程,加入wsock32.lib庫,將剛才生成的那些文件添加到工程中。然後編寫webserver.cpp主程序:

#include "add.h"
#include "add.nsmap"

int main(int argc, char* argv[])
{

int m, s; /**//* master and slave sockets */
struct soap add_soap;
soap_init(&add_soap);
//soap_set_namespaces(&add_soap, add_namespaces);

if (argc < 2)
{
printf("usage: %s <server_port> \n", argv[0]);
exit(1);
}
else
{
m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
if (m < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}

fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{
s = soap_accept(&add_soap);
if (s < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);

soap_serve(&add_soap);//該句說明該server的服務
soap_end(&add_soap);
}
}
return 0;
}
//server端的實現函數與add.h中聲明的函數相同,但是多了一個當前的soap連接的參數
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}

4.
編譯這個程序,會提示錯誤,將gsoap_win32目錄下stdsoap2.cpp,stdsoap2.h文件加入工程,重新編譯如果還有錯誤,可能是你將add.h生成的文件添加入工程出錯的原因。實際上在編寫server程序時,無須帶Client的那些文件,還有帶Lib的文件也無須添加到工程中。再重新編譯應該就沒有問題了,啟動4567埠,在ie中輸入localhost:4567,如果顯示xml頁面,說明程序已經啟動。


對應的客戶端
1。客戶端程序代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include "soapH.h"
#include "add.nsmap"

int add(const char* server, int num1, int num2, int *sum);

int main(int argc, char **argv)
{
int result = -1;
char* server="http://localhost:4567";
int num1 = 0;
int num2 = 0;
int sum = 0;
if( argc < 3 )
{
printf("usage: %s num1 num2 \n", argv[0]);
exit(0);
}

num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

result = add(server, num1, num2, &sum);
if (result != 0)
{
printf("soap err,errcode = %d\n", result);
}
else
{
printf("%d+%d=%d\n", num1, num2, sum );
}
return 0;
}

int add( const char* server, int num1, int num2, int *sum )
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
// soap_set_namespaces(&add_soap, add_namespaces);

//該函數是客戶端調用的主要函數,後面幾個參數和add.h中聲明的一樣,前面多了3個參數,函數名是介面函數名ns__add前面加上soap_call_
soap_call_ns__add( &add_soap, server, "", num1, num2, sum );
if(add_soap.error)
{
printf("soap error:%d,%s,%s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap) );
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}

2.客戶端程序既可以新建一個新的win32控制台程序,將剛才生成的nsmap,soapH.h,soapClient.h等文件加入工程,編譯既可。我是直接在原先工程中加入一客戶端代碼,將webserver.cpp文件移除,並且將soapServer.cpp等server端需要的文件移除,將soapClient.cpp等client端需要的cpp添加到工程,編譯既可。
3.啟動server程序,F5客戶端程序,經測試正常。


遇到的問題
1.server端可以編譯成CGI方式執行,而並不是綁定到某個埠,這種方式我沒有實踐。

if (argc < 2) // no args: assume this is a CGI application
{
soap_serve(&soap); // serve request, one thread, CGI style
soap_destroy(&soap); // dealloc C++ data
soap_end(&soap); // dealloc data and clean up
}
2.在編譯伺服器及客戶端程序時一開始對add.h生成的文件添加到工程,經常出現問題,需要自己不調試。特別是鏈接時段,server/client要與其生成的文件相對應,server調用生成的soapserver.cpp,client調用生成的soapclient.cpp文件。
3.多線程方式,在windows下建議用pthread_win32庫,這里給出多線程下的例子。

一 gSOAP需要的頭文件:

//gsoap ns service name: calc
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
//gsoap ns service location: http://127.0.0.1:8089/cal
//gsoap ns schema namespace: urn:calc
int ns__add(double a, double b, double *result);
int ns__sub(double a, double b, double *result);
int ns__mul(double a, double b, double *result);
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);

二 多線程伺服器關鍵代碼

#include
#include "calc.nsmap"
#include "soapH.h"

/**//////////////////////////////////////////////////////////////////////////
///宏與全局變數的定義
#define BACKLOG (100)
#define MAX_THR (10)
#define MAX_QUEUE (1000)

pthread_mutex_t queue_cs; //隊列鎖
pthread_cond_t queue_cv; //條件變數
SOAP_SOCKET queue[MAX_QUEUE]; //數組隊列
int head =0, tail =0; //隊列頭隊列尾初始化
/**///////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
void * process_queue(void *); //線程入口函數
int enqueue(SOAP_SOCKET); //入隊列函數
SOAP_SOCKET dequeue(void); //出隊列函數

/**///////////////////////////////////////////////////////////////////////////
//線程入口函數
void * process_queue(void * soap)
{
struct soap * tsoap = (struct soap *)soap;
for(;;)
{
tsoap->socket = dequeue();
if (!soap_valid_socket(tsoap->socket))
{
break;
}
soap_serve(tsoap);
soap_destroy(tsoap);
soap_end(tsoap);
}
return NULL;
}

//入隊列操作
int enqueue(SOAP_SOCKET sock)
{
int status = SOAP_OK;
int next;
pthread_mutex_lock(&queue_cs);
next = tail +1;
if (next >= MAX_QUEUE)
next = 0;
if (next == head)
status = SOAP_EOM;
else
{
queue[tail] =sock;
tail = next;
}
pthread_cond_signal(&queue_cv);
pthread_mutex_unlock(&queue_cs);
return status;
}

//出隊列操作
SOAP_SOCKET dequeue()
{
SOAP_SOCKET sock;
pthread_mutex_lock(&queue_cs);
while (head == tail )
{
pthread_cond_wait(&queue_cv,&queue_cs);
}
sock = queue[head++];
if (head >= MAX_QUEUE)
{
head =0;
}
pthread_mutex_unlock(&queue_cs);
return sock;
}

/**///////////////////////////具體服務方法////////////////////////////////////////
//加法的實現
int ns__add(struct soap *soap, double a, double b, double *result)
{
*result = a + b;
return SOAP_OK;
}
//減法的實現
int ns__sub(struct soap *soap, double a, double b, double *result)
{
*result = a - b;
return SOAP_OK;
}
//乘法的實現
int ns__mul(struct soap *soap, double a, double b, double *result)
{
*result = a * b;
return SOAP_OK;
}
//除法的實現
int ns__div(struct soap *soap, double a, double b, double *result)
{
if (b)
*result = a / b;
else
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't">http://tempuri.org/">Can't divide %f by %f", a, b);
return soap_sender_fault(soap, "Division by zero", s);
}
return SOAP_OK;
}
//乘方的實現
int ns__pow(struct soap *soap, double a, double b, double *result)
{
*result = pow(a, b);
if (soap_errno == EDOM) /**//* soap_errno 和errorno類似, 但是和widnows兼容 */
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't take the power of %f to %f", a, b);
sprintf(s, "Can't">http://tempuri.org/">Can't take power of %f to %f", a, b);
return soap_sender_fault(soap, "Power function domain error", s);
}
return SOAP_OK;
}

/**///////////////////////////////////////////////////////////////////////////////////////////////////////
//主函數
int main(int argc,char ** argv)
{
struct soap ServerSoap;
//初始話運行時環境
soap_init(&ServerSoap);
//如果沒有參數,當作CGI程序處理
if (argc <2)
{
//CGI 風格服務請求,單線程
soap_serve(&ServerSoap);
//清除序列化的類的實例
soap_destroy(&ServerSoap);
//清除序列化的數據
soap_end(&ServerSoap);
}else
{
struct soap * soap_thr[MAX_THR];
pthread_t tid[MAX_THR];
int i,port = atoi(argv[1]);
SOAP_SOCKET m,s;
//鎖和條件變數初始化
pthread_mutex_init(&queue_cs,NULL);
pthread_cond_init(&queue_cv,NULL);
//綁定服務埠
m = soap_bind(&ServerSoap,NULL,port,BACKLOG);
//循環直至服務套接字合法
while (!soap_valid_socket(m))
{
fprintf(stderr,"Bind port error! ");
m = soap_bind(&ServerSoap,NULL,port,BACKLOG);
}
fprintf(stderr,"socket connection successful %d ",m);

//生成服務線程
for(i = 0; i <MAX_THR; i++)

{
soap_thr[i] = soap_(&ServerSoap);
fprintf(stderr,"Starting thread %d ",i);
pthread_create(&tid[i],NULL,(void*(*)(void*))process_queue,(void*)soap_thr[i]);
}

for(;;)
{
//接受客戶端的連接
s = soap_accept(&ServerSoap);
if (!soap_valid_socket(s))
{
if (ServerSoap.errnum)
{
soap_print_fault(&ServerSoap,stderr);
continue;
}else
{
fprintf(stderr,"Server timed out ");
break;
}
}
//客戶端的IP地址
fprintf(stderr,"Accepted connection from IP= %d.%d.%d.%d socket = %d ",
((ServerSoap.ip)>>24)&&0xFF,((ServerSoap.ip)>>16)&0xFF,((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF,(ServerSoap.socket));
//請求的套接字進入隊列,如果隊列已滿則循環等待
while(enqueue(s) == SOAP_EOM)
Sleep(1000);
}
//服務結束後的清理工作
for(i = 0; i < MAX_THR; i++)
{
while (enqueue(SOAP_INVALID_SOCKET) == SOAP_EOM)
{
Sleep(1000);
}
}
for(i=0; i< MAX_THR; i++)
{
fprintf(stderr,"Waiting for thread %d to terminate ..",i);
pthread_join(tid[i],NULL);
fprintf(stderr,"terminated ");
soap_done(soap_thr[i]);
free(soap_thr[i]);
}
pthread_mutex_destroy(&queue_cs);
pthread_cond_destroy(&queue_cv);
}
//分離運行時的環境
soap_done(&ServerSoap);
return 0;
}

Ⅶ SOAPsnp只能在 Linux系統下使用嗎

linux下gsoap的初次使用

這兩天,接到一個項目,需要在linux程序中調用遠程的web應用,通過soap協議。開始上網查了下資料,發現了gsoap庫這個好東東^_^。繼續在網上搜索例子代碼,發現基本都不可編譯通過,於是便一邊學習一邊寫了這個最簡單的例子,希望對後來者起到一點幫助。

對gsoap的簡單介紹,請自己參閱http://gsoap2.sourceforge.net/
下載相應的包,主要有2個工具和源代碼:
wsdl2h -o outfile.h infile.wsdl 實現wsdl文件到h文件的數據映射
soapcpp2 -c outfile.h生成相應的底層通信stub,strech程序

下面這個簡單的例子實現的是在客戶端輸入2個數字,然後遠程調用服務端的加法函數,最後返回結果給客戶端。
在這里我們不需要wsdl的文件,可以直接從.h文件來生成代碼。我們定義一個函數聲明文件,用來定義介面函數,名稱為add.h,內容如下:

//gsoapopt cw
//gsoap ns2 schema namespace: urn:add
//gsoap ns2 schema form: unqualified
//gsoap ns2 service name: add
//gsoap ns2 service type: addPortType
//gsoap ns2 service port:http://websrv.cs.fsu.e/~engelen/addserver.cgi
//gsoap ns2 service namespace: urn:add
//gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http
//gsoap ns2 service method-style: add rpc
//gsoap ns2 service method-encoding:
add http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns2 service method-action: add ""
int ns2__add( int num1, int num2, int* sum );
然後我們執行soapcpp2 -c add.h,自動生成一些遠程調用需要的文件。

接下來我們寫一個服務端,創建文件addserver.c

#include "soapH.h"
#include "add.nsmap"
int main(int argc, char **argv)
{
int m, s;
struct soap add_soap;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
if (argc < 2) {
printf("usage: %s <server_port> /n", argv[0]);
exit(1);
} else {
m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
if (m < 0) {
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: master socket = %d/n", m);
for (;;) {
s = soap_accept(&add_soap);
if (s < 0) {
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d/n", s);
soap_serve(&add_soap);
soap_end(&add_soap);
}
}
return 0;
}
int ns2__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}

我們接著寫客戶端,文件addclient.c

#include "soapStub.h"
#include "add.nsmap"
int add(const char *server, int num1, int num2, int *sum)
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
printf("server is %s, num1 is %d, num2 is %d/n", server, num1, num2);
if (add_soap.error) {
printf("soap error: %d, %s, %s/n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}
最後寫一個測試代碼,addtest.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int add(const char *server, int num1, int num2, int *sum);
int main(int argc, char **argv)
{
int result = -1;
char server[128] = {0};
int num1;
int num2;
int sum;
if (argc < 4) {
printf("usage: %s <ip:port> num1 num2 /n", argv[0]);
exit(1);
}
strcpy(server,argv[1]);
num1 = atoi(argv[2]);
num2 = atoi(argv[3]);
result = add(server, num1, num2, ∑);
if (result != 0) {
printf("soap error, errcode=%d/n", result);
} else {
printf("%d + %d = %d/n", num1, num2, sum);
}
return 0;
}
到此為止,我們自己的代碼已經編寫完畢,現在我們來編譯服務端和客戶端
注意:編譯的時候我們需要gsoap包里的源代碼文件,把stdsoap2.c和stdsoap2.h文件拷貝到當前目錄

我們寫一個Makefile文件:

GSOAP_ROOT = /root/gsoap-2.7/gsoap
WSNAME = add
CC = g++ -g -DWITH_NONAMESPACES
INCLUDE = -I$(GSOAP_ROOT)
SERVER_OBJS = soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o
CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o $(WSNAME)test.o
all: server
server: $(SERVER_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS)
client: $(CLIENT_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)test $(CLIENT_OBJS)
cl:
rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test

然後我們執行make,即可生產addserver程序;make client,生成addtest程序。
讓server跑起來,執行./addserver 6666

終端列印出「Socket connection successful: master socket = 3」,那麼你的server已經在前台run起來了;
運行客戶端,./addtest ip:port num1 num2,返回加法的結果。

Ⅷ linux下安裝gsoap在make時報錯collect2: error: ld returned 1 exit status

此類問題一般是庫的問題,在你安裝應用之前請確保你的應用所需要的依賴全部安裝在電腦上,否則安裝就會提示失敗。抱歉的是,你這個軟體我沒有安裝過,不知道缺失的是什麼庫文件。

熱點內容
壓縮麵食 發布:2024-05-05 18:55:45 瀏覽:803
linux的gz解壓命令 發布:2024-05-05 18:24:13 瀏覽:311
伺服器機櫃屬於什麼輻射 發布:2024-05-05 18:02:10 瀏覽:336
存儲成本計算 發布:2024-05-05 18:02:10 瀏覽:584
如何把手機改安卓10 發布:2024-05-05 17:39:07 瀏覽:498
我的世界怎麼擴容伺服器內存 發布:2024-05-05 17:19:54 瀏覽:48
java讀取文件字元 發布:2024-05-05 17:15:18 瀏覽:11
三星怎麼應用加密 發布:2024-05-05 17:13:18 瀏覽:152
cad字體在那個文件夾 發布:2024-05-05 17:08:20 瀏覽:331
什麼時候用編譯器 發布:2024-05-05 17:08:20 瀏覽:766