Linux C实现的在线词典(基于TCP)

基本功能

  1. 注册
  2. 登录
  3. 查询
  4. 查看查询记录

源码

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

#define  N  16
#define  R  1   //  user register
#define  L  2   //  user login
#define  Q  3   //  query word
#define  H  4   //  history record
#define  COL 50
#define  F  0
#define  S  100

#define DATABASE "my.db"

typedef struct sockaddr SA;

typedef struct 
{
    int type;
    char name[N];
    char data[256];   // password or word or remark
} MSG;


服务器端:

#include"head_server.h"

void do_register(int connfd, MSG *pbuf, sqlite3 *db)
{
    char sqlstr[128];
    char *errmsg;

    sprintf(sqlstr, "insert into usr values ('%s', '%s')", pbuf->name, pbuf->data);
    printf("%s\n", sqlstr);
    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);
    }
    send(connfd, pbuf, sizeof(MSG), 0);

    return;
}

void do_login(int connfd,MSG *msg,sqlite3 *db)
{
    char sqlopt[128];
    char *errmsg,**result;
    int r, c;

    sprintf(sqlopt, "select * from usr where name = '%s' and pass = '%s'", msg -> name, msg -> data);
    if (sqlite3_get_table(db, sqlopt, &result, &r, &c, &errmsg) != SQLITE_OK)
    {
        printf("error:%s\n",errmsg);
    }
    if(r == 0)
    {
        msg -> type = F;
    }
    else
    {
        msg -> type = S;
    }
    send(connfd, msg, sizeof(MSG), 0);
}

int do_search(int connfd, MSG *msg)
{
    FILE *fp;
    char str[300];
    int result,len;
    char *p;
    if((fp = fopen("dict.txt", "r")) == NULL)
    {
        strcpy(msg -> data, "File can't be opened!!!");
        send(connfd, msg, sizeof(MSG), 0);
        return -1;
    }
    len = strlen(msg -> data);
    printf("%s\n", msg -> data);
    while(fgets(str,300,fp) != NULL)
    {
        result = strncmp(msg -> data,str,len);
        if(result > 0)
        {
            continue;
        }           
        if(result < 0 || str[len] != ' ')
        {
            break;
        }
        p = str + len;
        while(*p == ' ')
            p ++;
        strcpy(msg -> data, p);
        printf("%s\n",msg -> data);
        send(connfd, msg, sizeof(MSG), 0);

        fclose(fp);

        return 1;
    }
    strcpy(msg -> data, "Word can't be found!!!");
    send(connfd, msg, sizeof(MSG), 0);
    fclose(fp);
    return 0;
}

void get_date(char date[])
{
    time_t t;
    struct tm *tp;
    time(&t);
    tp = localtime(&t);
    strftime(date, 64, "%Y-%m-%d %H:%M:%S", tp);
}
int do_query(int connfd,MSG *msg,sqlite3 *db)
{
    char date[64];
    char word[64];
    char sqlstr[100];
    char *errmsg;
    strcpy(word,msg -> data);
    if(do_search(connfd, msg) != 1)
        return -1;

    get_date(date);
    sprintf(sqlstr, "insert into record values ('%s', '%s', '%s')", msg->name,date, word);
    if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }
}

int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{

    //f_value 每个记录的值    
    //f_num 记录中包含的字段个数
    //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 *msg, sqlite3 *db)
{
    char sqlstr[128], *errmsg;

    sprintf(sqlstr, "select * from record where name = '%s'", msg->name);
    if (sqlite3_exec(db, sqlstr, history_callback, (void *)&connfd, &errmsg) != SQLITE_OK)
    {
        printf("error : %s\n", errmsg);
    }
    msg->data[0] = '\0';
    send(connfd, msg, sizeof(MSG), 0);

}


void do_menu(int connfd, sqlite3 *db)
{
    MSG msg;
    while( recv(connfd, &msg, sizeof(msg), 0) > 0)
    {
        switch(msg.type)
        {
        case R:
            do_register(connfd,&msg,db);
            break;
        case L:
            do_login(connfd,&msg,db);
            break;
        case Q:
            do_query(connfd,&msg,db);
            break;
        case H:
            do_history(connfd, &msg, db);
            break;
        }
    }
    printf("client quit\n");
    exit(0);

}
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);
    }
    if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    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]);
    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);

    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_menu(connfd,db);
        }
        close(connfd);
    }

    return 0;
}

客户端:

#include"head_client.h"

void do_register(int sockfd, MSG *msg)
{
    msg->type = R;
    printf("Please input name : ");
    scanf("%s", msg->name);
    printf("Please input password : ");
    scanf("%s", msg->data);

    send(sockfd, msg, sizeof(MSG), 0);
    recv(sockfd, msg, sizeof(MSG), 0);
    printf("Register : %s\n", msg->data);

}
/**************************************************************/

int do_login(int sockfd,MSG *msg)
{
    memset(msg,0,sizeof(msg)); 
    msg -> type = L;
    printf("Please input name : ");
    scanf("%s", msg->name);
    printf("Please input password : ");
    scanf("%s", msg->data);

    send(sockfd, msg, sizeof(MSG), 0);
    recv(sockfd, msg, sizeof(MSG), 0);

    if(msg -> type == F)
    {
        printf("LOGIN:name or password is wrong!!!\n");
        return 0;
    }
    else
    {
        printf("LOGIN: OK\n");
        return 1;
    }
}

/******************************************************************/

int do_query(int sockfd,MSG *msg)
{
    msg -> type = Q;
    while(1)
    {
    printf("Please inputs words : ");
    scanf("%s", msg->data);
    if(strncmp(msg -> data, "#",  2) == 0) 
        break;
    send(sockfd, msg, sizeof(MSG), 0);
    recv(sockfd, msg, sizeof(MSG), 0);
    printf("Explain:   %s\n", msg->data);
    }
}

/****************************************************************/

void do_history(int sockfd, MSG *msg)
{
    msg->type = H;
    send(sockfd, msg, sizeof(MSG), 0);
    while (1)
    {
        recv(sockfd, msg, sizeof(MSG), 0);
        if (msg->data[0] == '\0') break;
        printf("%s\n", msg->data);
    }
}


int main(int argc, char *argv[])
{
    int sockfd, login = 0;
    struct sockaddr_in servaddr;
    MSG msg;
    char clean[64];

    if (argc < 3)
    {
        printf("INPUT ERROR\n", argv[0]);
        exit(-1);
    }

    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Fail to socket");
        exit(-1);
    }

    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]);

    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("Fail to connect");
        exit(-1);
    }

    int n;

front:

    while ( 1 )
    {
        printf("\n");
        printf("************************************\n");
        printf("* 1: Register   2: Login   3: Quit *\n");
        printf("************************************\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, &msg);
            printf("\n");
            break;
        case 2 :
            printf("\n");
            if (do_login(sockfd, &msg) == 1)
            {
                printf("\n");
                goto next;
            }
            printf("\n");
            break;
        case 3 :
            close(sockfd);
            exit(0);
        }
    }

    //the second menu
next:
    while (1)
    {
        printf("****************************************************\n");
        printf("* 1: Query word    2: History word    3: Main menu *\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, &msg);
            printf("\n");
            break;
        case 2 :
            printf("\n");
            do_history(sockfd, &msg);
            printf("\n");
            break;
        case 3 :
            goto front;
        }
    }

    return 0;
}

你可能感兴趣的:(关于C,Linux,网络)