电子词典(全代码)

这里写目录标题

  • 服务器
    • main.c
    • server.c
    • sqlite.c
    • public.h
  • 客户端
    • main.c
    • client.c
    • sqlite.c
    • public.h

服务器

main.c

#include "public.h"

int main(int argc, char const *argv[])
{
    
    int sfd = 0;

    if(NET_init(&sfd) == -1) printf("网络初始化失败");

    if(sqlite3_init(&db) == -1) printf("数据库初始化失败");

    /*等待服务器链接*/
    if(wait_cli_link(sfd) == -1) printf("服务器出错");

    return 0;
}

server.c

#include "public.h"

/*等待回收进程*/
void handler(int sig)
{
    while(waitpid(-1,NULL,0) >0);
}

/*网络初始化*/
int NET_init(int* sfd)
{
    /*创建流式套接字*/
    *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)))
    {
        ERR_MSG("setsockopt");
        return -1;
    }

    /*绑定服务器的IP和端口*/
    struct sockaddr_in sin_addr;//定义本机通用地址信息结构体
    sin_addr.sin_family = AF_INET;//必须填AF_INET
    sin_addr.sin_port = htons(8080);//端口号
    sin_addr.sin_addr.s_addr = inet_addr("192.168.2.101");//填充IP地址
    if(bind(*sfd,(const struct sockaddr*)&sin_addr,sizeof(sin_addr))<0)
    {
        ERR_MSG("bind");
        return -1;
    }

    /*将套接字设定为被动监听状态*/
    if(listen(*sfd,BACKLOG) == -1)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("网络初始化完成\r\n当前服务器>192.168.2.101:8080\r\n");
    return 0;   
}

