#include "my_headfile.h"
struct send userInfo;
struct recv userBack;
sqlite3 * db = NULL; //基础信息存放数据库
int i;
int ret;
PUMge head; //在线用户表
char sql[1024] = {0};
char *errmsg = NULL;
//保存用户
void save_user()
{
memset(sql, 0, sizeof(sql));
sprintf(sql, "insert into save_user values('%s','%s','%s','%s',%d,%d)",userInfo.account, userInfo.passward, userInfo.name, userInfo.moto, userInfo.likes, userInfo.vip);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg); //将用户基本信息插入数据库对应表中
if(ret != SQLITE_OK)
{
printf("insert fail:%d(%s)\n", ret, errmsg);
userBack.result = existing_acc; //账号已存在
printf("%s is insert error...\n",userInfo.name);
return;
}
printf("sqlite save_user insert success...\n");
memset(sql, 0, sizeof(sql));
sprintf(sql,"insert into question values('%s','%s','%s','%s')", userInfo.account, userInfo.passward, userInfo.question, userInfo.answer);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg); //将用户密保信息插入数据库对应表中
if(ret != SQLITE_OK)
{
printf("insert fail:%d(%s)\n", ret, errmsg);
return;
}
userBack.result = reg_success; //注册成功
}
//登录检查表和客户端发来的数据对比
void deal_log(int cfd)
{
char **resultp = NULL;
int nrow;
int ncolumn;
memset(sql, 0, sizeof(sql));
sprintf(sql, "select account,passward,name,moto,likes,vip from save_user where account = '%s' and passward = '%s'", userInfo.account, userInfo.passward);
ret = sqlite3_get_table(db, sql, &resultp, &nrow, &ncolumn, &errmsg); //提取表中所需变量值
if (ret != SQLITE_OK)
{
printf ("log error : %d(%s)!\n", ret, errmsg);
return;
}
printf("%s is logging...\n", resultp[8]); //按变量名 变量值 的顺序排列
if(nrow == 1)
{
PUMge temp = head->next;
while(temp != head)
{
if(strcmp(temp->name, resultp[8]) == 0) //与在线用户信息链表进行比对
{
userBack.result = logged_acc;
printf("%s logged error because him has logged...\n", userInfo.name); //用户已登录
return;
}
temp = temp->next;
}
userBack.result = log_success; //登录成功
strcpy(userBack.from_name, resultp[8]);
strcpy(userBack.moto, resultp[9]);
userBack.likes = *(resultp[10]) - 48; //ASCLL码
userBack.vip = *(resultp[11]) - 48;
printf("%s logged success...\n", resultp[8]);
PUMge p = (PUMge)malloc(sizeof(UMge)/sizeof(char)); //若登录成功 创建新节点插入在线用户链表
if(p == NULL)
{
perror("malloc");
return;
}
p->socket = cfd;
strcpy(p->name, resultp[8]);
printf("%s 's socket is %d...\n", p->name, p->socket);
p->next = head->next;
head->next = p;
}
else
{
userBack.result = error; //账号密码错误
printf("%s 's passward is error...\n", resultp[8]);
}
ret = write(cfd, &userBack, sizeof(userBack)); //将处理信息返回给客户端
if(ret == -1)
{
perror("write");
return;
}
}
//忘记密码
void deal_forget(int cfd)
{
char **resultp = NULL;
int nrow; //行
int ncolumn; //列
printf("i am select from question...\n");
memset(sql, 0, sizeof(sql));
sprintf(sql, "select passward, question, answer from question where account = '%s'", userInfo.account); //查找对应账号成员密保信息
ret = sqlite3_get_table(db, sql, &resultp, &nrow, &ncolumn, &errmsg);
if(ret != SQLITE_OK)
{
printf("select error fail:%d(%s)\n", ret, errmsg);
}
strcpy(userBack.passward, resultp[3]);
strcpy(userBack.question, resultp[4]);
strcpy(userBack.answer, resultp[5]);
write(cfd, &userBack, sizeof(userBack)); //将找到的信息回馈给客户端
}
//处理私聊请求
int deal_pchar(int cfd)
{
int flag = 0;
PUMge temp = head->next;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd) //在在线用户链表中寻找发送对象的信息
{
flag = 1;
strcpy(userBack.msg, userInfo.msg);
strcpy(userBack.from_name, userInfo.name);
userBack.result = private_chat;
printf("%s(%d) send %s(%d) ...\n", userInfo.name, cfd, userInfo.toname, temp->socket);
write(temp->socket, &userBack, sizeof(userBack));
break;
}
temp = temp->next;
}
if(flag) //flag标志位返回 发送成功或发送失败
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));
printf("%s send a message to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));
printf("%s send error...\n", userInfo.name);
}
}
//处理群聊请求
int deal_groupchat(int cfd)
{
int flag = 0;
PUMge temp = head->next;
while(temp != head)
{
if(temp->socket != cfd) //向所有在线用户发送信息
{
flag = 1;
strcpy(userBack.from_name, userInfo.name);
strcpy(userBack.msg, userInfo.msg);
userBack.result = group_chat;
write(temp->socket, &userBack, sizeof(userBack));
}
temp = temp->next;
}
printf("%s send a msg to everyone...\n",userInfo.name);
if(flag) //flag标志位返回 发送成功或发送失败
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));
printf("%s send grep error...\n", userInfo.name);
}
}
//处理查看当前人数
int deal_member(int cfd)
{
userBack.online_num = 0;
int i = 0;
PUMge temp = head->next;
while(temp != head) //遍历在线用户链表
{
userBack.online_num++;
strcpy(userBack.num[i], temp->name);
i++;
temp = temp->next;
}
userBack.result = online_member;
write(cfd,&userBack,sizeof(userBack));
printf("show online_member success...\n");
}
//发送表情
int deal_expression(int cfd)
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd) //在在线用户链表中寻找发送对象的信息
{
flag = 1;
userBack.result = expression;
strcpy(userBack.from_name, userInfo.name);
userBack.e_s = userInfo.e_s;
write(temp->socket, &userBack, sizeof(userBack));
break;
}
temp = temp->next;
}
if(flag) //flag标志位返回 发送成功或发送失败
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));
printf("%s send a expression to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));
printf("%s send expression error...\n", userInfo.name);
}
}
//处理点赞请求
int deal_like(int cfd)
{
char **resultp = NULL;
int nrow;
int ncolumn;
memset(sql, 0, sizeof(sql));
sprintf(sql, "select likes from save_user where name = '%s'", userInfo.toname); //找出数据库中相关信息likes
ret = sqlite3_get_table(db, sql, &resultp, &nrow, &ncolumn, &errmsg);
if(ret != SQLITE_OK)
{
printf("select fail:%d(%s)\n", ret, errmsg);
return -1;
}
if(nrow == 1)
{
memset(sql, 0, sizeof(sql));
sprintf(sql, "update save_user set likes = %d where name = '%s'",*(resultp[1]) - 47, userInfo.toname);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg); //收到客户端的点赞请求 将数据库内likes信息加一
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}
PUMge temp = head->next;
while(temp != head) //将点赞信息保存到对应用户基本信息中
{
if(strcmp(temp->name, userInfo.toname) == 0)
{
userBack.likes = *(resultp[1]) - 47;
userBack.result = like;
strcpy(userBack.from_name,userInfo.name);
write(temp->socket, &userBack, sizeof(userBack));
break;
}
temp = temp->next;
}
userBack.result = like_success;
write(cfd, &userBack, sizeof(userBack));
}
else //返回点赞成功或失败
{
userBack.result = Send_error;
write(cfd, &userBack, sizeof(userBack));
}
}
//处理更改个签
int deal_motochange(int cfd)
{
memset(sql, 0, sizeof(sql));
sprintf(sql, "update save_user set moto = '%s' where name = '%s'",userInfo.moto,userInfo.name);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}
strcpy(userBack.moto, userInfo.moto);
userBack.result = change_success;
write(cfd, &userBack,sizeof(userBack));
printf("%s change the moto...\n", userInfo.name);
return 0;
}
//处理注册会员
int deal_vip(int cfd)
{
memset(sql, 0, sizeof(sql));
sprintf(sql, "update save_user set vip = %d where name = '%s';", userInfo.vip, userInfo.name);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg); //更改用户在数据库内的VIP信息
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}
userBack.vip = 1;
userBack.result = vip_success;
write(cfd, &userBack,sizeof(userBack)); //返回信息给客户端
printf("%s become vip...\n", userInfo.name);
return 0;
}
//处理禁言
int deal_Shutup(int cfd) //vip操作
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0)
{
flag = 1;
userBack.result = Shutup;
strcpy(userBack.from_name, userInfo.name);
write(temp->socket, &userBack, sizeof(userBack));
}
temp = temp->next;
}
if(flag) //flag标志位返回 禁言成功或失败
{
userBack.result = Shutup_success;
write(cfd, &userBack, sizeof(userBack));
}
else
{
userBack.result = Send_error;
write(cfd, &userBack, sizeof(userBack));
}
}
//处理退出请求
int deal_exit(int cfd)
{
PUMge temp = head;
while(temp->next != head) //将该用户从在线用户链表中删除
{
if(strcmp(temp->next->name, userInfo.name) == 0)
{
PUMge p = temp->next;
temp->next = p->next;
free(p);
printf("%s logged out...\n", userInfo.name);
break;
}
temp = temp->next;
}
return 0;
}
void *server_requst(int const cfd)
{
printf("i am dealing...\n");
while(1)
{
ret = read(cfd, &userInfo, sizeof(userInfo));
if(ret == 0)
{
pthread_exit(NULL);
}
switch (userInfo.cmd)
{
case (reg) : //注册
{
save_user(db);
write(cfd, &userBack,sizeof(userBack));
break;
}
case (log): //登录
{
deal_log(cfd);
break;
}
case (forget): //忘记密码
{
deal_forget(cfd);
break;
}
case (private_chat): //私聊
{
deal_pchar(cfd);
break;
}
case (group_chat): //群聊
{
deal_groupchat(cfd);
break;
}
case (online_member): //查看在线人数
{
deal_member(cfd);
break;
}
case (expression): //发送表情
{
deal_expression(cfd);
break;
}
case (like): //点赞
{
deal_like(cfd);
break;
}
case (motto_change): //更改个性签名
{
deal_motochange(cfd);
break;
}
case (Vip): //注册会员
{
deal_vip(cfd);
break;
}
case (Shutup): //禁言
{
deal_Shutup(cfd);
break;
}
case (exit): //退出
{
deal_exit(cfd);
break;
}
}
}
}
int main()
{
int sfd;
head = (PUMge) malloc (sizeof(UMge)/sizeof(char)); //分配在线用户信息链表的头结点空间
if(head == NULL)
{
printf("malloc error\n");
}
head->next = head;
sfd = socket(AF_INET, SOCK_STREAM, 0); //创建套接字
if(sfd == -1)
{
perror("socket");
return -1;
}
struct sockaddr_in serveraddr;
memset(&serveraddr,0,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(PORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));
if(ret == -1)
{
perror("bind");
return -1;
}
ret = listen(sfd,10); //监听它,并设置允许最大的连接数为10个
if(ret == -1)
{
perror("listen");
close(sfd);
return -1;
}
ret = sqlite3_open("user.db",&db);
if(ret != SQLITE_OK)
{
perror("open user.db error");
return -1;
}
printf("open user.db success...\n");
ret = sqlite3_exec(db, "create table if not exists save_user(account TEXT, passward TEXT, name TEXT, moto TEXT, likes INTEGER, vip INTEGER, primary key(account))",NULL,NULL,&errmsg);
if(ret != SQLITE_OK) //在数据库创建用户基本信息表
{
printf("create fail:%d(%s)\n", ret, errmsg);
return -1;
}
printf("create save_user success...\n");
ret = sqlite3_exec(db, "create table if not exists question(account TEXT, passward TEXT, question TEXT, answer TEXT, primary key(account))", NULL, NULL, &errmsg);
if(ret !=SQLITE_OK) //在数据库创建用户密保信息表
{
printf("creat fail:%d(%s)\n",ret,errmsg);
return -1;
}
pthread_t clientid;
while(1)
{
int cfd, len;
struct sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(struct sockaddr));
len = sizeof(struct sockaddr);
//sfd接受客户端的连接,并创建新的socket为cfd,将请求连接的客户端的ip、port保存在结构体clientaddr中
cfd = accept(sfd, (struct sockaddr*)&clientaddr, &len);
if(cfd == -1)
{
perror("accept");
close(sfd);
return -1;
}
printf("%s %d success connet...\n",
inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
ret = pthread_create(&clientid, NULL, (void*)server_requst, (void*)cfd); //创建线程更快处理客户请求
if(ret == -1)
{
perror("pthread create");
return -1;
}
pthread_detach(clientid); // 线程分离
pthread_join(clientid, NULL);
}
sqlite3_close(db);
return 0;
}