有一个数据中心监测项目,命名为CPing,它的主要原理通过WEB进行前台统一配置管理,后台定期对数据中心相关设备执行Ping操作,并将结果及时写入到数据库。 该项目基于Linux平台部署,前端开发语言采用PHP,后台开发语言采用C,由于考量到项目的部署简洁性,后台开发的守护进程尽量不直接操作数据库,而是将需要写入的数据以HTTP的形式发送给PHP的WEB页面,由PHP完成写入操作。这样的好处是后台守护进程部署时不需要配置相关数据库接入环境。 下面给出一段后台代码,作用是执行Ping操作,并将结果封装成HTTP报文发送至WEB端。 <span xmlns="http://www.w3.org/1999/xhtml" style="">//---------------------------------------------------------
// 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
",URLTOIP(argv));
serverAddr.sin_addr.s_addr=inet_addr(URLTOIP(argv));
if(connect(clientSocket,( struct sockaddr * )&serverAddr,sizeof(serverAddr)) < 0)
return -1; //printf("%s
",sendbuf);
sprintf(sendbuf,"HEAD http://%s/ HTTP/1.1
HOST: %s
CONNECTION: CLOSED
",argv,argv); send(clientSocket,sendbuf,strlen(sendbuf),0);
recv(clientSocket,recvbuf,sizeof(recvbuf),0);
printf("%s
",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
",sendbuf);
send(clientSocket,sendbuf,strlen(sendbuf),0);
recv(clientSocket,recvbuf,sizeof(recvbuf),0);
//printf("%s
",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
",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
HOST: %s
CONNECTION: CLOSED
",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
HOST: %s
CONNECTION: CLOSED
",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
HOST: %s
CONNECTION: CLOSED
",strIP,strURL,strIP);// ProcessSQLUpdate(strURL,strIP,nPort,sendbuf);
/*
printf("Min Value: %f
",pRet[2]);
printf("Avg Value: %f
",pRet[3]);
printf("Max Value: %f
",pRet[4]);
printf("MDev Value: %f
",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个章节中:- Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
- Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
- Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
- Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
- Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
- Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
- Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
- Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
- Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
- Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107902.htm