/*等待服务器链接*/
int wait_cli_link(int sfd)
{
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    int newfd = -1;
    pid_t cli_pid = 0;

    while(1)
    {
        newfd = accept(sfd,(struct sockaddr*)&addr,&addrlen);
        if(newfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }
        cli_pid = fork();
        if(cli_pid == 0)
        {
            close(sfd);
            printf("客户端已连接>%s:%d\r\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
            cli_interactive(newfd,addr);
            printf("客户端已断开>%s:%d\r\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
            exit(0);
        }
    }

}

/*客户端交互进程*/
void cli_interactive(int fd,struct sockaddr_in cli_addr)
{
    time_t sys_t = time(NULL);
    struct tm *fomattime = localtime(&sys_t);
    char time_str[50] = {0};

    char str[1024];
    char buf[1024];
    char word[25] = {0};
    cli_date_t client;
    bzero(client.user, sizeof(client.user));
    bzero(client.pawd, sizeof(client.pawd));
    int res;
    /*flag: 0-未登录状态 1-已登录状态*/
    char flag = 0;

    while(1)
    {
        switch(flag)
        {
            case 0:{//等待登录
                res = recv(fd,str,sizeof(str),0);
                analysis_login_json(str,&client);
                /*插入注册表*/
                if(insert_date(&db,"registry",client) != 0)
                {//该账号已注册
                    switch(check_user_pawd(client))
                    {
                        case 0:{//账号密码正确
                            if(insert_date(&db,"login",client) != 0)
                            {//该账号已被登录
                                TCP_printf(fd,"The account has been logged in\r\n");
                            }
                            else
                            {//账号已成功登录
                                
                                TCP_printf(fd,"Login succeeded\r\n");
                                flag = 1;
                            }
                        }break;

                        case 1:{//密码错误
                            TCP_printf(fd,"password error\tr\n");
                        }break;

                        case 2:{//账号错误
                            TCP_printf(fd,"user error\r\n");
                        }break;
                    }
                }
                else
                {//该账号未被注册,直接注册并登录
                    insert_date(&db,"login",client);
                    TCP_printf(fd,"The username does not exist, it is now registered and logged in\r\n");
                    flag = 1;
                }
            }break;

            case 1:{//已登录
                res = recv(fd,str,sizeof(str),0);

                if(res == 0)
                {
                    /*删除登录表*/
                    if(delete_login(client.user) == -1) printf("delete login fail!!\r\n");
                    printf("The client close >%s:%d\r\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
                    exit(0);
                }

                if(analysis_word_json(str,word) == -1)
                {
                    TCP_printf(fd,"405 Not find\r\n");
                }else
                {
                    if(check_word(word,buf) == -1)
                    {
                        printf("404 Not find >> %s\r\n",word);
                    }
                    else 
                    {
                        GET_TIME(time_str);
                        sprintf(str,"{\"word\":%s,\"translate\":%s,\"time\":%s}",word,buf,time_str);
                        TCP_printf(fd,"%s\r\n",str);
                    }
                }
            }break;
        }
    }

}

/*解析来自客户端的登录json包*/
void analysis_login_json(const char* str,cli_date_t* client)
{
    char* p1 = NULL;
    int i;

    p1 = strstr(str,"\"user\":");
    p1+=7;

    for(i=0;*(p1+i)!=',';i++)
    {
        client->user[i] = *(p1+i);
    }

    p1 = strstr(str,"\"pawd\":");
    p1+=7;

    for(i=0;*(p1+i)!='}';i++)
    {
        client->pawd[i] = *(p1+i);
    }
}

/*解析来自客户端的登录json包*/
int analysis_word_json(const char* str,char* word)
{
    char* p1 = NULL;
    int i;

    bzero(word,sizeof(word));
    if((p1 = strstr(str,"\"word\":")) == NULL)
    {
        return -1;
    }
    p1+=7;

    for(i=0;*(p1+i)!='}';i++)
    {
        word[i] = *(p1+i);
    }
}

void TCP_printf(int sockfd,char* fmt,...)
{
    char buf[1024];
    char buf_tim[50];
    time_t sys_t;
    struct tm *fomattime;
    va_list ap;
    va_start(ap,fmt);
    vsprintf(buf,fmt,ap);
    va_end(ap);

    GET_TIME(buf_tim);
    send(sockfd,buf,strlen(buf),0);
}

sqlite.c

#include "public.h"

sqlite3 *db;

/*解析文件数据*/
void parsing_file(const char* date,date_t* dict)
{
    char str[100] = {0};
    char buf[100] = {0};
    strcpy(str,date);
    char* p = str;
    int i = 0;
    int cnt = 0;

    /*获取单词*/
    for(i=0;cnt!=2;i++)
    {
        buf[i] = *(p+i);
        if(*(p+i) == ' ') cnt++;
    }
    strcpy(dict->word,buf);
    /*获取翻译*/
    strcpy(dict->translate,(p+i));
    dict->translate[strlen(dict->translate)-1] = '\0';
}

int sqlite3_init(sqlite3 **db)
{
    char str[1024] = {0};
    char SQ_name[25] = {0};
    char* err_msg = NULL;
    int menu_num = 0;
    char ch = 0;
    int num = 0;

    FILE* fp = NULL;
    int ret = 0;
    int cnt = 0;
    date_t dict;

    if((fp=fopen("./dict.txt","r")) == NULL)
    {//打开文件读取数据
        perror("open file");
        return -1;
    }

    /*打开数据库*/
    if(sqlite3_open("sq.db",&*db) != SQLITE_OK)
    {
        printf("sqlite3_open : %s\r\n",err_msg);
        return -1;
    }
    /*创建注册表 主键为user*/
    sprintf(str,"create table if not exists Registry (user char PRIMARY KEY, pawd char)");
    if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }

    /*删除原有登录表*/
    if(sqlite3_exec(*db,"DROP TABLE login",NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
    }
    /*创建登录表 主键为user*/
    sprintf(str,"create table if not exists login (user char PRIMARY KEY, pawd char)");
    if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }
    printf("Do you want to reload the dictionary? [y/n]:");
    scanf("%c",&ch);
    /*导入词典*/
    if(ch == 'y')
    {
        /*删除原有词典表*/
        if(sqlite3_exec(*db,"DROP TABLE dict",NULL,NULL,&err_msg) != SQLITE_OK)
        {
            printf("sqlite3_exec : %s\r\n",err_msg);
        }

        /*创建词典表*/
        sprintf(str,"create table if not exists dict (word char, translate char)");
        if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
        {
            printf("sqlite3_exec : %s\r\n",err_msg);
            return -1;
        }

        do
        {
            bzero(str,sizeof(str));
            fgets(str,sizeof(str),fp);//读取词典文件
            ret = strlen(str);
            if(ret == 0) break;
            parsing_file(str,&dict);

            sprintf(str,"insert into dict values (\"%s\",\"%s\")\r\n",dict.word,dict.translate);
            if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
            {
                printf("sqlite3_exec : %s\r\n",err_msg);
                printf("str: %s\r\n",str);
                return -1;
            }
            printf("\r%.0f%%",(++num/7987.0)*100.0);
        }while(ret != 0);
        printf("\n词典导入完成\r\n");
    }
    
    printf("词典已打开\r\n");
    fclose(fp);
    return 0;
}

/*全字段插入*/
int insert_date(sqlite3 **db,const char* name,cli_date_t client)
{
    char* err_msg = NULL;
    char str[256] = {0};

    sprintf(str,"insert into %s values ('%s','%s')\r\n",name,client.user,client.pawd);
    if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }
    return 0;
}

/*查询对比账号密码是否正确 0-账号密码正确 1-密码错误 2-不存在该账号*/
int check_user_pawd(cli_date_t client)
{
    char* err_msg = NULL;
    char str[100] = {0};
    char** pres = NULL;
    int i = 1;
    int row,column;

    /*从注册表获取数据*/
    sprintf(str,"select * from registry WHERE user = '%s'",client.user);
    if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_get_table : %s\r\n",err_msg);
        return -1;
    }
    if(strcmp(client.user, pres[2]) != 0)
    {
        sqlite3_free_table(pres);
        return -1;
    }
    if(strcmp(client.pawd, pres[3]) != 0)
    {
        sqlite3_free_table(pres);
        return 1;//密码错误
    }
    sqlite3_free_table(pres);
    return 0;//密码正确
}

