2012-09-30 105 views
0

我是gSOAP的新手,可以在某些機構發佈一個正常工作的gSOAP多線程C++獨立服務器和客戶端。在gSOAP文檔中,多線程獨立服務器沒有關於在哪裏調用webservice函數的細節,如int ns_add(int a,int b),沒有調用主函數頭文件calc.h中定義的函數服務器類 - calc.cpp。gSOAP C++多線程獨立服務器和客戶端示例

請給出一步一步的指導,這將有助於像我這樣的新來者。

您的答覆將不勝感激。

+0

請參閱此鏈接的C++多線程服務器示例:http://stackoverflow.com/questions/8150380/gsoap-multithreading/8245349#8245349。如果你想要更好的答案,那麼你需要自己做一些工作,你嘗試過什麼代碼?爲什麼不自己開始創建這樣的說明? – Jackson

+0

嗨,我試過你發送的鏈接,但沒有提到ns_add,ns__mul是如何從void * process_request(void * calc)提供的。這就是爲什麼發佈這個問題。自己創造一個例子也是一個不錯的選擇,我會嘗試一下 –

回答

1

這是gSOAP的在C++中的一個例子:我們有這個例子功能(服務)在頭文件 :

int yb__add_list(char *name, char *tel, char **result); 
int yb__del_list(int no, char **result); 
int yb__get_tel_by_name(char *name, char **result); 
int yb__get_name_by_tel(char *tel, char **result); 
int yb__get_list(char **result); 

運行soapcpp2 interface.h 產生我們存根後,我們有一些文件檢查文件,以瞭解它:soapStub.h,soapH.h,soapC.c,soapClient.c,soapServer.c 那麼我們可以這樣實現服務:

int main(int argc, char **argv) 
{ int m, s; /* master and slave sockets */ 
struct soap soap; 

soap_init(&soap); 
load_list(); 
show_list(); 
if (argc < 2) 
soap_serve(&soap); /* serve as CGI application */ 
else 
{ m = soap_bind(&soap, NULL, atoi(argv[1]), 100); 
if (m < 0) 
{ soap_print_fault(&soap, stderr); 
    exit(-1); 
} 
fprintf(stderr, "Socket connection successful: master socket = %d\n", m); 
for (; ;) 
{ s = soap_accept(&soap); 
    fprintf(stderr, "Socket connection successful: slave socket = %d\n", s); 
    if (s < 0) 
    { soap_print_fault(&soap, stderr); 
    exit(1); 
    } 
    soap_serve(&soap); 
    soap_end(&soap); 
    } 
    } 
    return 0; 
    } 

int yb__add_list(struct soap *soap, char *name, char *tel, char **result) 
{ 
printf("the name is %s, tel is %s\n", name, tel); 
*result = (char *) soap_malloc(soap, 50); 
if(add_list(name, tel) > 0) 
strcpy(*result, "Success!"); 
else 
strcpy(*result, "Failed!"); 
show_list(); 
save_list(); 
return SOAP_OK; 
} 
int yb__del_list(struct soap *soap, int no, char **result) 
{ 
*result = (char *) soap_malloc(soap, 50); 
if(delete_list(no) > 0) 
strcpy(*result, "Success!"); 
else 
strcpy(*result, "Failed!"); 
save_list(); 
return SOAP_OK; 
} 
int yb__get_tel_by_name(struct soap *soap, char *name, char **result) 
{ 
char tel[50]; 
*result = (char *) soap_malloc(soap, 200); 
if(get_tel_by_name(name, tel) == 1) 
sprintf(*result, "The tel of %s is: \n%s\nSuccess!", name, tel); 
else 
strcpy(*result, "Failed!"); 
return SOAP_OK; 
} 
int yb__get_name_by_tel(struct soap *soap, char *tel, char **result) 
{ 
char name[50]; 

*result = (char *) soap_malloc(soap, 200); 
if(get_name_by_tel(tel, name) == 1) 
sprintf(*result, "The owner of %s is: \n%s\nSuccess!", tel, name); 
else 
strcpy(*result, "Failed!"); 
return SOAP_OK; 
} 
int yb__get_list(struct soap *soap, char **result) 
{ 
*result = (char *)malloc(1024); 
get_list(*result); 
return SOAP_OK; 
} 

可以執行這樣的服務爲沙子a

const char server[] = "localhost:8080/"; 
int executecmd(char *inbuf, struct soap *p_soap); 
int main(void) 
{ 
struct soap soap; 
char inbuf[MAX_CANON]; 
int ret; 
soap_init(&soap); 
for(; ;) { 
fputs(NEWLINE_STRING, stdout); 
fputs(CLIENT_PROMPT_STRING, stdout); 
if (fgets(inbuf, MAX_CANON, stdin) == NULL) 
    break; 
if (*(inbuf + strlen(inbuf) - 1) == NEWLINE_SYMBOL) 
    *(inbuf + strlen(inbuf) - 1) = 0; 
if(is_blank_line(inbuf)) 
    continue; 
if((ret = executecmd(inbuf, &soap)) < 0) 
    break; /* accept the quit command */ 
else if(ret == 0) 
    continue; /* encount the error command */ 

} 
return 1; 
} 

int executecmd(char *inbuf, struct soap *p_soap) 
{ 
char *result; 
int n; 
char **chargv; 

if((n = makeargv(inbuf, BLANK_STRING, &chargv)) < 0){ 
printf("makeargv error\n"); 
exit(0); 
} 
if(!strcmp(chargv[0], "add") && n == 3){ 
soap_call_yb__add_list(p_soap, server, "", chargv[1], chargv[2], &result); 
printf("%s\n", result); 
return 0; 
} 
else if(!strcmp(chargv[0], "del") && n == 2){ 
soap_call_yb__del_list(p_soap, server, "", atoi(chargv[1]), &result); 
printf("%s\n", result); 
return 0; 
} 
else if(!strcmp(chargv[0], "gettel") && n == 2){ 
soap_call_yb__get_tel_by_name(p_soap, server, "", chargv[1], &result); 
printf("%s\n", result); 
return 0; 
} 
else if(!strcmp(chargv[0], "getname") && n == 2){ 
soap_call_yb__get_name_by_tel(p_soap, server, "", chargv[1], &result); 
printf("%s\n", result); 
return 0; 
} 
else if(!strcmp(chargv[0], "list")){ 
soap_call_yb__get_list(p_soap, server, "", &result); 
printf("%s\n", result); 
return 0; 
} 
else if(!strcmp(chargv[0], QUIT_STRING)){ 
return -1; 
} 
else if(!strcmp(chargv[0], HELP_STRING)){ 
printf("The usage of the command:\n"); 
printf("\"add name tel\"  add new entry to address book.\n"); 
printf("\"del no\"   delete entry by its corresponding phone number.\n"); 
printf("\"getname tel\"  get name by the corresponding phone number.\n"); 
printf("\"gettel name\"  get phone number by name.\n"); 
printf("\"list\"    list the current entries in the address book.\n"); 
printf("\"help\"    get help.\n"); 
printf("\"quit\"    quit this client.\n"); 
return 0;  
} 
else{ 
printf("Can not execute this command.\n input \"help\" to get usage of commands."); 
return 0; 
} 
return 1; 
} 

您也可以在這樣的終端上運行的客戶端:在./server +端口 然後在客戶端終端孤立無援的服務器./client

1

你可以找到你有gSOAP的許多例子在你的系統上安裝gsoap。您將在安裝文件夾內找到一個SAMPLE文件夾,以幫助您瞭解gSoap的不同應用。