磕磕绊绊还是差不多完成了,tcp多客户端在线词典
代码:
数据库导入:有点粗糙,不知道怎么搞成两列,一个单词中间还是空格卧槽难搞
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//词典数据库初始化
sqlite3 *ppDb;
if(sqlite3_open("../ele_directory.db",&ppDb)!=0){
printf("__%d__:sqlite3_open error\n",__LINE__);
return -1;
}
char sql[128]="create table if not exists directory (word char);";
char *errmsg="";
int sql_res=sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
if(sql_res!=0){
printf("__%d__:sqlite3_exec :%s\n",__LINE__,errmsg);
return -1;
}
//导入数据
FILE* fp=fopen("../dict.txt","r");
if(NULL==fp){
ERR_MSG("fopen");
return -1;
}
while(1){
char line[64]="";
bzero(line,sizeof(line));
char* fs_res=fgets(line,sizeof(line),fp);
if(NULL==fs_res){
puts("文件读取完毕");
break;
}
bzero(sql,sizeof(sql));
sprintf(sql,"insert into directory values(\"%s\");",line);
sql_res=sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
printf("%s\n",sql);
if(sql_res!=0){
printf("__%d__:sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
}
puts("数据导入完成");
if(sqlite3_close(ppDb)!=0){
printf("__%d__:sqlite3_close error\n",__LINE__);
return -1;
}
return 0;
}
server.c
/*
* function: 在线词典服务器TCP多线程
* @param [ in]
* @param [out]
* @return
*/
#include "head.h"
int main(int argc, const char *argv[])
{
//TCP服务器创建
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
//允许端口快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&addr,sizeof(addr))<0){
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("监听状态\n");
struct sockaddr_in cli_addr;
socklen_t addrlen=sizeof(cli_addr);
pthread_t pth;
while(1){
printf("客户端:%s:%d:连接前\n",\
inet_ntoa(cli_addr.sin_addr),\
ntohs(cli_addr.sin_addr.s_addr));
int cfd=accept(sfd,(struct sockaddr*)&cli_addr,&addrlen);
if(cfd<0){
ERR_MSG("accept");
return -1;
}
printf("客户端:%s:%d:连接成功\n",\
inet_ntoa(cli_addr.sin_addr),\
ntohs(cli_addr.sin_port));
//调用线程
cli_info client;
client.cfd=cfd;
client.cli_addr=cli_addr;
if(pthread_create(&pth,NULL,fun,(void*)&client)<0){
ERR_MSG("pthread_create");
return -1;
}
printf("[%d]线程创建成功\n",cfd);
pthread_detach(pth);//分离线程交由系统回收
}
close(sfd);
return 0;
}
fun.c
#include "head.h"
void * fun(void*arg){
int cfd=((cli_info*)arg)->cfd;
struct sockaddr_in cli_addr=((cli_info*)arg)->cli_addr;
char buf[128]="";
char user[16]="";
//打开数据库
sqlite3 *ppDb;
if(sqlite3_open("../ele_directory.db",&ppDb)!=0){
printf("__%d__:sqlite3_open error\n",__LINE__);
return NULL;
}
//第一层
//登录判断,(注册判断)1代表登录,2代表注册
socklen_t addrlen=sizeof(cli_addr);
int recv_res=recv(cfd,&buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return NULL;
}
if(recv_res==0){
printf("客户端:%s:%d:断开连接\n",\
inet_ntoa(cli_addr.sin_addr),\
ntohs(cli_addr.sin_port));
return NULL;
}
printf("11111=%s\n",buf);
if(buf[0]=='1'){
//登录判断
printf("login...\n");
int login_res;
while(login_res=isLogin(ppDb,cfd,&user)){
printf("login_res=%d\n",login_res);
char*l_res=NULL;
if(1==login_res)
l_res="1";
else if(2==login_res)
l_res="2";
else if(3==login_res)
l_res="3";
else
return NULL;
printf("l_res=%s\n",l_res);
int send_res=send(cfd,l_res,sizeof(recv_res),0);
if(send<0){
ERR_MSG("send");
return NULL;
}
}
int send_res=send(cfd,"0",1,0);
if(send<0){
ERR_MSG("send");
return NULL;
}
printf("%s登陆成功\n",user);
}else if(buf[0]=='2'){
//注册判断
printf("register...\n");
int register_res;
while(register_res=isRegister(ppDb,cfd,&user)){
printf("register_res=%d",register_res);
if(register_res!=1)
return NULL;
}
printf("%s注册成功\n",user);
return NULL;
}else{
printf("__%d__unknown error\n",__LINE__);
return NULL;
}
//第二层
//单词查询或者记录查询或退出
while(1){
char sql[128]="";
char* errmsg=NULL;
bzero(buf,sizeof(buf));
recv_res=recv(cfd,&buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return NULL;
}
if(recv_res==0){
printf("客户端:%s:%d:断开连接\n",\
inet_ntoa(cli_addr.sin_addr),\
ntohs(cli_addr.sin_port));
//账号状态设为未登录
bzero(sql,sizeof(sql));
sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
return NULL;
}
}
if(buf[0]=='3'){
//单词查询
printf("word finding...\n");
findWord(ppDb,cfd,user);
}else if(buf[0]=='4'){
//记录查询
printf("record finding...\n");
findRecord(ppDb,cfd,user);
}else if(buf[0]=='5'){
printf("客户端:%s:退出登录\n",user);
//账号状态设为未登录
bzero(sql,sizeof(sql));
sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
return NULL;
}
return NULL;
}else{
printf("__%d__unknown error\n",__LINE__);
return NULL;
}
}
//关闭数据库
if(sqlite3_close(ppDb)!=0){
printf("__%d__:sqlite3_close error\n",__LINE__);
return NULL;
}
}
int isLogin(sqlite3 *ppDb,int cfd,char (*user)[]){
char account[10]="";
char password[10]="";
char buf[128]="";
//数据库查询,账号是否存在,若存在密码是否正确
int recv_res=recv(cfd,&buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("客户端:%s:断开连接\n",*user);
return -1;
}
printf("buf=%s\n",buf);
sscanf(buf,"%s %s",account,password);
char sql[128]="";
char** pres = NULL;
int row, column;
char* errmsg = NULL;
sprintf(sql,"select * from account where account=\"%s\";",account);
if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("sql=%s\n",sql);
int exist_res=0;//是否存在
for (int i=0; i<(row+1)*column; i++)
{
printf("%s\t", pres[i]);
if((i+1)%column == 0)
putchar('\n');
exist_res=1;//存在为1
}
printf("exist_res=%d\n",exist_res);
if(0==exist_res){
printf("账号不存在\n");
return 1;//账号不存在返回1
}else{
printf("账号存在,判断密码\n");
bzero(sql,sizeof(sql));
sprintf(sql,"select status from account where account=\"%s\" and password=\"%s\";",account,password);
if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("sql=%s\n",sql);
int login_res=0;//是否存在账号密码匹配
for (int i=column; i<(row+1)*column; i++)
{
printf("%s\t", pres[i]);
if((i+1)%column == 0)
putchar('\n');
login_res=1;//存在为1
}
if(0==login_res){
printf("密码错误\n");
return 2;//账号密码错误返回2
}else if(0==strcmp(pres[column],"0")){
printf("账号重复登录\n");
return 3;
}
//账号状态设为已登录
bzero(sql,sizeof(sql));
sprintf(sql,"update account set status=\"0\" where account=\"%s\";",account);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("密码正确\n");
strcpy(*user,account);
return 0;//登陆成功返回0
}
}
int isRegister(sqlite3 *ppDb,int cfd,char(*user)[]){
char account[10]="";
char password[10]="";
char buf[128]="";
//数据库查询,账号是否存在
bzero(buf,sizeof(buf));
int recv_res=recv(cfd,&buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("客户端:%s:断开连接\n",*user);
return -1;
}
printf("buf=%s\n",buf);
sscanf(buf,"%s %s",account,password);
char sql[128]="";
char** pres = NULL;
int row, column;
char* errmsg = NULL;
sprintf(sql,"select * from account where account=\"%s\";",account);
if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("sql=%s\n",sql);
int reg_res=0;//是否存在
for (int i=0; i<(row+1)*column; i++)
{
printf("%s\t", pres[i]);
if((i+1)%column == 0)
putchar('\n');
reg_res=1;//存在为1
}
printf("reg_res=%d\n",reg_res);
if(0==reg_res){
//不存在
printf("账号不存在,开始注册:\n");
bzero(sql,sizeof(sql));
sprintf(sql,"insert into account values (\"%s\",\"%s\",\"1\");",account,password);
printf("sql=%s\n",sql);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("注册完成\n");
strcpy(*user,account);
char *r_res="0";//注册成功
printf("r_res=%s\n",r_res);
int send_res=send(cfd,r_res,sizeof(recv_res),0);
if(send<0){
ERR_MSG("send");
return -1;
}
return 0;
}else{
printf("账号已存在\n");
char *r_res="1";
printf("r_res=%s\n",r_res);
int send_res=send(cfd,r_res,sizeof(recv_res),0);
if(send<0){
ERR_MSG("send");
return -1;
}
return 1;
}
}
int findWord(sqlite3 *ppDb,int cfd,char*user){
printf("user=%s\n",user);
char buf[128]="";
char word[32]="";
char sql[128]="";
char*errmsg=NULL;
int recv_res=recv(cfd,&word,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}
if(recv_res==0){
printf("客户端:%s:断开连接\n",user);
//账号状态设为未登录
sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
return -1;
}
}
printf("word=%s\n",word);
char** pres = NULL;
int row, column;
bzero(sql,sizeof(sql));
sprintf(sql,"select * from directory where word like \"%s%\";",word);
if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("sql=%s\n",sql);
for (int i=column; i<(row+1)*column; i++)
{
bzero(buf,sizeof(buf));
printf("%s\t", pres[i]);
strcpy(buf,pres[i]);
strcat(buf," ");
if((i+1)%column == 0)
strcat(buf,"\n");
printf("buf=%s\n",buf);
int send_res=send(cfd,buf,sizeof(buf),0);
if(send<0){
ERR_MSG("send");
return -1;
}
if((i+1)%column == 0)
putchar('\n');
}
int send_res=send(cfd,"find over",strlen("find over"),0);
if(send<0){
ERR_MSG("send");
return -1;
}
printf("查找完毕\n");
//记录(word,time,user)
bzero(sql,sizeof(sql));
time_t t=time(NULL);
struct tm *timeinfo=localtime(&t);
char r_time[64]="";
sprintf(r_time,"%d-%d-%d:%d:%d:%d",\
timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,\
timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
printf("word:%s;time:%s;user:%s\n",word,r_time,user);
printf("sql=%s\n",sql);
sprintf(sql,"insert into record values(\"%s\",\"%s\",\"%s\");",word,r_time,user);
printf("sql=%s\n",sql);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("记录完成\n");
return 0;
}
int findRecord(sqlite3 *ppDb,int cfd,char*user){
printf("user=%s\n",user);
char sql[128]="";
char** pres = NULL;
int row, column;
char* errmsg = NULL;
char buf[128]="";
sprintf(sql,"select * from record where user=\"%s\";",user);
if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("sql=%s\n",sql);
for (int i=column; i<(row+1)*column; i++)
{
bzero(buf,sizeof(buf));
printf("%s;%ld\t", pres[i],strlen(pres[i]));
strcpy(buf,pres[i]);
strcat(buf," ");
if((i+1)%column == 0)
strcat(buf,"\n");
int send_res=send(cfd,buf,sizeof(buf),0);
if(send<0){
ERR_MSG("send");
return -1;
}
if((i+1)%column == 0)
putchar('\n');
}
printf("查找完毕\n");
int send_res=send(cfd,"find record over",strlen("find record over"),0);
if(send<0){
ERR_MSG("send");
return -1;
}
printf("记录查找完毕\n");
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.1.6"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct clientInfo{
int cfd;
struct sockaddr_in cli_addr;
}cli_info;
void * fun(void*arg);
int isLogin(sqlite3 *ppDb,int cfd,char(*user)[]);
int isRegister(sqlite3 *ppDb,int cfd,char(*user)[]);
int findWord(sqlite3 *ppDb,int cfd,char*user);
int findRecord(sqlite3 *ppDb,int cfd,char*user);
#endif
client.c
#include
#include
#include
#include
#include
#include
#include
//自定义报错提示
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.1.6"
/*
* function: TCP客户端
* @param [ in]
* @param [out]
* @return
*/
int login(int cfd);
int registe(int cfd);
int findWord(int cfd);
int findRecord(int cfd);
int main(int argc, const char *argv[])
{
//1.创建socket套接字,
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
puts("socket create");
//允许端口快速复用
int reuse = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//2.连接服务端connect
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(SER_PORT);
addr.sin_addr.s_addr=inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&addr,sizeof(addr))<0){
ERR_MSG("connect");
return -1;
}
puts("connect success");
printf("-------------------------------------------------\n");
printf("-------Wecome to eletronic directory system------\n");
printf("-------------------------------------------------\n");
printf("--------------1.登录-----------------------------\n");
printf("--------------2.注册-----------------------------\n");
printf("--------------3.退出-----------------------------\n");
printf("-------------------------------------------------\n");
printf("-------------------------------------------------\n");
printf("-------------------------------------------------\n");
char choice=0;
while(1){
printf("请选择1.登录;2注册;3.退出>>>");
choice=getchar();
while(getchar()!='\n');
switch(choice){
case '1':
//登录
if(5==login(cfd))
return 0;
break;
case '2':
//注册
if(0==registe(cfd))
return 0;
case '3':
//退出
return 0;
default :
printf("错误输入,请重输\n");
break;
}
}
close(cfd);
return 0;
}
int login(int cfd){
char buf[32]="";
char status=0;
int send_res=send(cfd,"1",1,0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
while(1){
printf("请输入账号 密码 (空格分开)>>>");
fgets(buf,sizeof(buf),stdin);
int send_res=send(cfd,buf,sizeof(buf),0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
int recv_res=recv(cfd,&status,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("socket peer has shutdown\n");
return -2;
}
printf("status=%c\n",status);
if('1'==status){
printf("账号不存在\n");
}else if('2'==status){
printf("密码错误\n");
}else if('3'==status){
printf("账号已登录\n");
}else{
printf("登陆成功\n");
break;}
}
printf("-----------------------------------\n");
printf("-------请输入你的操作--------------\n");
printf("---------3.查找单词----------------\n");
printf("---------4.记录查询----------------\n");
printf("---------5.退出--------------------\n");
printf("-----------------------------------\n");
char choice=0;
while(1){
printf("请选择>>>3.查找单词4.记录查询5.退出");
choice=getchar();
while(getchar()!='\n');
switch(choice){
case '3':
//查找单词
findWord(cfd);
break;
case '4':
//查询记录
findRecord(cfd);
break;
case '5':
//退出
send_res=send(cfd,"5",1,0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
return 5;
default :
printf("错误输入,请重输\n");
break;
}
}
return 0;
}
int registe(int cfd){
char buf[32]="";
char status=0;
int send_res=send(cfd,"2",1,0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
while(1){
bzero(buf,sizeof(buf));
printf("请输入账号 密码 (空格分开)>>>");
fgets(buf,sizeof(buf),stdin);
printf("%s",buf);
int send_res=send(cfd,buf,sizeof(buf),0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
int recv_res=recv(cfd,&status,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("socket peer has shutdown\n");
return -2;
}
printf("status=%c\n",status);
if(status=='0'){
printf("注册成功,请重新进入系统\n");
return 0;
}else if(status=='1'){
printf("账号已存在\n");
}
}
}
int findWord(int cfd){
char buf[128]="";
int send_res=send(cfd,"3",1,0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
printf("请输入你要查询的单词(支持模糊查询)>>>");
fgets(buf,sizeof(buf),stdin);
printf("%s",buf);
buf[strlen(buf)-1] = '\0';
send_res=send(cfd,buf,sizeof(buf),0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
while(1){
bzero(buf,sizeof(buf));
int recv_res=recv(cfd,buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("socket peer has shutdown\n");
return -2;
}
if(0==strcmp(buf,"find over"))
break;
printf("%s\n",buf);
}
putchar(10);
printf("查找单词完毕\n");
return 0;
}
int findRecord(int cfd){
char buf[128]="";
int send_res=send(cfd,"4",1,0);
if(send_res<0){
ERR_MSG("send");
return -1;
}
while(1){
bzero(buf,sizeof(buf));
int recv_res=recv(cfd,buf,sizeof(buf),0);
if(recv_res<0){
ERR_MSG("recv");
return -1;
}else if(recv_res==0){
printf("socket peer has shutdown\n");
return -2;
}
if(0==strcmp(buf,"find record over"))
break;
printf("%s",buf);
}
putchar(10);
printf("查找记录完毕\n");
return 0;
}
又是依托屎山代码,好乱,不想去整理了