/*查询对比单词翻译 0-查询完成 -1-查询失败*/
int check_word(const char* word,char* buf)
{
    char* err_msg = NULL;
    char str[100] = {0};
    char** pres = NULL;
    int i = 1;
    int row,column;

    /*从注册表获取数据*/
    sprintf(str,"select * from dict");
    // sprintf(str,"select * from dict where word = %s",word);
    if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_get_table : %s\r\n",err_msg);
        return -1;
    }

    for(i=0; i<row*2; i+=2)
    {
        if(strstr(pres[i+2],word) != NULL)
        {
            strcpy(buf,pres[i+3]);
            sqlite3_free_table(pres);
            return 0;
        }
    }
    sqlite3_free_table(pres);
    return -1;//查询失败
}

/*删除登录表*/
int delete_login(const char* user)
{
    char* err_msg = NULL;
    char str[50] = {0};
    /*创建注册表 主键为user*/
    sprintf(str,"DELETE FROM login WHERE user='%s'",user);
    if(sqlite3_exec(db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }
    return 0;
}

public.h

#ifndef __PUBLIC_H_
#define __PUBLIC_H_

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define R O_RDONLY
#define W O_WRONLY|O_CREAT|O_TRUNC
#define A O_WRONLY|O_CREAT|O_APPEND
#define Rp O_RDWR
#define Wp O_RDWR|O_CREAT|O_TRUNC
#define Ap O_RDWR|O_CREAT|O_APPEND

#define IP "192.168.2.52"
#define PORT 8080
#define BACKLOG 128

/*输入打印的错误类型 打印错误信息,行,具体函数*/
#define ERR_MSG(msg) do{\
    fprintf(stderr, "Line:%d :%s %s\n",__LINE__, __FILE__,__func__);\
    perror(msg);\
}while(0)
/*获取时间戳*/
#define GET_TIME(str) do{\
    time_t sys_t = time(NULL);\
    struct tm *fomattime = localtime(&sys_t);\
    sprintf(str,"[%.4d-%.2d-%.2d %.2d:%.2d:%.2d]",\
    fomattime->tm_year+1900,fomattime->tm_mon+1,fomattime->tm_mday,\
    fomattime->tm_hour,fomattime->tm_min,fomattime->tm_sec);\
}while(0)

#pragma pack(1) //设置默认对齐系数 :() 中的参数只能填2^n (n=0,1,2,3,4,5......)

typedef struct
{
    char word[50];
    char translate[50];
}date_t;

typedef struct
{
    int line;
    char user[50];
    char pawd[50];
}cli_date_t;

extern sqlite3 *db;

/*************************服务器*******************************/
/*网络初始化*/
int NET_init(int* sfd);
/*TCP服务器数据发送*/
void TCP_printf(int sockfd,char* fmt,...);
/*等待服务器链接*/
int wait_cli_link(int sfd);
/*客户端交互进程*/
void cli_interactive(int fd,struct sockaddr_in cli_addr);
/*解析来自客户端的登录json包*/
void analysis_login_json(const char* str,cli_date_t* client);
/*解析来自客户端的登录json包*/
int analysis_word_json(const char* str,char* word);
/*************************数据库*******************************/
/*数据库初始化*/
int sqlite3_init(sqlite3 **db);
/*全字段插入*/
int insert_date(sqlite3 **db,const char* name,cli_date_t client);
/*查询对比账号密码是否正确 0-账号密码正确 1-密码错误 2-不存在该账号*/
int check_user_pawd(cli_date_t client);
/*查询对比单词翻译 0-查询完成 -1-查询失败*/
int check_word(const char* word,char* buf);
/*删除登录表*/
int delete_login(const char* user);

#endif

客户端

main.c

#include "public.h"

int main(int argc, char const *argv[])
{
    time_t sys_t = time(NULL);
    struct tm *fomattime = localtime(&sys_t);
    char time_str[50] = {0};
    
    char ch = 0;
    int sfd = 0;
    pthread_t Rx_server_pid;//线程号

    if(NET_init(&sfd) == -1) printf("网络初始化失败");

    if(create_server_line(&Rx_server_pid,&sfd) == -1) printf("客户端接收线程初始化失败");

    if(sqlite3_init(&db) == -1) printf("数据库初始化失败");

    /*等待服务器链接*/
    Client_Operations(sfd);

    return 0;
}

client.c

#include "public.h"

char cil_flig = 0;
cli_date_t cilent;

/*网络初始化*/
int NET_init(int* cfd)
{
    /*创建流式套接字*/
    *cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(cfd < 0)
    {//如果创建失败则
        ERR_MSG("socket");
        return -1;
    }

    /*绑定服务器的IP和端口*/
    struct sockaddr_in cin_addr;//定义本机通用地址信息结构体
    cin_addr.sin_family = AF_INET;//必须填AF_INET
    cin_addr.sin_port = htons(8080);//端口号
    cin_addr.sin_addr.s_addr = inet_addr("192.168.2.101");//填充IP地址
    if(connect(*cfd,(const struct sockaddr*)&cin_addr,sizeof(cin_addr)) == -1)//链接服务器
    {
        ERR_MSG("connect");
        return -1;
    }
    printf("网络初始化完成\r\n当前已链接服务器>192.168.2.101:8080\r\n");
    return 0;   
}

/*创建客户端接收线程*/
int create_server_line(pthread_t* Rx_pid,int* sfd)
{
    if(pthread_create(Rx_pid, NULL,TCP_server_Rx,sfd) != 0)
    {//如果创建失败
        perror("pthread task1");
        return -1;
    }
    return 0;
}

void TCP_printf(int sockfd,char* fmt,...)
{
    char buf[1024];
    char buf_tim[50];
    time_t sys_t;
    struct tm *fomattime;
    va_list ap;
    va_start(ap,fmt);
    vsprintf(buf,fmt,ap);
    va_end(ap);

    GET_TIME(buf_tim);
    send(sockfd,buf,strlen(buf),0);
}

/*解析单词包*/
int server_josn(const char* str,record_date_t* record)
{
    char* p1 = NULL;
    char buf[50] = {0};
    int i;

    p1 = strstr(str, "\"word\":");
    p1+=7;
    for(i=0;*(p1+i)!=',';i++)
    {
        record->date[i] = *(p1+i);
    }

    p1 = strstr(str, "\"translate\":");
    p1+=12;
    for(i=0;*(p1+i)!=',';i++)
    {
        buf[i] = *(p1+i);
    }
    strcat(record->date,buf);

    p1 = strstr(str, "\"time\":");
    p1+=7;
    for(i=0;*(p1+i)!='}';i++)
    {
        record->time[i] = *(p1+i);
    }
}

/*选项菜单*/
void menu(void)
{
    printf("********************************************\r\n");
    printf("1. 登录/注册 | 2. 查询 | 3.历史记录| 4. 退出客户端\r\n");
    printf("********************************************\r\n");
}

/*接收服务器数据*/
void* TCP_server_Rx(void* input)
{
    time_t sys_t = time(NULL);
    struct tm *fomattime = localtime(&sys_t);
    char time_str[50] = {0};

    record_date_t record;
    int res = 0;
    int sfd = *(int*) input;
    char buf1[256] = {0};
    char buf2[256] = {0};

    printf("子线程启动\r\n");
    while(1)
    {
        res = recv(sfd,buf1,sizeof(buf1),0);
        if(res == -1) continue;

        switch(cil_flig)
        {
            case 0:{//客户端未登录
                if(strstr(buf1,"Login succeeded") != NULL)
                {
                    GET_TIME(time_str);
                    sprintf(buf2,"%s>>%s\r\n",time_str,buf1);
                    printf("%s\r\n",buf2);
                    /*创建/打开用户记录*/
                    sqlite3_user_create(&db,cilent);
                    cil_flig = 1;
                    
                }else if(strstr(buf1,"The username does not exist, it is now registered and logged in") != NULL)
                {
                    GET_TIME(time_str);
                    sprintf(buf2,"%s>>%s\r\n",time_str,buf1);
                    printf("%s\r\n",buf2);
                    cil_flig = 1;
                }else
                {
                    printf("server connection failed\r\n");
                }
            }break;

            case 1:{//客户端已登录
                bzero(record.date,sizeof(record.date));
                bzero(record.time,sizeof(record.time));
                server_josn(buf1,&record);
                /*全字段插入*/
                insert_date(&db,cilent.user,record);
                GET_TIME(time_str);
                sprintf(buf2,"%s>>%s\r\n",time_str,record.date);
                printf("%s",buf2);
            }break;
        }
        if(res == 0)
        {
            printf("服务器下线\r\n");
        }
    }
    printf("\r\n");
}

/*客户端功能运行*/
int Client_Operations(int sfd)
{
    int menu_num = 0;
    int res = 0;
    char buf1[256] = {0};
    char buf2[256] = {0};

    while(1)
    {
        menu();
        scanf("%d", &menu_num);
        switch(menu_num)
        {
            case 1:{
                printf("Please enter your user: ");
                scanf("\r\n%s",cilent.user);
                printf("Please enter your password: ");
                scanf("\r\n%s",cilent.pawd);

                sprintf(buf1,"{\"user\":%s,\"pawd\":%s}",cilent.user,cilent.pawd);
                TCP_printf(sfd,"%s",buf1);//发送数据包
            }break;

            case 2:{
                printf("Please enter the word you want to query:");
                scanf("\r\n%s",buf1);
                sprintf(buf2,"{\"word\":%s}",buf1);
                TCP_printf(sfd,"%s",buf2);//发送数据包
            }break;

            case 3:{
                /*打印历史记录*/
                sqlite3_History(cilent.user);
            }break;

            case 4:{

            }break;
        }
    }
    
}

sqlite.c

#include "public.h"

sqlite3 *db;

int sqlite3_init(sqlite3 **db)
{
    char str[1024] = {0};
    char SQ_name[25] = {0};
    char* err_msg = NULL;
    int menu_num = 0;
    char ch = 0;
    int num = 0;

    int ret = 0;
    int cnt = 0;
    date_t dict;

    /*打开数据库*/
    if(sqlite3_open("user.db",&*db) != SQLITE_OK)
    {
        printf("sqlite3_open : %s\r\n",err_msg);
        return -1;
    }
    printf("用户数据库已打开\r\n");
    return 0;
}

/*创建/打开用户记录*/
int sqlite3_user_create(sqlite3 **db,cli_date_t cli)
{
    char* err_msg = NULL;
    char str[1024] = {0};
    /*创用户记录 无主键*/
    sprintf(str,"create table if not exists '%s' ( date char , time char)",cli.user);
    if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }
    printf("已创建/打开用户记录\r\n");
}

/*全字段插入*/
int insert_date(sqlite3 **db,const char* name,record_date_t record)
{
    char* err_msg = NULL;
    char str[256] = {0};

    sprintf(str,"insert into '%s' values ('%s','%s')\r\n",name,record.date,record.time);
    if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_exec : %s\r\n",err_msg);
        return -1;
    }
    return 0;
}

/*查询对比单词翻译 0-查询完成 -1-查询失败*/
int check_word(const char* word,char* buf)
{
    char* err_msg = NULL;
    char str[100] = {0};
    char** pres = NULL;
    int i = 1;
    int row,column;

    /*从注册表获取数据*/
    sprintf(str,"select * from dict");
    if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_get_table : %s\r\n)",err_msg);
        return -1;
    }
    printf("row:%d column:%d\r\n",row,column);
    for(i = 0; i < row*2; i+=2)
    {
        if(strstr(*(pres+(i+2)),word) == NULL) continue;
        strcpy(buf,*(pres+(i+3)));
        return 0;//密码正确
    }
    return -1;//查询失败
}

/*打印历史记录*/
void sqlite3_History(const char* name)
{
    char* err_msg = NULL;
    char str[100] = {0};
    char** pres = NULL;
    int i = 0;
    int z = 0;
    int row,column;

    /*从注册表获取数据*/
    sprintf(str,"select * from '%s'",name);
    if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
    {
        printf("sqlite3_get_table : %s\r\n)",err_msg);
        return ;
    }
    printf("row:%d column:%d\r\n",row,column);
    for(i = 0; i < row*2; i+=2)
    {
        if(i==0) printf("----------%s----------|----------%s----------\r\n",pres[i+1],pres[i]);
        else printf("%s\t| %s\r\n",pres[i+1],pres[i]);
    }
}

public.h

#ifndef __PUBLIC_H_
#define __PUBLIC_H_

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define R O_RDONLY
#define W O_WRONLY|O_CREAT|O_TRUNC
#define A O_WRONLY|O_CREAT|O_APPEND
#define Rp O_RDWR
#define Wp O_RDWR|O_CREAT|O_TRUNC
#define Ap O_RDWR|O_CREAT|O_APPEND

#define IP "192.168.2.52"
#define PORT 8080
#define BACKLOG 128

/*输入打印的错误类型 打印错误信息,行,具体函数*/
#define ERR_MSG(msg) do{\
    fprintf(stderr, "Line:%d :%s %s\n",__LINE__, __FILE__,__func__);\
    perror(msg);\
}while(0)
/*获取时间戳*/
#define GET_TIME(str) do{\
    time_t sys_t = time(NULL);\
    struct tm *fomattime = localtime(&sys_t);\
    sprintf(str,"[%.4d-%.2d-%.2d %.2d:%.2d:%.2d]",\
    fomattime->tm_year+1900,fomattime->tm_mon+1,fomattime->tm_mday,\
    fomattime->tm_hour,fomattime->tm_min,fomattime->tm_sec);\
}while(0)

#pragma pack(1) //设置默认对齐系数 :() 中的参数只能填2^n (n=0,1,2,3,4,5......)

typedef struct
{
    char word[50];
    char translate[50];
}date_t;

typedef struct
{
    char user[50];
    char pawd[50];
}cli_date_t;

typedef struct
{
    char time[50];
    char date[50];
}record_date_t;

extern sqlite3 *db;

/*************************客户端*******************************/
/*网络初始化*/
int NET_init(int* sfd);
/*TCP服务器数据发送*/
void TCP_printf(int sockfd,char* fmt,...);
/*接收服务器数据*/
void* TCP_server_Rx(void* input);
/*创建客户端接收线程*/
int create_server_line(pthread_t* Rx_pid,int* sfd);
/*客户端功能运行*/
int Client_Operations(int sfd);
/*解析单词包*/
int server_josn(const char* str,record_date_t* record);
/*************************数据库*******************************/
/*数据库初始化*/
int sqlite3_init(sqlite3 **db);
/*全字段插入*/
int insert_date(sqlite3 **db,const char* name,record_date_t record);
/*创建/打开用户记录*/
int sqlite3_user_create(sqlite3 **db,cli_date_t cli);
/*打印历史记录*/
void sqlite3_History(const char* name);

#endif

你可能感兴趣的:(服务器,网络,linux)