代码块
typedef struct sockaddr SA;
typedef struct
{
int type;
char name[N];
char data[256]; // password or word or remark
} MSG;
void do_register(int sockfd, MSG *pbuf)
{
//封装注册MSG包
pbuf->type = R;
printf("input name : ");
scanf("%s", pbuf->name);
printf("input password : ");
scanf("%s", pbuf->data);
//发送注册包
send(sockfd, pbuf, sizeof(MSG), 0);
//接收服务器反馈
recv(sockfd, pbuf, sizeof(MSG), 0);
printf("register : %s\n", pbuf->data);
return;
}
int do_login(int sockfd, MSG *pbuf)
{
//封装登录MSG包
pbuf->type = L;
printf("input name : ");
scanf("%s", pbuf->name);
printf("input password : ");
scanf("%s", pbuf->data);
//发送登录包
send(sockfd, pbuf, sizeof(MSG), 0);
//接收服务器反馈
recv(sockfd, pbuf, sizeof(MSG), 0);
if (strncmp(pbuf->data, "OK", 3) == 0)
{
printf("login : OK\n");
return 1;
}
printf("login : %s\n", pbuf->data);
return 0;
}
void print(char *p)
{
int count = 0, len;
char *q;
printf(" ");
while ( *p )
{
if (*p == ' ' && count == 0) p++;
printf("%c", *p++);
count++;
/* if a new word will begin */
if (*(p-1) == ' ' && *p != ' ')
{
q = p;
len = 0;
/* count the length of next word */
while (*q != ' ' && *q != '\0')
{
len++;
q++;
}
if ((COL - count) < len)
{
printf("\n ");
count = 0;
}
}
if (count == 50)
{
count = 0;
printf("\n ");
}
}
return;
}
void do_query(int sockfd, MSG *pbuf)
{
//封装查询包
pbuf->type = Q;
while ( 1 )
{
printf("input word : ");
scanf("%s", pbuf->data);
if (strcmp(pbuf->data, "#") == 0)
break;
//发送查询包
send(sockfd, pbuf, sizeof(MSG), 0);
//接收服务器反馈
recv(sockfd, pbuf, sizeof(MSG), 0);
print(pbuf->data);
printf("\n");
}
return;
}
void do_history(int sockfd, MSG *pbuf)
{
//封装查询历史包
pbuf->type = H;
//发送历史包
send(sockfd, pbuf, sizeof(MSG), 0);
while ( 1 )
{
//接收服务器反馈的历史信息
recv(sockfd, pbuf, sizeof(MSG), 0);
if (pbuf->data[0] == '\0')
break;
printf("%s\n", pbuf->data);
}
return;
}
int main(int argc, char *argv[])
{
int sockfd, login = 0;
struct sockaddr_in servaddr;
MSG buf;
char clean[64];
if (argc < 3)
{
printf("Usage : %s \n", argv[0]);
exit(-1);
}
// 创建socket,用于下一步建立连接
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("fail to socket");
exit(-1);
}
//servaddr,服务器端的地址结构
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET; //地址族
servaddr.sin_port = htons(atoi(argv[2])); //端口号
servaddr.sin_addr.s_addr = inet_addr(argv[1]); //IP地址
// 建立客户端与服务器之间的连接
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
{
perror("fail to connect");
exit(-1);
}
int n;
while ( 1 )
{
printf("************************************\n");
printf("* 1: register 2: login 3: quit *\n");
printf("************************************\n");
printf("please choose : ");
if (scanf("%d", &n) == 0)
{
fgets(clean, 64, stdin);
printf("\n");
continue;
}
switch ( n )
{
case 1 :
printf("\n");
do_register(sockfd, &buf);
printf("\n");
break;
case 2 :
printf("\n");
if (do_login(sockfd, &buf) == 1)
{
printf("\n");
goto next;
}
printf("\n");
break;
case 3 :
close(sockfd);
exit(0);
}
}
next:
while ( 1 )
{
printf("***********************************************\n");
printf("* 1: query_word 2: history_record 3: quit *\n");
printf("***********************************************\n");
printf("please choose : ");
if (scanf("%d", &n) == 0)
{
fgets(clean, 64, stdin);
printf("\n");
continue;
}
switch ( n )
{
case 1 :
printf("\n");
do_query(sockfd, &buf);
printf("\n");
break;
case 2 :
printf("\n");
do_history(sockfd, &buf);
printf("\n");
break;
case 3 :
close(sockfd);
exit(0);
}
}
return 0;
}
typedef struct sockaddr SA;
typedef struct
{
int type;
char name[N];
char data[256]; // password or word
} MSG;
void do_register(int connfd, MSG *pbuf, sqlite3 *db)
{
char sqlstr[128];
char *errmsg;
//组合SQL语句,插入一条用户注册信息
sprintf(sqlstr, "insert into usr values ('%s', '%s')", pbuf->name, pbuf->data);
printf("%s\n", sqlstr);
//执行SQL语句
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK)
{
sqlite3_free(errmsg);
sprintf(pbuf->data, "user %s already exist!!!", pbuf->name);
}
else
{
strncpy(pbuf->data, "OK", 256);
}
//向客户端反馈信息,如,注册成功OK
send(connfd, pbuf, sizeof(MSG), 0);
return;
}
/******************************************************************************/
void do_login(int connfd, MSG *pbuf, sqlite3 *db)
{
char sqlstr[128];
char *errmsg, **result;
int nrow, ncolumn;
sprintf(sqlstr, "select * from usr where name = '%s' and pass = '%s'", pbuf->name, pbuf->data);
if (sqlite3_get_table(db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
if (nrow == 0)
{
strncpy(pbuf->data, "name or password is wrong!!!", 256);
}
else
{
strncpy(pbuf->data, "OK", 256);
}
send(connfd, pbuf, sizeof(MSG), 0);
sqlite3_free_table(result);
return;
}
/******************************************************************************/
void get_date(char date[])
{
time_t t;
struct tm *tp;
time(&t);
tp = localtime(&t);
/*
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d", tp->tm_year+1900,
tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
*/
strftime(date, 64, "%Y-%m-%d %H:%M:%S", tp);
return;
}
int do_searchword(int connfd, MSG *pbuf)
{
FILE *fp;
char line[300];
char *p;
int len, result;
len = strlen(pbuf->data);
//读词库文件dict.txt
if ((fp = fopen("dict.txt", "r")) == NULL)
{
strcpy(pbuf->data, "dict on server can't be opened :(");
send(connfd, pbuf, sizeof(MSG), 0);
}
printf("query word is %s\n", pbuf->data);
//取一行数据
while (fgets(line, 300, fp) != NULL)
{
result = strncmp(pbuf->data, line, len);
if (result > 0) continue;
if (result < 0 || line[len] != ' ') break;
p = line + len;
while (*p == ' ') p++;
strcpy(pbuf->data, p);
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}
void do_query(int connfd, MSG *pbuf, sqlite3 *db)
{
char sqlstr[128], *errmsg;
int len, result, found = 0;
char date[64], word[64];
strcpy(word, pbuf->data);//pbuf->data,用户查询的单词
found = do_searchword(connfd, pbuf);
if ( found )
{
get_date(date);
//向历史记录表中插入一条记录信息
sprintf(sqlstr, "insert into record values ('%s', '%s', '%s')", pbuf->name, date, word);
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
}
else
{
strcpy(pbuf->data, "not found\n");
}
//向用户反馈信息,如,"not found"
send(connfd, pbuf, sizeof(MSG), 0);
return;
}
/***************************************************************************/
int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{
int connfd;
MSG buf;
connfd = *(int *)arg;
sprintf(buf.data, "%s : %s", f_value[1], f_value[2]);
send(connfd, &buf, sizeof(buf), 0);
return 0;
}
void do_history(int connfd, MSG *pbuf, sqlite3 *db)
{
char sqlstr[128], *errmsg;
sprintf(sqlstr, "select * from record where name = '%s'", pbuf->name);
if (sqlite3_exec(db, sqlstr, history_callback, (void *)&connfd, &errmsg) != SQLITE_OK)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
pbuf->data[0] = '\0';
send(connfd, pbuf, sizeof(MSG), 0);
return;
}
/******************************************************************************/
void do_client(int connfd, sqlite3 *db)
{
MSG buf;
//接收客户端数据,并存放到MSG包
while (recv(connfd, &buf, sizeof(buf), 0) > 0) // receive request
{
//根据包的类型,做相应的处理
switch ( buf.type )
{
case R : //客户端发来注册包
do_register(connfd, &buf, db);
break;
case L ://客户端发来登录包
do_login(connfd, &buf, db);
break;
case Q ://客户端发来查询包
do_query(connfd, &buf, db);
break;
case H ://客户端发来查询历史包
do_history(connfd, &buf, db);
break;
}
}
printf("client quit\n");
exit(0);
return;
}
int main(int argc, char *argv[])
{
int listenfd, connfd;
struct sockaddr_in myaddr;
pid_t pid;
MSG buf;
sqlite3 *db;
if (argc < 3)
{
printf("Usage : %s \n", argv[0]);
exit(-1);
}
//打开或创建数据库
if (sqlite3_open(DATABASE, &db) != SQLITE_OK)
{
printf("error : %s\n", sqlite3_errmsg(db));
exit(-1);
}
// 创建socket
if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("fail to socket");
exit(-1);
}
//绑定服务器地址,myaddr服务器地址结构
bzero(&myaddr, sizeof(myaddr));
myaddr.sin_family = PF_INET; //地址族
myaddr.sin_port = htons(atoi(argv[2])); //端口号
myaddr.sin_addr.s_addr = inet_addr(argv[1]); //服务器IP
if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
{
perror("fail to bind");
exit(-1);
}
// 设置监听队列的长度
if (listen(listenfd, 5) < 0)
{
perror("fail to listen");
exit(-1);
}
signal(SIGCHLD, SIG_IGN);
//循环与客户端建立连接,实现客户端通信(send/recv)
while ( 1 )
{
//接受客户端建立连接
if ((connfd = accept(listenfd, NULL, NULL)) < 0)
{
perror("fail to accept");
exit(-1);
}
if ((pid = fork()) < 0)
{
perror("fail to fork");
exit(-1);
}
else if (pid == 0)
{
do_client(connfd, db);
}
close(connfd);
}
return 0;
}