展开全部
1.准备开发环境:macosx/xcode3.2.1/数据库:mysql/soap/mysql++2.建soap服务端:
a.用xcode建一个应用32313133353236313431303231363533e59b9ee7ad9431333339663266(我用得是用终端应用);
b.从安装soap的路径下找到这些文件:soapcpp2/stdsoap2.cpp/stdsoap2.h把这三个文件copy,建的应用目录下;
c.在应用目录建test.h文件代码如下:
//gsoap ns service name: Test Service
//gsoap ns service style:
rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://192.168.1.100:8888
//gsoap ns service location: http://192.168.1.100:8888
//gsoap ns schema
namespace: urn:test
//gsoap test service method-documentation: logon
int
Test__logon(char* name,char* password,int
*returnResult);
复制代码
把main.app文件修改为如下:#include "TestH.h"
#include "Test.nsmap"
int main(int argc, char
**argv){
SOAP_SOCKETm, s;
/* master and slave sockets
*/
structsoapsoap;soap_init(&soap);
m = soap_bind(&soap,
NULL, 8888, 100);
if(!soap_valid_socket(m)) {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(!soap_valid_socket(s)) {
soap_print_fault(&soap, stderr);
exit(-1); }
soap_serve(&soap);
soap_end(&soap);
}
return 0;
}
int Test__logon(struct soap *soap,char* name,char*
passname,int *result){
fprintf(stderr,"coming:Test__logon\n");
int rtn;
*result = rtn;
returnSOAP_OK;
}
复制代码
d.在终端下,进入应用目录后,输入:soapcpp2 -SLwx -pTest Test.h回车,并生成如下6个文件:Test.nsmap/TestC.cpp/TestH.h/TestServer.cpp/TestStub.h/TestObject.h;把这6个文件加入项目,编译运行,出现:[Switching to process 4796]Running…Socket connection successful: master socket = 3,说明soap服务器代码可以运行了。
3.处理引入mysql++库到开发环境(我的mysql安装路径:/usr/local/mysql;我的mysql++安装路径:/usr/local/include/mysql++):
在xcode中:Edit Project Settings:
a: Search Paths -> Header Search Paths
/usr/local/include/mysql++ /usr/local/mysql/include
b: Search Paths ->Library Search Paths
/usr/local/mysql/lib c: Linking ->Other Linker Flags
-bind_at_load -lmysqlpp -lmysqlclient
4.建一个c++类,以便soap服务端方便处理接受客服端送来得数据,与mysql交互处理来处理。 GetDBData.h代码如下:
#ifndef _GetDBData_H_
#define _GetDBData_H_
class GetDBData{public:
GetDBData();
~GetDBData();
virtual int getSelectCountData(char*
name,char* password);
};
#endif // _GetDBData_H_
复制代码
GetDBData.app代码:
#include "GetDBData.h"#include
< mysql++.h>#include
#include
<
iomanip>
usingnamespacestd;
GetDBData::GetDBData(){}
GetDBData::~GetDBData(){}
int
GetDBData::getSelectCountData(char* name,char*
password){
fprintf(stderr,"coming: GetDBData::getSelectCountData\n");
int count = 0;
//构造一个Connection类的对象
mysqlpp::Connection
conn(false);
mysqlpp::Connection *con = new mysqlpp::Connection();
con->set_option(new
mysqlpp::SetCharsetNameOption("utf8"));
//创建数据库的连接:第一个是db名称,服务器地址,用户名,密码
con->connect("mysql",
"localhost", "root", "wangjc");
mysqlpp::Query query =
con->query();
query << "select count(1) as ct from TEST.logon where
name= '"<< name << "' and password = '"<< password
<<
"'"; mysqlpp::StoreQueryResult res = query.store();
// 处理查询结果
if (res) {
count = res[0]["ct"];
}else{
cerr<< "Failed
to get stock table:
"<< query.error() << endl;
delete
con;return-1;
}
//mysql_free_result(res);
//std::cout <<
"Hello, World! 查询结果:%i"
<<
count;
fprintf(stderr,"GetDBData::getSelectCountData->name:%s,password:%s,查询结果:%i\n",name,password,count);
delete
con; return count;
}
复制代码
把main.app代码修改为:
#include "TestH.h"
#include "Test.nsmap"
#include "GetDBData.h"
int
main(int argc, char **argv){SOAP_SOCKETm, s;
/* master and slave sockets
*/
structsoapsoap;soap_init(&soap);
m = soap_bind(&soap,
NULL, 8888, 100);
if(!soap_valid_socket(m))
{
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(!soap_valid_socket(s))
{
soap_print_fault(&soap, stderr);
exit(-1);
}
soap_serve(&soap);
soap_end(&soap);
}
return 0;}
int Test__logon(struct
soap *soap,char* name,char* passname,int
*result){
fprintf(stderr,"coming:Test__logon\n");
int rtn;GetDBData*data
= newGetDBData();
rtn = data->getSelectCountData(name,
passname);
delete data; *result = rtn;
returnSOAP_OK;
}
复制代码
说明:在mysql中建一个TEST库,在TEST库中建logon表(字段有name和password)
以上就完成soap+mysql得开发,接下来写客户端iphone的代码,主要是把soap客户端代码引入到iphone应用中去处理。
5.用xcode建iphone应用,在建一个文件Test.h(存根文件,代码与服务器端的Test.h代码相同,这里就不在多讲)。
6.把soapcpp2/stdsoap2.cpp/stdsoap2.h把这三个文件copy到建iphone应用目录下。
7.在终端下,进入建iphone应用目录,输入:soapcpp2 -CLwx -pTest Test.h回车,并生成如下6个文件:
Test.nsmap/TestC.cpp/TestH.h/TestClient.cpp/TestStub.h/TestProxy.h;把这6个文件加入iphone项目。
8.在xcode一个类(GetWebServicesData)来soap进行数据交互处理
GetWebServicesData.h代码:
#ifndef _getServicesData_H_
#define _getServicesData_H_
class
GetWebServicesData
{
public:
GetWebServicesData();
~GetWebServicesData();
virtual
int getData(char *name,char* password);
};
#endif //
_getServicesData_H_
复制代码
9.把GetWebServicesData类引用到oc代码中,实现数据交互处理。
主要代码:
NSString *_name = self.nameTextField.text;
NSString *_password =
self.passwordTextField.text;
char *name = (char *)[_name
UTF8String];
char *password = (char *)[_password
UTF8String];
GetWebServicesData*services =
newGetWebServicesData();
int rtn = services->getData(name,
password);
if (rtn > 0) {
self.label.text = @"登录成功!";
}else if
(rtn == 0) {
self.label.text=
@"无该用户登录信息!";
}
else{
self.label.text= @"网络问题,无法登录!";
}
delete
services;
复制代码
GetWebServicesData.app代码:
#include
"TestH.h"
#include "Test.nsmap"
#include
"GetWebServicesData.h"
constcharserver[] =
"http://192.168.1.100:8888";
//const char server[] =
"http:127.0.0.1:8888";
GetWebServicesData::GetWebServicesData(){
}
GetWebServicesData::~GetWebServicesData(){
}
int
GetWebServicesData::getData(char *name,char*
password){
printf("GetWebServicesData::getData-name:%s,password:%s\n",name,password);
structsoapsoap;
int result;
soap_init1(&soap, SOAP_XML_INDENT);
soap_call_Test__logon(&soap,server,"",name,password,&result);
if (soap.error)
{
soap_print_fault(&soap,
stderr);
return -1;
}
printf("result = %i\n",
result);
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
//system("pause");
return result;
}
复制代码
10.先运行服务端程序,再运行iphone端,哈哈,可以看到结果了
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起