????Linux???????????
???????????? ???????[ 2015/2/4 17:58:55 ] ????????Linux ??????
?????????????????????????????????д??????滮????????????????????????????????????????μ??????????????????????????????????????????????????????????????????strcpy??strlen?????????????????????飬??????о?????е???ù??????????
????????????????????????????????????????????????????????????????????????£????????????????????????????????????????????????????????????????e???????????????????ν???????????????????????????????????????????????????????????????﹦???????????о???????????????????????????????????
????????????????
????????????????????????????滮?????????????μ???????????????????????????????????????????????????????????????????????????е??????????????????У???????????????????????????С????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????
?????????????????????????????????????????????????????????????????????????????????????????д????ο?????????????
????Makefile
????????????????ò??????????????????о???????????У????????????????????????????????γ??Makefile????????
????main=insert.o chatroom.o sql.o passwd.o string.o cmd.o socket.o
????chatroom:$(main)
????@gcc$(main)-o chatroom-lsqlite3
????passwd.o:passwd.c
????@gcc passwd.c-c
????insert.o:insert.c
????@gcc insert.c-c
????sql.o:sql.c
????@gcc sql.c-c
????string.o:string.c
????@gcc string.c-c
????cmd.o:cmd.c
????@gcc cmd.c-c
????socket.o:socket.c
????@gcc socket.c-c
????.PHONY:clean cleanall
????clean:
????@rm*.o
????cleanall:
????@rm*.o chatroom
????????????????????????????????????????Щ?????
????passwd.c???????
?????????????????????????
????#include"data.h"
????void reg_db(sqlite3*db??char**errmsg??char*name??char*passwd)//??????
????{
????char sql[1024];
????int rc;
????sprintf(sql??"insert into user(name??passwd)values('%s'??'%s')"??name??passwd);
????rc=sqlite3_exec(db??sql??NULL??NULL??errmsg);
????is_sqlite(rc);
????}
????void log_db(sqlite3*db??char**errmsg??char*name??int sockfd)//???????
????{
????char sql[1024];
????int rc;
????sprintf(sql??"insert into online(name??socket??flag)values('%s'??%d??1)"??name??sockfd);
????rc=sqlite3_exec(db??sql??NULL??NULL??errmsg);
????is_sqlite(rc);
????}
????void delete_online_db(sqlite3*db??char**errmsg??int sockfd)//??????
????{
????char sql[1024];
????int rc;
????sprintf(sql??"delete from online where socket=%d"??sockfd);
????rc=sqlite3_exec(db??sql??NULL??NULL??errmsg);
????is_sqlite(rc);
????}
????int read_user(sqlite3*db??char**errmsg??char*user)//???????????
????{
????int rc;
????int i;
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from user"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????if(userflag==0)
????{
????return USERIN;
????}
????rc=sqlite3_step(stmt);
????}
????return USEROUT;
????}
????int read_id(sqlite3*db??char**errmsg??char*user)//???????????
????{
????int rc;
????int i;
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from user"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????if(userflag==0)
????{
????return atoi(sqlite3_column_text(stmt??0));
????}
????rc=sqlite3_step(stmt);
????}
????return USEROUT;
????}
????void read_id_name(sqlite3*db??char**errmsg??vpChat temp)//???????????
????{
????int rc;
????int i;
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from user"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_atoi(sqlite3_column_text(stmt??0));
????if(userflag==temp->flag)
????{
????my_strcpy(temp->name??sqlite3_column_text(stmt??1));
????}
????rc=sqlite3_step(stmt);
????}
????}
????int read_pass(sqlite3*db??char**errmsg??char*user??char*passwd)//??????????
????{
????int rc;
????int i;
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from user"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????int passwdflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????passwdflag=my_strcmp(passwd??sqlite3_column_text(stmt??2));
????if(userflag==0&&passwdflag==0)
????{
????return PASSWDOK;
????}
????rc=sqlite3_step(stmt);
????}
????return PASSWDNO;
????}
????int read_online_ok(sqlite3*db??char**errmsg??char*user)//??????????????
????{
????int rc;
????int i;
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from online"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????if(userflag==0)
????{
????return ONLINEIN;
????}
????rc=sqlite3_step(stmt);
????}
????return ONLINEOUT;
????}
???????????????????????????????
?????????????????passwd.h???
????void reg_db(sqlite3*db??char**errmsg??char*name??char*passwd);
????int read_user(sqlite3*db??char**errmsg??char*user);
????int read_pass(sqlite3*db??char**errmsg??char*user??char*passwd);//??????????
????int read_online_ok(sqlite3*db??char**errmsg??char*user);
????void delete_online_db(sqlite3*db??char**errmsg??int sockfd);
????int read_id(sqlite3*db??char**errmsg??char*user);//???????????
????void read_id_name(sqlite3*db??char**errmsg??vpChat temp);//???????????
?????????????????????????
????????#include”data.h”??????????????????
???????data.h????2????????????????????
????#ifndef__DATA__
????#define__DATA__
????#define MAXSIZE 100
????#include<stdio.h>
????#include<stdlib.h>
????#include<errno.h>
????#include<string.h>
????#include<netdb.h>
????#include<netinet/in.h>
????#include<sys/socket.h>
????#include<arpa/inet.h>
????#include<ctype.h>
????#include<sys/types.h>
????#include<sys/stat.h>
????#include<fcntl.h>
????#include<sqlite3.h>
????#include<unistd.h>
????#include<signal.h>
????#include<time.h>
????#include<termios.h>
????#include<assert.h>
????#define portnumber 8000//???????
????struct chat
????{
????char name[20];//?????
????char passwd[20];//????
????int cmd;//????
????int revert;//?????
????char toname[20];//???????
????char msg[800];//???????
????int flag;
????int sockfd;
????char time[40];
????char filename[40];
????};
????typedef struct chat stChat;
????typedef struct chat*vpChat;
????enum cmd
????{
????LOGID=0??
????REG=1??
????LOG=2??
????USERHELP=3??
????USEREXIT=4??
????CHAT=3??
????ALL=4??
????SMILE=5??
????WELCOME=6??
????EXIT=7??
????HELP=8??
????PASSWD=9??
????BOOT=10??
????STEP=11??
????BAN=12??
????SET=13??
????SEE=14??
????DATA=15??
????SEND=16??
????CHANGE=17
????};
????enum revert
????{
????USERIN=1??
????USEROUT=-1??
????PASSWDOK=2??
????PASSWDNO=-2??
????ONLINEIN=3??
????ONLINEOUT=-3??
????REGNO=-4??
????REGOK=4??
????MYFLAGOK=-5??
????MYFLAGNO=-6??
????TOFLAGOK=-7??
????TOFLAGNO=-8??
????CHATOK=1??
????ALLOK=6??
????SMILEOK=6??
????WELCOMEOK=6??
????SEEOK=7??
????BOOTOK=1??
????DATAOK=8??
????SENDOK=2
????};
????#endif
??????????????????????
????insert.c???????
????#include"data.h"
????void insert_server()//?????????????
????{
????system("reset");
????printf(" **********************************
");
????printf(" *??????*
");
????printf(" *???????????*
");
????printf(" **********************************
");
????}
????sogo?????????????????????????????????????
????sql.c???????
????????????????????????й??
????#include"data.h"
????void is_malloc_ok(vpChat*list)
????{
????*list=(vpChat)malloc(sizeof(stChat));
????if(*list==NULL)
????{
????exit(1);
????}
????}
????void is_sqlite(int rc)//?????????
????{
????if(rc==SQLITE_OK)
????{
????printf("sqlite%d succse
"??__LINE__);
????}
????else
????{
????printf("????????????????SQLITE3???????!
");
????printf("sqlite%d error
"??__LINE__);
????exit(1);
????}
????}
????void is_sqlite_ok(int rc)
????{
????if(rc==SQLITE_OK)
????{
????printf("sqlite%d succse
"??__LINE__);
????}
????else
????{
????printf("sqlite%d error
"??__LINE__);
????}
????}
????void open_db(sqlite3**db)//???????
????{
????int rc;
????rc=sqlite3_open("server.db"??db);
????is_sqlite(rc);
????}
????void creat_user_db(sqlite3*db??char**errmsg)//????user?????
????{
????int rc;
????rc=sqlite3_exec(db??"create table user(id integer primary key autoincrement??name text??passwd text)"??NULL??NULL??errmsg);
????is_sqlite_ok(rc);
????}
????void creat_data_db(sqlite3*db??char**errmsg)//????data?????
????{
????int rc;
????rc=sqlite3_exec(db??"create table data(id integer primary key autoincrement??time text??name text??toname text??msg text)"??NULL??NULL??errmsg);
????is_sqlite_ok(rc);
????}
????void creat_online_db(sqlite3*db??char**errmsg)//????online?????
????{
????int rc;
????rc=sqlite3_exec(db??"create table online(id integer primary key autoincrement??name text??socket integer??flag integer)"??NULL??NULL??errmsg);
????is_sqlite_ok(rc);
????}
????void creat_server_db(sqlite3*db??char**errmsg)//????server?????
????{
????int rc;
????rc=sqlite3_exec(db??"create table server(id integer primary key autoincrement??time text)"??NULL??NULL??errmsg);
????is_sqlite_ok(rc);
????}
????void insert_server_db(sqlite3*db??char*time??char**errmsg)//??server????????????
????{
????int rc;
????char sql[1024];
????sprintf(sql??"insert into server(time)values('%s')"??time);
????rc=sqlite3_exec(db??sql??NULL??NULL??errmsg);
????is_sqlite(rc);
????printf("??????????????
");
????}
????void insert_data_db(sqlite3*db??char**errmsg??char*time??vpChat temp)//??????????
????{
????int rc;
????char sql[1024];
????sprintf(sql??"insert into data(time??name??toname??msg)values('%s'??'%s'??'%s'??'%s')"??time??temp->name??temp->toname??temp->msg);
????rc=sqlite3_exec(db??sql??NULL??NULL??errmsg);
????is_sqlite(rc);
????printf("???????????????
");
????}
????void delete_clean_db(sqlite3*db??char*tablename??char**errmsg)//????????
????{
????char sql[1024];
????int rc;
????sprintf(sql??"delete from%s where name!='root'"??tablename);
????rc=sqlite3_exec(db??"delete from student"??NULL??NULL??errmsg);
????is_sqlite(rc);
????}
????void read_db_ok(sqlite3*db??char*errmsg??char*tablename)//???????????
????{
????int rc;
????int i;
????char sql[1024];
????sqlite3_stmt*stmt=NULL;
????sprintf(sql??"select*from%s"??tablename);
????rc=sqlite3_prepare(db??sql??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int ncolumn;
????ncolumn=sqlite3_column_count(stmt);
????while(rc==SQLITE_ROW)
????{
????for(i=0;i<ncolumn;i++)
????{
????printf("%s|"??sqlite3_column_text(stmt??i));
????}
????printf("
");
????rc=sqlite3_step(stmt);
????}
????}
????int read_online_fd(sqlite3*db??char**errmsg??char*user)//???FD
????{
????int rc;
????int i;
????char fd[100];
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from online"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????if(userflag==0)
????{
????my_strcpy(fd??sqlite3_column_text(stmt??2));
????return my_atoi(fd);
????}
????rc=sqlite3_step(stmt);
????}
????return ONLINEOUT;
????}
????int read_online_flag(sqlite3*db??char**errmsg??char*user)//?????????????
????{
????int rc;
????int i;
????char flag[10];
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from online"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????userflag=my_strcmp(user??sqlite3_column_text(stmt??1));
????if(userflag==0)
????{
????my_strcpy(flag??sqlite3_column_text(stmt??3));
????return my_atoi(flag);
????}
????rc=sqlite3_step(stmt);
????}
????return-10;
????}
????void write_online_all(sqlite3*db??char**errmsg??vpChat temp)//????????????????
????{
????int rc;
????int flag;
????char fd[100];
????sqlite3_stmt*stmt=NULL;
????rc=sqlite3_prepare(db??"select*from online"??-1??&stmt??0);
????is_sqlite_ok(rc);
????rc=sqlite3_step(stmt);
????int userflag=1;
????while(rc==SQLITE_ROW)
????{
????flag=my_strcmp(temp->name??sqlite3_column_text(stmt??1));
????my_strcpy(fd??sqlite3_column_text(stmt??2));
????if(flag!=0)
????{
????write(my_atoi(fd)??temp??sizeof(stChat));
????}
????sleep(1);
????rc=sqlite3_step(stmt);
????}
????}
????int update_passwd(sqlite3*db??char**errmsg??char*name??char*passwd)//???????
????{
????int rc;
????char sql[1024];
????sprintf(sql??"update user set passwd='%s'where name='%s'"??passwd??name);
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11