簡單服務端和客戶端的開發

        在編程之前我安裝了一些軟件工具包,其中包括vc2005、gsoap、Flash builder 4.5等等,我利用vc2005開發服務端,FB來開發客戶端,當然我對於這些工具以及環境還不是很熟悉,也是第一次來動手寫關於這方面的,現在對於其中一些原理有了大概的瞭解了。

        我首先動手寫的是服務端,關於服務端:

        在VC裏我建了一個login的項目,在這個項目裏最初只包含三個文件,然後我自建了一個名爲ifun.h的頭文件,在這個文件裏我自己編寫了聲明瞭四個函數頭(int ns__Login(char* strUserID, char* strPwd, wchar_t*& iLoginInfo);int ns__Add_ID(char* strUserID, char* strPwd,wchar_t*& iLoginInfo);int ns__Delete_ID(char* strUserID, wchar_t*& iLoginInfo);int ns__Modify_ID(char* strUserID, char* strPwd, wchar_t*& iLoginInfo);),這四個函數就是我設計的要向客戶端提供的服務函數,分別提供四種功能,然後我利用gsoap工具通過這個文件自動生成了一些與soap服務有關的文件,當然在這些文件裏對於有些文件我做了一些修改(例如,在soapserver.h文件裏我加入了int is_wsdl();int ser_send_wsdl();這兩個函數,前一個用於判斷是否請求wsdl,後一個用於發送wsdl文件,在soapserver.cpp文件裏我借鑑着編寫了上面聲明的那四個函數的函數體),之後我又借鑑着陸續添加了一些文件到我的工程裏面,這些文件有頭文件:CharConvert.h、Database.h、MailSender.h、markup.h、publicFunction.h、stdsoap2.h以及源文件:Database.cpp、markup.cpp、publicFunction.cpp、stdsoap2.cpp。在這些文件裏Database.h和Database.cpp用於提供數據庫服務,最後我向stdafx.h文件里加入了一些宏定義、條件編譯以及設計了主文件login.cpp,文件代碼如下:

// login1.cpp : 定義控制檯應用程序的入口點。
//
#include <vector>

#include "stdafx.h"
#include "Database.h"
#include "soapService.h"
#include "ns.nsmap"
#include "Markup.h"
#include "PublicFunction.h"

#include <stdio.h>
#include <string.h>
#include <iostream>
#include "MailSender.h"
#define PER_MINUTE (60000)
using namespace std;
int http_get(struct soap * soap);
CDataBase g_DataBase;

int _tmain(int argc, _TCHAR* argv[])
{
 //連接數據庫
 if(!g_DataBase.OpenDatabase())
 {
  return 1;
 }

 Service calc;
 calc.imode |= SOAP_C_UTFSTRING;
 calc.omode |= SOAP_C_MBSTRING;
 calc.fget = http_get;


 if (!(argc < 2))
 {
  // serve as CGI application
  calc.serve();
  calc.destroy();
 }
 if (calc.run(9999))
 {
  calc.soap_stream_fault(std::cerr);
  exit(-1);
 }
 return 0;
}

//能夠遠程讀取wsdl文件
int http_get(struct soap * soap)
{
 if (0 != strcmp(soap->path, "/ns.wsdl"))
 {
  return SOAP_GET_METHOD;
 }

 //讀取硬盤wsdl文件
 CString cstrWsdlName = CPublicFunction::GetWsdlName();
 CString cstrWsdlPath = CPublicFunction::GetWsdlPath();

 CString cstrWsdl = cstrWsdlPath + cstrWsdlName;
 CMarkup xmlWsdl;
 string strWsdl;
 if(xmlWsdl.Load(cstrWsdl))
 {
  strWsdl = xmlWsdl.GetDoc();
  cout << strWsdl.c_str() <<endl;

 }


 soap->http_content = "xml";

 soap_response(soap, SOAP_FILE);


 soap_send_raw(soap, strWsdl.c_str(), strWsdl.length());  


 soap_end_send(soap);

 return SOAP_OK;
}

   之後我便進行編譯了,根據編譯報錯逐漸地向相應的文件中加入了一些文件包含,以及對於工程屬性做了一些修改,之後便通過編譯了,我的服務端便完成了。

    最後我再來寫我的客戶端,對於客戶端:

   我在Flashbuilder裏建了一個名爲webserver的

 

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