在线词典是基于Linux 多进程并发服务器编程,由服务器端和客户端构成,客户端可以运行在多个不同的主机上连接服务器,服务器对员工信息的操作结果通过数据库sqlite来保存。当用户登录后,根据用户名判断用户是否已注册。如果未注册,则进行注册用户名和密码,如果已经存在该用户名,输入正确密码,则进行查询单词和查询历史记录操作。
服务器端
服务器端功能描述:
服务器端操作流程:
客户端
客户端功能描述:
客户端操作流程:
源代码
服务器端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define N 16
#define R 1 // user register
#define L 2 // user login
#define Q 3 // query word
#define H 4 // history record
//定义数据库
#define DATABASE "my.db"
typedef struct
{
int type;
char name[N];
char data[256]; // password or word
} MSG;
void do_client(int acceptfd, sqlite3 *db);
void do_register(int acceptfd, MSG *msg, sqlite3 *db);
void do_login(int acceptfd, MSG *msg, sqlite3 *db);
void do_query(int acceptfd, MSG *msg, sqlite3 *db);
int do_searchword(int acceptfd, MSG *msg);
void getdata(char data[]);
void do_history(int acceptfd, MSG *msg, sqlite3 *db);
int history_callback(void *arg, int f_num, char **f_value, char **f_name);
int main(int argc, char *argv[])
{
int sockfd, acceptfd;
struct sockaddr_in server_addr;
pid_t pid;
sqlite3 *db;
if (argc < 3)
{
printf("Usage : %s <ip> <port>\n", argv[0]);
exit(-1);
}
//打开数据库(如果数据库已经创建好了,调用函数后,之后利用指针操作数据库)
//数据库里面有两个表,一个负责存放用户名和密码,用户名唯一,另一个负责存放历史记录
if (sqlite3_open(DATABASE, &db) !=


697

被折叠的 条评论
为什么被折叠?



