PHP如何与后台C++程序通信,C++操作Mysql数据库等
-------------my.h概要
#include <winsock2.h>
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <vector>
#include <map>
#include <algorithm>
-------------
分三大块,winsock2.h只能放在mysql.h的前面,否则会发生一屏的重定义错误。
另外遇到的另外一个问题是,用<string.h>时如果用cout<<string类型变量就会报错,需用<string>
前台PHP与后台C++通信采用socket通信实现:
PHP socket通信:
if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))< 0)
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
//echo "Attempting to connect to '$address' on port '$service_port'...";
if (($result = socket_connect($socket, $address, $service_port))< 0)
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
$res = socket_write($socket, $content, strlen($content));
$data = socket_read($socket,1024);
C++后台服务器端:
int main()
{
WSADATA wsaData;
SOCKET serverSocket,newConnection;
SOCKADDR_IN serverAddr,clientAddr;
int Ret,clientAddrLen,nbytes;
//初始化Winsock Dll
if((Ret=WSAStartup(MAKEWORD(2,2),&wsaData))!=0)
{
printf("WSAStartup failed with error %d\n", Ret);
return 0;
}
//建立服务器端socket
if((serverSocket=socket(AF_INET, SOCK_STREAM, 0))==INVALID_SOCKET)
{
printf("Create socket error\n");
WSACleanup();
return 0;
}
//填充服务器端SOCKADDR_IN结构
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddr.sin_port=htons(USER_PORT);
//将服务器端socket与指定IP地址和端口绑定
if(bind(serverSocket,(SOCKADDR *)&serverAddr,sizeof(serverAddr))==SOCKET_ERROR)
{
int temp;
temp=WSAGetLastError();
printf("Bind error!\n");
closesocket(serverSocket);
WSACleanup();
return 0;
}
//指定服务器端serverSocket为监听模式
if(listen(serverSocket,SOMAXCONN)!=0)
{
printf("Listen error!\n");
closesocket(serverSocket);
WSACleanup();
return 0;
}
printf("server init success!\n");
clientAddrLen=sizeof(clientAddr);
char buffer[1024];
//不断接受客户数据
while(true)
{
memset(buffer,0,1024);
if((newConnection=accept(serverSocket,(sockaddr FAR*)&clientAddr,&clientAddrLen))==INVALID_SOCKET)
{
printf("Connect failed.Please try again!\n");
continue;
}
else
{
printf("here comes a query\n");
}
if(nbytes = recv(newConnection,buffer,1024,0)==-1)
{
printf("read error\n");
closesocket(newConnection);
continue;
}
cout<< "send_str:"<< buffer<<endl;
if(func(buffer)==true)
{
if(send(newConnection,"ok",strlen("ok"),0)==-1)
{
printf("write error!\n");
}
}
closesocket(newConnection);
}
closesocket(serverSocket);
WSACleanup();
return 0;
}
-----------
C++与Mysql通信部分代码:
MYSQL_RES *res_set,*res_set2,*res_exist;
MYSQL_ROW row,row2;
char *query = new char[150];
sprintf(query,"select * from liser_association where resource_id1 = %d and resource_id1_type ='book'",bookid);
//process begin
my_init ();
conn = mysql_init (NULL);
if (conn == NULL)
{
print_error (NULL, "mysql_init() failed (probably out of memory)");
exit (1);
}
//connect to server
if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
{
print_error (conn, "mysql_real_connect() failed");
mysql_close (conn);
exit (1);
}
//send query and process
if(mysql_query(conn,query)!=0)
print_error(conn,"error when select from liser_asssociation");
else
{
res_set=mysql_store_result(conn);//generate result set
if(res_set==NULL)
print_error(conn,"mysql_store_result() failed");
else
{
while((row=mysql_fetch_row(res_set))!=NULL)
{
//处理数据部分
}
mysql_free_result(res_set);
}
}
delete query;
// disconnect from server
mysql_close (conn);
----------------------------
这些API都很常见,但要注意编译环境的配置。
其中SOCKET通信记得加上ws2_32.lib,Mysql需要添加mysql/include和mysql/lib路径作为附加目录,一个是头文件的,一个是lib的,需要添加libmysql.lib,libmysql.dll在运行时也需要。
另外,Mysql的C API地址:http://www.yesky.com/imagesnew/software/mysql/manual_Clients.html