728x90
반응형
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/include/mysql++/mysql++.h>
#include </usr/include/mysql/mysql.h>
#define HOST "localhost"
#define USER "root"
#define PASS "tjrrb12"
#define DATABASE "agent"
#define BUFFERSIZE 1000
#define PORT 5500
using namespace std;
using namespace mysqlpp;
int main()
{
int serverSocket=socket(PF_INET, SOCK_STREAM, 0 );
struct sockaddr_in serverAddress;
memset(&serverAddress , 0, sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
bind(serverSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress) );
listen(serverSocket,5);
fd_set readSet;
fd_set tempSet;
FD_ZERO(&tempSet);
FD_ZERO(&readSet);
FD_SET(serverSocket, &tempSet);
int fdmax;
fdmax=serverSocket;
time_t t = time(0);
char cur_t[10];
char *p;
vector<string> tokens;
mysqlpp::Connection con(DATABASE,HOST,USER,PASS);
while(1){
time(&t);
readSet = tempSet;
if(select(fdmax+1, &readSet, NULL, NULL, NULL) == -1){
printf("select function error\n\n");
return 0;
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/include/mysql++/mysql++.h>
#include </usr/include/mysql/mysql.h>
#define HOST "localhost"
#define USER "root"
#define PASS "tjrrb12"
#define DATABASE "agent"
#define BUFFERSIZE 1000
#define PORT 5500
using namespace std;
using namespace mysqlpp;
int main()
{
int serverSocket=socket(PF_INET, SOCK_STREAM, 0 );
struct sockaddr_in serverAddress;
memset(&serverAddress , 0, sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
bind(serverSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress) );
listen(serverSocket,5);
fd_set readSet;
fd_set tempSet;
FD_ZERO(&tempSet);
FD_ZERO(&readSet);
FD_SET(serverSocket, &tempSet);
int fdmax;
fdmax=serverSocket;
time_t t = time(0);
char cur_t[10];
char *p;
vector<string> tokens;
mysqlpp::Connection con(DATABASE,HOST,USER,PASS);
while(1){
time(&t);
readSet = tempSet;
if(select(fdmax+1, &readSet, NULL, NULL, NULL) == -1){
printf("select function error\n\n");
return 0;
}
for (int fd=0;fd<fdmax+1;fd++) {
if(FD_ISSET(fd,&readSet) != 0) {
if(fd == serverSocket) {
int clientSocket = accept(serverSocket,NULL,NULL);
FD_SET(clientSocket,&tempSet);
fdmax = clientSocket;
}
else {
char fromClient[BUFFERSIZE];
int strlen = read(fd,fromClient,BUFFERSIZE);
if(strlen > 0){
sprintf(cur_t,"%ld",t);
p = strtok(fromClient,"\n");
while(p != NULL){
tokens.push_back(p);
p = strtok(NULL,"\n");
}
if(tokens.size() == 8)
{
/*string pro_q = "INSERT INTO server_property(uuid,ip,os,cpu_model,cpu_core,mem_total_phy,disk_total_phy,regdate,license_key) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens[3]+"','"+tokens[4]+"','"+
tokens[5]+"','"+tokens[6]+"','"+cur_t+"','"+tokens[7]+"')";
cout << pro_q << endl;
mysqlpp::Query query = con.query();
query << pro_q.c_str();
mysqlpp::StoreQueryResult res = query.store();*/
//string chk_q = "SHOW TABLES LIKE '"+tokens[7]+"'";
try
{
string chk_q = "select * from "+tokens[7]+"";
cout << chk_q << endl;
Query query = con.query();
query << chk_q.c_str();
StoreQueryResult res = query.store();
}
catch(BadQuery er)
{
string create_q = "CREATE TABLE `"+tokens[7]+"`( `id` int(255) NOT NULL AUTO_INCREMENT, `uuid` varchar(255) DEFAULT NULL, `cpu_load` varchar(255) DEFAULT NULL, `mem_used` varchar(255) DEFAULT NULL, `mem_avail`
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
Query query = con.query();
query << create_q.c_str();
StoreQueryResult res = query.store();
}
//catch(!BadQuery er)
//{
string pro_q = "INSERT INTO server_property(uuid,ip,os,cpu_model,cpu_core,mem_total_phy,disk_total_phy,regdate,license_key) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens[3]+"','"+tokens[4]+"','"+t
okens[5]+"','"+tokens[6]+"','"+cur_t+"','"+tokens[7]+"')";
cout << pro_q << endl;
Query query = con.query();
query << pro_q.c_str();
StoreQueryResult res = query.store();
//tokens.clear();
//}
/* else
{
for (int fd=0;fd<fdmax+1;fd++) {
if(FD_ISSET(fd,&readSet) != 0) {
if(fd == serverSocket) {
int clientSocket = accept(serverSocket,NULL,NULL);
FD_SET(clientSocket,&tempSet);
fdmax = clientSocket;
}
else {
char fromClient[BUFFERSIZE];
int strlen = read(fd,fromClient,BUFFERSIZE);
if(strlen > 0){
sprintf(cur_t,"%ld",t);
p = strtok(fromClient,"\n");
while(p != NULL){
tokens.push_back(p);
p = strtok(NULL,"\n");
}
if(tokens.size() == 8)
{
/*string pro_q = "INSERT INTO server_property(uuid,ip,os,cpu_model,cpu_core,mem_total_phy,disk_total_phy,regdate,license_key) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens[3]+"','"+tokens[4]+"','"+
tokens[5]+"','"+tokens[6]+"','"+cur_t+"','"+tokens[7]+"')";
cout << pro_q << endl;
mysqlpp::Query query = con.query();
query << pro_q.c_str();
mysqlpp::StoreQueryResult res = query.store();*/
//string chk_q = "SHOW TABLES LIKE '"+tokens[7]+"'";
try
{
string chk_q = "select * from "+tokens[7]+"";
cout << chk_q << endl;
Query query = con.query();
query << chk_q.c_str();
StoreQueryResult res = query.store();
}
catch(BadQuery er)
{
string create_q = "CREATE TABLE `"+tokens[7]+"`( `id` int(255) NOT NULL AUTO_INCREMENT, `uuid` varchar(255) DEFAULT NULL, `cpu_load` varchar(255) DEFAULT NULL, `mem_used` varchar(255) DEFAULT NULL, `mem_avail`
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
Query query = con.query();
query << create_q.c_str();
StoreQueryResult res = query.store();
}
//catch(!BadQuery er)
//{
string pro_q = "INSERT INTO server_property(uuid,ip,os,cpu_model,cpu_core,mem_total_phy,disk_total_phy,regdate,license_key) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens[3]+"','"+tokens[4]+"','"+t
okens[5]+"','"+tokens[6]+"','"+cur_t+"','"+tokens[7]+"')";
cout << pro_q << endl;
Query query = con.query();
query << pro_q.c_str();
StoreQueryResult res = query.store();
//tokens.clear();
//}
/* else
{
string create_q = "CREATE TABLE `"+tokens[7]+"`( `id` int(255) NOT NULL AUTO_INCREMENT, `uuid` varchar(255) DEFAULT NULL, `cpu_load` varchar(255) DEFAULT NULL, `mem_used` varchar(255) DEFAULT NULL, `mem_avail`
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
Query query = con.query();
query << create_q.c_str();
StoreQueryResult res = query.store();
}*/
/*string create_q = "CREATE TABLE `"+tokens[7]+"`( `id` int(255) NOT NULL AUTO_INCREMENT, `uuid` varchar(255) DEFAULT NULL, `cpu_load` varchar(255) DEFAULT NULL, `mem_used` varchar(255) DEFAULT NULL, `mem_avail`
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
mysqlpp::Query query = con.query();
query << create_q.c_str();
mysqlpp::StoreQueryResult res = query.store();*/
tokens.clear();
}
else if(tokens.size() == 11)
{
string info_q = "INSERT INTO "+tokens[10]+"(uuid,cpu_load,mem_used,mem_avail,disk_total_logic,disk_used_logic,disk_avail_logic,rx,tx,hostname,regdate) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens
[3]+"','"+tokens[4]+"','"+tokens[5]+"','"+tokens[6]+"','"+tokens[7]+"','"+tokens[8]+"','"+tokens[9]+"','"+cur_t+"')";
cout << info_q << endl;
Query query = con.query();
query << info_q.c_str();
StoreQueryResult res = query.store();
tokens.clear();
}
else
{
tokens.clear();
}
}
//printf("fromClient : %s\n",fromClient);
else if(strlen == 0) {
FD_CLR(fd,&tempSet);
close(fd);
}
else {
write(fd,fromClient,strlen);
//continue;
}
}
}
}
}
}
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
Query query = con.query();
query << create_q.c_str();
StoreQueryResult res = query.store();
}*/
/*string create_q = "CREATE TABLE `"+tokens[7]+"`( `id` int(255) NOT NULL AUTO_INCREMENT, `uuid` varchar(255) DEFAULT NULL, `cpu_load` varchar(255) DEFAULT NULL, `mem_used` varchar(255) DEFAULT NULL, `mem_avail`
varchar(255) DEFAULT NULL, `disk_total_logic` varchar(255) DEFAULT NULL, `disk_used_logic` varchar(255) DEFAULT NULL, `disk_avail_logic` varchar(255) DEFAULT NULL, `rx` varchar(255) DEFAULT NULL, `tx` varchar(255) DEFAULT NULL, `h
ostname` varchar(255) DEFAULT NULL, `regdate` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
cout << create_q << endl;
mysqlpp::Query query = con.query();
query << create_q.c_str();
mysqlpp::StoreQueryResult res = query.store();*/
tokens.clear();
}
else if(tokens.size() == 11)
{
string info_q = "INSERT INTO "+tokens[10]+"(uuid,cpu_load,mem_used,mem_avail,disk_total_logic,disk_used_logic,disk_avail_logic,rx,tx,hostname,regdate) VALUES ( '"+tokens[0]+"','"+tokens[1]+"','"+tokens[2]+"','"+tokens
[3]+"','"+tokens[4]+"','"+tokens[5]+"','"+tokens[6]+"','"+tokens[7]+"','"+tokens[8]+"','"+tokens[9]+"','"+cur_t+"')";
cout << info_q << endl;
Query query = con.query();
query << info_q.c_str();
StoreQueryResult res = query.store();
tokens.clear();
}
else
{
tokens.clear();
}
}
//printf("fromClient : %s\n",fromClient);
else if(strlen == 0) {
FD_CLR(fd,&tempSet);
close(fd);
}
else {
write(fd,fromClient,strlen);
//continue;
}
}
}
}
}
}
반응형
'엔지니어 > C++' 카테고리의 다른 글
socket_test_server (0) | 2016.05.26 |
---|---|
socket_test_client (0) | 2016.05.26 |
agent server makefile (create by sk) (0) | 2016.05.26 |
agent client makefile (create by sk) (0) | 2016.05.26 |
j2-agent(/etc/init.d/) (create by sk) (0) | 2016.05.26 |