程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> soap通信(gsoap做server ,php做client)

soap通信(gsoap做server ,php做client)

編輯:關於C語言

網上流傳的gsoap與php的通信沒有能成功的,

為實際業務自己動手調通一個,如果其它方式望告之

php client

<?php
try{
    $soap = new SoapClient("http://127.0.0.1:8888/add?wsdl",array('location'=>"http://127.0.0.1:8888",'uri'=>"urn:add",'connection_timeout'=>3,));
    var_dump($soap);
    $p=array('num1'=>6,'num2'=>2, 'sum'=>$sum);
    $result1 = $soap->add($p);
    var_dump($result1);
    echo $result1->sum;
                                   
                                    
}catch(SoapFault $e){
    echo $e->getMessage();
}catch(Exception $e){
    echo $e->getMessage();
}


gsoap server

#include "soapH.h"
#include <windows.h>
#include "add.nsmap"
#define BACKLOG (100)    /* Max. request backlog */
DWORD WINAPI process_request(LPVOID*);
int http_get(struct soap * soap);
int main(int argc, char **argv) {
    struct soap soap;
    struct soap *tsoap;
    const char* fmt = "accepts socket %d connection from IP %d.%d.%d.%d\n";
    soap_init(&soap);
        
    if ( argc < 2 ) {
        soap_serve(&soap);
        soap_destroy(&soap);
        soap_end(&soap);
    }
    else {
        soap.send_timeout = 60;
        soap.recv_timeout = 60;
        soap.accept_timeout = 3600;
        soap.max_keep_alive = 100;
        soap.fget = http_get;
        DWORD tid;
        HANDLE hThread;
            
        int port = atoi(argv[1]); // first command-line arg is port
        SOAP_SOCKET m, s;
        m = soap_bind(&soap, NULL, port, BACKLOG);
        if ( !soap_valid_socket(m) ) exit(1);
            
        printf("Socket connection successful %d\n", m);
        for ( ; ; ) {
            s = soap_accept(&soap);
            if ( !soap_valid_socket(s) ) {
                if ( soap.errnum ) {
                    soap_print_fault(&soap, stderr);
                    exit(1);
                }
                printf("server timed out\n");
                break;
            }
                
            printf(fmt, s, (soap.ip>>24)&0xFF, (soap.ip>>16)&0xFF, (soap.ip>>8)&0xFF, soap.ip&0xFF);
            tsoap = soap_copy(&soap); // make a safe copy
            if ( !tsoap ) break;
            hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)process_request, tsoap, 0, &tid);
            if ( hThread == NULL ) {
                printf("can not create a thread for SOAP request processing.\n");
                exit(-1);
            }
        }
    }
        
    soap_done(&soap);
    return 0;
}
DWORD WINAPI process_request(LPVOID* soap) {
    soap_serve((struct soap*)soap);
    soap_destroy((struct soap*)soap);
    soap_end((struct soap*)soap);
    soap_done((struct soap*)soap);
    free(soap);
    return 0;
}
// 服務消息的實現代碼
int ns__add(struct soap *add_soap, int num1, int num2, int *sum) {
    *sum = num1 + num2;
    return SOAP_OK;
}
int http_get(struct soap * soap) {
    FILE *fd = NULL;
    printf("call http_get.\n");
    char *s = strchr(soap->path, '?');
    printf("call http_get2.\n");
    if ( !s || strcmp(s, "?wsdl") ) return SOAP_GET_METHOD;
    printf("call http_get3.\n");
    fd = fopen("./add.wsdl", "rb");
    //fd = fopen("F://soap_server//soap_server//soap_server//add.wsdl", "rb");
    if ( !fd ) return 404;
    printf("call http_get4.\n");
    soap->http_content = "text/xml";
    soap_response(soap, SOAP_FILE);
    printf("call http_ge5.\n");
    for ( ; ; ) {
        size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
        if ( !r ) break;
        if ( soap_send_raw(soap, soap->tmpbuf, r) ) break;
    }
    fclose(fd);
    soap_end_send(soap);
    return SOAP_OK;
}


本文出自 “djshell” 博客,轉載請與作者聯系!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved