有一個數據中心監測項目,命名為CPing,它的主要原理通過WEB進行前台統一配置管理,後台定期對數據中心相關設備執行Ping操作,並將結果及時寫入到數據庫。
該項目基於Linux平台部署,前端開發語言采用PHP,後台開發語言采用C,由於考量到項目的部署簡潔性,後台開發的守護進程盡量不直接操作數據庫,而是將需要寫入的數據以HTTP的形式發送給PHP的WEB頁面,由PHP完成寫入操作。這樣的好處是後台守護進程部署時不需要配置相關數據庫接入環境。
下面給出一段後台代碼,作用是執行Ping操作,並將結果封裝成HTTP報文發送至WEB端。
<span xmlns="http://www.w3.org/1999/xhtml" >//---------------------------------------------------------
// HTTPSinge.c ./http www.ifeng.com 0/1 80 127.0.0.1 80
// 用於執行HTTP命令,並將結果通過HTTP GET方式傳至WEB數據庫
// my2005lb 2013-8-3
//
//---------------------------------------------------------
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
char* URLTOIP(char *argv)
{
struct hostent *h;
if((h=gethostbyname(argv))==NULL)
{
fprintf(stderr,"不能得到IP/n");
exit(1);
}
printf("HostName :%s/n",h->h_name);
printf("IP Address :%s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return inet_ntoa(*((struct in_addr *)h->h_addr));
}
// 執行PING 操作
int HTTPScan(char *argv,int nPort,int nType,double *pRet)
{
struct sockaddr_in serverAddr;
int clientSocket;
int nCount = 0;
char sendbuf[2000];
char recvbuf[2000];
if((clientSocket=socket(AF_INET,SOCK_STREAM,0)) < 0)
return -1;
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(nPort);
printf("%s\n",URLTOIP(argv));
serverAddr.sin_addr.s_addr=inet_addr(URLTOIP(argv));
if(connect(clientSocket,( struct sockaddr * )&serverAddr,sizeof(serverAddr)) < 0)
return -1;
//printf("%s\n",sendbuf);
sprintf(sendbuf,"HEAD http://%s/ HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",argv,argv);
send(clientSocket,sendbuf,strlen(sendbuf),0);
recv(clientSocket,recvbuf,sizeof(recvbuf),0);
printf("%s\n",recvbuf);
close(clientSocket);
return nCount;
}
// 執行向遠程數據中心上報數據
int ProcessSQLUpdate(char *strURL,char *strIP,int nPort,char *sendbuf)
{
struct sockaddr_in serverAddr;
int clientSocket;
char recvbuf[2000];
if((clientSocket=socket(AF_INET,SOCK_STREAM,0)) < 0)
return -1;
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(nPort);
serverAddr.sin_addr.s_addr=inet_addr(strIP);
if(connect(clientSocket,( struct sockaddr * )&serverAddr,sizeof(serverAddr)) < 0)
return -1;
//printf("%s\n",sendbuf);
send(clientSocket,sendbuf,strlen(sendbuf),0);
recv(clientSocket,recvbuf,sizeof(recvbuf),0);
//printf("%s\n",recvbuf);
close(clientSocket);
return 0;
}
int main(int argc,char *argv[])
{
char strIP[250];
int nIPPort = 80;
char strURL[250];
int nPort = 80;
double pRet[6];
int nType=0;
int nCount = 0;
char sendbuf[2000];
if(argc != 6) return 0;
// ./http www.sina.com.cn 1/0 80 127.0.0.1 80(1為HTTPS,0為HTTP)
// url
if(strlen(argv[1]) > 0 && strlen(argv[1]) < 200)
sprintf(strURL,"%s",argv[1]);
else
return 0;
// http https
if(strlen(argv[2]) > 0 && strlen(argv[2]) < 2)
nType = atoi(argv[2]);
else
return 0;
// scan port
if(strlen(argv[3]) > 0 && strlen(argv[3]) < 6)
nIPPort = atoi(argv[3]);
else
return 0;
// ip
if(strlen(argv[4]) > 0 && strlen(argv[4]) < 160)
sprintf(strIP,"%s",argv[4]);
else
sprintf(strIP,"127.0.0.1");
if(strlen(argv[5]) > 0 && strlen(argv[5]) <= 5)
nPort = atoi(argv[5]);
printf("%s %d %d %s %d\n",strIP,nIPPort,nType,strURL,nPort);
// process ping
nCount = HTTPScan(strURL,nIPPort,nType,pRet);
if(nCount == 2)
sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=%f HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,pRet[1],strIP);
else if(nCount == 6)
sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=%f&avg=%f HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,pRet[1],pRet[3],strIP);
else
sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=-1&lost=-1 HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,strIP);
// ProcessSQLUpdate(strURL,strIP,nPort,sendbuf);
/*
printf("Min Value: %f\n",pRet[2]);
printf("Avg Value: %f\n",pRet[3]);
printf("Max Value: %f\n",pRet[4]);
printf("MDev Value: %f\n",pRet[5]);
*/
return 0;
}</span>
C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm
讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm
讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm
讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm
將C語言梳理一下,分布在以下10個章節中: