• 电子词典的客户端程序


    整个电子词典是分块做的:包含的Dic_Server.c,Dic_Client.c,db.c,query.c,xprtcl.c,dict.h,xprtcl.h,dict.txt(单词文件)

    下面是Dic_Client.c代码:

    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sqlite3.h>

    #include "dict.h"
    #include "xprtcl.h"

    #define MY_PORT 8888
    #define DATABASE "dict.db"
    #define T_USERS "users" /* table name for login users */
    #define T_HISTORY "history" /* table name for query history */
    #define MAXLEN_SQL 256

    sqlite3 *g_db = NULL;
    char quit_name[16];
    int sockfd;

    void do_login();
    void do_query();
    void err_quit();
    void do_register();

    void re_register()
    {
    printf("Registration error, please check the user name and password ");
    printf("Now need to register again? y/n ");
    char regist;
    scanf("%c",&regist);
    getchar();
    if(regist == 'y')
    {
    do_register();
    }
    else
    {
    err_quit();
    printf("Goodbye! ");
    exit(0);
    }

    }

    void quit()
    {
    int i;
    xprotocol_t packet;
    packet.hdr.cmd_type = REQ_QUIT;
    for(i = 0; i < 16; i++)
    {
    packet.hdr.usrname[i] = quit_name[i];
    }
    printf("%s have withdrawn from the account ",packet.hdr.usrname);

    /* send reply back to the client */
    if (xp_send(sockfd, &packet) < 0) {
    dprintf("xp_send error");
    }

    return;
    }

    void err_quit()
    {
    int i;
    xprotocol_t packet;
    packet.hdr.cmd_type = REQ_ERR_QUIT;
    for(i = 0; i < 16; i++)
    {
    packet.hdr.usrname[i] = quit_name[i];
    }

    /* send reply back to the client */
    if (xp_send(sockfd, &packet) < 0) {
    dprintf("xp_send error");
    }

    printf("quit client----> ");
    return;
    }


    void do_register()
    {
    xprotocol_t packet;
    int i;
    char *buf = NULL;
    memset(&packet, 0x00, sizeof(xprotocol_t));
    packet.hdr.cmd_type = REQ_Register;

    /*从标准输入读入注册名*/
    printf("User name of the valid characters for Arabic numerals and English letters,
    the shortest one character, the longest 15 characters ");
    printf(" ");
    printf("input user name : ");

    if (NULL == fgets(packet.hdr.usrname, MAXLEN_USRNAME + 1, stdin))
    {
    perror("fgets error");
    return;
    }
    /* cut off the LF added by fgets() */
    packet.hdr.usrname[strlen(packet.hdr.usrname) - 1] = 0x00;
    printf(" ");
    buf = packet.hdr.usrname;
    if((strlen(buf) > 15))
    {
    re_register();
    }
    for(i = 0; i < strlen(buf); i++)
    {

    if((buf[i] <='z' && buf[i] >='a')|| (buf[i] <='Z' && buf[i] >='A')
    ||(buf[i]>=48&&buf[i]<=57));
    else
    {
    re_register();
    }
    }

    printf(" ");
    /*从标准输入读入密码*/
    printf("Password characters for Arabic numerals and English letters,
    other letters, at least 8, 15 longest ");
    printf(" ");
    printf("input password : ");
    if (NULL == fgets(packet.data.req_register.passwd, MAXLEN_PASSWD + 1, stdin))
    {
    perror("fgets error");
    return;
    }
    /* cut off the LF added by fgets() */
    packet.data.req_register.passwd[
    strlen(packet.data.req_register.passwd) - 1] = 0x00;

    buf = packet.data.req_register.passwd;
    if((strlen(buf) < 8) || (strlen(buf) > 15))
    {
    re_register();
    }
    for(i = 0; i < strlen(buf); i++)
    {

    if((buf[i] <='z' && buf[i] >='a')|| (buf[i] <='Z' && buf[i] >='A')
    ||(buf[i]>=48&&buf[i]<=57));
    else
    {
    re_register();
    }
    }

    /* upon till now, the register request packet is completed */

    /* send the register request packet to the target server */
    if (xp_send(sockfd, &packet) < 0)
    {
    perror("xp_send failed");
    return;
    }

    /* wait and receive register reply packet from the target server */
    if (xp_recv(sockfd, &packet) <= 0)
    {
    perror("read failed");
    return;
    }

    /* checking the reply content from the target .... */
    if (RPL_Register != packet.hdr.cmd_type)
    {
    printf("wrong reply! ");
    return;
    }

    if (RC_SUCCESS != packet.hdr.ret_code)
    {
    printf("registered failed because Account registered ");
    printf("Now need to register again ? y/n ");

    char regist;
    scanf("%c",&regist);
    getchar();
    if(regist == 'y')
    {
    do_register();
    }
    else
    {
    err_quit();
    printf("Goodbye! ");
    exit(0);
    }
    }
    else
    {
    char login;
    printf(" ");
    printf("registered successfully ");
    printf("Now need to login ? y/n ");
    scanf("%c",&login);
    getchar();
    if(login == 'y')
    {
    do_login();
    }
    else
    {
    quit();
    printf("Goodbye! ");
    exit(0);
    }

    }

    }

    void do_query()
    {
    int i;
    xprotocol_t packet;
    memset(&packet, 0x00, sizeof(xprotocol_t));
    for(i = 0; i < 16; i++)
    {
    packet.hdr.usrname[i] = quit_name[i];
    }

    packet.hdr.cmd_type = REQ_Query;

    /*从标准输入读入查寻单词*/
    printf("input query dictionary : ");
    if (NULL == fgets(packet.word_data.req_query.word, MAXLEN_PASSWD + 1, stdin))
    {
    perror("fgets error");
    return;
    }

    packet.word_data.req_query.word[
    strlen(packet.word_data.req_query.word) - 1] = 0x00;

    if (xp_send(sockfd, &packet) < 0)
    {
    perror("xp_send failed");
    return;
    }

    /* wait and receive register reply packet from the target server */
    if (xp_recv(sockfd, &packet) <= 0)
    {
    perror("read failed");
    return;
    }
    /* checking the reply content from the target .... */
    if (RPL_Query != packet.hdr.cmd_type)
    {
    printf("wrong reply! ");
    return;
    }

    if (RC_SUCCESS != packet.hdr.ret_code)
    {
    printf(" ");
    printf("query failed ");
    printf("Input word is wrong, please input again ");

    printf("Are you query again ? y/n ");
    char query;
    scanf("%c",&query);
    getchar();
    if(query == 'y')
    {
    do_query();
    }
    else
    {
    quit();
    printf("Goodbye! ");
    exit(0);
    }
    }
    else
    {
    printf(" ");
    printf("query successfully ");
    printf("Words to explain the following: ");
    printf("%s ",packet.data.rpl_query.text);

    printf("Are you query again ? y/n ");
    char query;
    scanf("%c",&query);
    getchar();
    if(query == 'y')
    {
    do_query();
    }
    else
    {
    quit();
    printf("Goodbye! ");
    exit(0);
    }
    }


    }

    void do_history_query()
    {
    char sqlstr[MAXLEN_SQL];
    xprotocol_t packet;
    sqlite3 *g_db;
    char *errmsg;
    int result, i;

    for(i = 0; i < 16; i++)
    {
    packet.hdr.usrname[i] = quit_name[i];
    }

    result = sqlite3_open(DATABASE, &g_db);
    if (result != SQLITE_OK)
    {
    if (NULL != g_db)
    {
    fprintf(stderr, "error: open database: %s ",
    sqlite3_errmsg(g_db));
    }
    else
    {
    fprintf(stderr, "error: failed to allocate memory! ");
    }
    exit(-1);
    }


    if (snprintf(sqlstr, MAXLEN_SQL, "select word,time from history where usrname='%s'",
    packet.hdr.usrname)< 0)
    {
    perror("snprintf error");
    exit(-1);
    }

    int show_sql_result(void *arg, int n_column, char **column_value, char **column_name)
    {
    printf("%-17s%s ",column_value[0],column_value[1]);
    return 0;
    }
    if(SQLITE_OK != sqlite3_exec(g_db,sqlstr,show_sql_result,NULL,&errmsg))
    {
    fprintf(stderr,"sqlite3_exec(%s): %s. ",sqlstr,errmsg);
    sqlite3_free(errmsg);
    return ;
    }
    printf(" ");
    printf("Are you query or quit now? 1/2 ");
    printf("1.query 2.quit ");
    printf("please input your select:");
    int select;
    scanf("%d",&select);
    getchar();
    if(select== 1)
    {
    do_query();

    }
    else
    {
    quit();
    printf("Goodbye! ");
    exit(0);
    }

    }


    void do_login()
    {
    xprotocol_t packet;
    int i;

    memset(&packet, 0x00, sizeof(xprotocol_t));
    packet.hdr.cmd_type = REQ_Login;

    /*从标准输入读入注册名*/
    printf("input name : ");
    if (NULL == fgets(packet.hdr.usrname, MAXLEN_USRNAME + 1, stdin))
    {
    perror("fgets error");
    return;
    }
    /* cut off the LF added by fgets() */
    packet.hdr.usrname[strlen(packet.hdr.usrname) - 1] = 0x00;
    for(i = 0; i < 16; i++)
    {
    quit_name[i] = packet.hdr.usrname[i];
    }
    /*从标准输入读入密码*/
    printf("input password : ");
    if (NULL == fgets(packet.data.req_login.passwd, MAXLEN_PASSWD + 1, stdin))
    {
    perror("fgets error");
    return;
    }

    /* cut off the LF added by fgets() */
    packet.data.req_login.passwd[
    strlen(packet.data.req_login.passwd) - 1] = 0x00;

    /* upon till now, the register request packet is completed */

    /* send the register request packet to the target server */
    if (xp_send(sockfd, &packet) < 0)
    {
    perror("xp_send failed");
    return;
    }

    /* wait and receive register reply packet from the target server */

    if (xp_recv(sockfd, &packet) <= 0)
    {
    perror("read failed");
    return;
    }

    /* checking the reply content from the target .... */
    if (RPL_Login != packet.hdr.cmd_type)
    {
    printf("wrong reply! ");
    return;
    }

    if (RC_LOGINED == packet.hdr.ret_code)
    {
    printf(" ");
    printf("Accounts have been online ");
    printf("login failed ");
    printf("Are you login again ? y/n ");
    char login;
    scanf("%c",&login);
    getchar();
    if(login == 'y')
    {
    do_login();
    }
    else
    {
    err_quit();
    printf("Goodbye! ");
    exit(0);
    }
    }

    if (RC_EREGISTER == packet.hdr.ret_code)
    {
    printf(" ");
    printf("login failed ");
    printf("Are you login again ? y/n ");
    char login;
    scanf("%c",&login);
    getchar();
    if(login == 'y')
    {
    do_login();
    }
    else
    {
    err_quit();
    printf("Goodbye! ");
    exit(0);
    }
    }
    else if (RC_SUCCESS == packet.hdr.ret_code)
    {
    printf(" ");
    printf("login successfully ");
    printf("Are you query or history ? 1/2 ");
    printf("1.query 2.history_query 3.quit ");
    printf("please input your select:");
    int query;
    scanf("%d",&query);
    getchar();
    printf(" ");
    if(query == 1)
    {
    do_query();

    }
    else if(query == 2)
    {
    do_history_query();
    }
    else
    {
    quit();
    printf("Goodbye! ");
    exit(0);
    }

    }

    }

    int main(int argc, char *argv[])
    {
    int cmd;
    //char buffer[] = "do_register";
    struct sockaddr_in server_addr;
    struct hostent *host; /* 使用hostname查询host 名字 */
    if(argc!=2)
    {
    fprintf(stderr,"Usage:%s hostname a ",argv[0]);
    exit(1);
    }
    if((host=gethostbyname(argv[1]))==NULL)
    {
    fprintf(stderr,"Gethostname error ");
    exit(1);
    }
    /* 客户程序开始建立 sockfd描述符 */
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) // AF_INET:Internet;SOCK_STREAM:TCP
    {
    fprintf(stderr,"Socket Error:%sa ",strerror(errno));
    exit(1);
    }
    /* 客户程序填充服务端的资料 */
    bzero(&server_addr,sizeof(server_addr)); // 初始化,置0
    server_addr.sin_family=AF_INET; // IPV4
    server_addr.sin_port=htons(MY_PORT);
    server_addr.sin_addr=*((struct in_addr *)host->h_addr);
    if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
    {
    fprintf(stderr,"Connect Error:%sa ",strerror(errno));
    exit(1);
    }
    /* 连接成功了 */

    printf("connect() done ");
    while(1)
    {
    printf("********************************************** ");
    printf("* 1: register 2: login * ");
    printf("* 3: query 4: history_query * ");
    printf("* 5: exit * ");
    printf("********************************************** ");
    printf(" ");
    printf("please choose : ");

    if (scanf("%d", &cmd) == 0 )
    {
    printf("scanf error");
    continue;
    }
    getchar();
    switch (cmd)
    {
    case 1:
    printf(" ");
    do_register();
    printf(" ");
    break;

    case 2:
    printf(" ");
    printf("**************welcome_login************* ");
    do_login();
    printf(" ");
    break;

    case 3:
    printf("******Please login first****** ");
    printf("Are you login now ? y/n ");
    char login1;
    scanf("%c",&login1);
    getchar();
    if(login1 == 'y')
    {
    do_login();
    }
    else
    {
    err_quit();
    }
    printf(" ");
    break;
    case 4:
    printf("******Please login first****** ");
    printf("Are you login now ? y/n ");
    char login2;
    scanf("%c",&login2);
    getchar();
    if(login2 == 'y')
    {
    do_login();
    }
    else
    {
    err_quit();
    }
    printf(" ");
    break;

    case 5:
    printf("Goodbye! ");
    exit(0);
    default: break;
    }
    }

    exit(0);
    }

  • 相关阅读:
    关于大文件下载
    关于小文件下载
    小文件下载
    AppStore 中的app怎么样生成二维码,来提供下载
    重学STM32---(十)之CAN通信(二)
    重学STM32---(九)之CAN通信(一)
    将博客搬至CSDN
    绑定socket描述符到一个网络设备
    通用 Makefile(及makefile中的notdir,wildcard和patsubst)
    vsftpd 编译安装 及 隐藏版本号
  • 原文地址:https://www.cnblogs.com/cnlg/p/4366909.html
Copyright © 2020-2023  润新知