#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <mysql/mysql.h>
#define SERV_PORT 1987
#define BACKLOG 10
#define BUF_SIZE 1024
struct record{
char ip[20];
char date[15];
char time[12];
char domain[50];
};
struct times{
char time_begin[12];
char time_end[12];
};
int CreateTcpServer()
{
int SockFd = -1;
int sockopt = 1;
int ssize = sizeof(struct sockaddr_in);
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(SERV_PORT); //htons(SERV_PORT);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
SockFd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(SockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sockopt, sizeof(sockopt)); //接收超时
setsockopt(SockFd, SOL_SOCKET, SO_SNDTIMEO, (char *)&sockopt, sizeof(sockopt)); //发送超时
setsockopt(SockFd, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt)); //允许重用本地地址
if (bind(SockFd, (struct sockaddr *)&saddr, ssize) == -1)
{
fprintf(stderr, "bind error!\n");
close(SockFd);
return -1;
}
if (listen(SockFd, BACKLOG) == -1)
{
fprintf(stderr, "listen error!\n");
close(SockFd);
return -1;
}
return SockFd;
}
int main()
{
int sockfd,clientfd;//服务器套接字和客户端套接字
int len;
int ret;
char buff[BUF_SIZE];
char rebuff[BUF_SIZE];
char ip[20],date[15],time[12],domain[50];
struct sockaddr_in youaddr;
struct record sockrecord;
struct times socktime;
int t, r;
MYSQL *conn_ptr;
MYSQL_RES *res;
MYSQL_ROW row;
char sql[100];
conn_ptr = mysql_init(NULL);
if (!conn_ptr)
{
fprintf(stderr, "mysql_init failed\n");
return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "abc123", "record", 0, NULL, 0);
if (!conn_ptr)
{
printf("Connection failed\n");
exit(-1);
}
len=sizeof(struct sockaddr);
sockfd=CreateTcpServer();
if(sockfd<0)
{
close(sockfd);
exit(-1);
}
clientfd=accept(sockfd,(struct sockaddr*)&youaddr,&len);
if(clientfd==-1)
{
perror("accept fail ...\n");
close(sockfd);
exit(1);
}
memset(rebuff,0,sizeof(rebuff));
if((ret=recv(clientfd,rebuff,sizeof(rebuff),0))==-1)
{
perror("receive fail ...\n");
close(sockfd);
}
memset(&socktime,0,sizeof(socktime));
memcpy(&socktime,rebuff,sizeof(socktime));
sprintf(sql,"select * from user where time>'%s' and time<'%s'",socktime.time_begin,socktime.time_end);
t = mysql_real_query(conn_ptr,sql,(unsigned int)strlen(sql));
if(t)
{
printf("Error making query: %s\n", mysql_error(conn_ptr));
}
else
{
printf("Query made...\n");
}
// 获取语句执行的返回结果。
res=mysql_use_result(conn_ptr);
while( (row = mysql_fetch_row(res))!=NULL)
{
if(row < 0)
break;
memset(&sockrecord,0,sizeof(struct record));
memcpy(sockrecord.ip,row[0],strlen(row[0]));
memcpy(sockrecord.date,row[1],strlen(row[1]));
memcpy(sockrecord.time,row[2],strlen(row[2]));
memcpy(sockrecord.domain,row[3],strlen(row[3]));
memset(buff,0,sizeof(buff));
memcpy(buff,&sockrecord,sizeof(sockrecord));
printf("%s | %s | %s | %s\n",sockrecord.ip,sockrecord.date,
sockrecord.time,sockrecord.domain);
if((ret=send(clientfd,buff,sizeof(buff),0))==-1)
{
perror("send data fail ...\n");
close(sockfd);
exit(-1);
}
else
usleep(1000);
}
mysql_close(conn_ptr);
close(clientfd);
close(sockfd);
return 0;
}