欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux在线英英词典项目开发

程序员文章站 2022-05-04 14:08:10
...

Linux在线英英词典项目完整版代码

创建sqlite3数据库:
输入命令:
1、 sqlite3 my.db
2、 sqlite3
3、 create table usr(name char,data char);
4、 create table record(name char, date char,word char);
5、下载词典:dict.txt放在当前目录下(链接:https://pan.baidu.com/s/1miL2h2w);

6.编写Makefile:(gcc是按键Tab的间距)

all:
gcc server.c -lsqlite3 -o server
gcc client.c -o client

sever.c

#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 DATABASE “my.db”
typedef struct
{
int type;
char name[20];
char data[256];

}MSG;

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

sprintf(sqlstr, "insert into usr values ('%s', '%s')", msg->name, msg->data);
printf("%s\n", sqlstr);
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK) 
{
    sqlite3_free(errmsg);
    sprintf(msg->data, "user %s already exist!!!", msg->name);


}
else
{
    strncpy(msg->data, "OK", 256);    


}
send(connectfd, msg, sizeof(MSG), 0);
return;

}
void do_login(int connectfd, MSG *msg, sqlite3 *db)
{
char sqlstr[128];
char *errmsg, **result;
int nrow, ncolumn;
sprintf(sqlstr, “select * from usr where name = ‘%s’ and data = ‘%s’”, msg->name, msg->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(msg->data, "name or password is wrong!!!", 256);


}
else                  
{
    strncpy(msg->data, "OK", 256);


}
send(connectfd, msg, sizeof(MSG), 0);
sqlite3_free_table(result);
return;

}
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);
return;

}
int do_searchword(int connectfd, MSG *msg)
{
FILE *fp;
char s[300];
char *p;
if ((fp = fopen(“dict.txt”, “r”)) == NULL)
{
strcpy(msg->data, “dict on server can’t be opened ????”);
send(connectfd, msg, sizeof(MSG), 0);
}
printf(“query word is %s\n”, msg->data);
while (fgets(s, 300, fp) != NULL)
{
if (0 == strncmp(msg->data,s,strlen(msg->data)) )
{
p = s + strlen(msg->data);
while(0 == strncmp(p," ",1) ) p++;
strcpy(msg->data,p);
send(connectfd,msg,sizeof(MSG),0);
fclose(fp);
return 1;
}

}
fclose(fp);
return 0;                

}
void do_query(int connectfd, MSG *msg, sqlite3 *db)
{
char sqlstr[128], *errmsg;
int found = 0;
char date[64], word[64];
strcpy(word, msg->data);
found = do_searchword(connectfd, msg);
if ( found )
{
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);

    }


}
else                 
{
    strcpy(msg->data, "not found\n");


}
send(connectfd, msg, sizeof(MSG), 0);
return;

}
int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{
int connectfd;
MSG msg;
connectfd = *(int *)arg;
sprintf(msg.data, “%s : %s”, f_value[1], f_value[2]);
send(connectfd, &msg, sizeof(msg), 0);
return 0;

}
void do_history(int connectfd, 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 *)&connectfd, &errmsg) != SQLITE_OK)
{
printf(“error : %s\n”, errmsg);
sqlite3_free(errmsg);

}
msg->data[0] = '\0';               
send(connectfd, msg, sizeof(MSG), 0);


return;

}
void do_client(int connectfd, sqlite3 *db)
{
MSG msg;
while (recv(connectfd, &msg, sizeof(msg), 0) > 0)
{
switch ( msg.type )
{
case 1 :
do_register(connectfd, &msg, db);
break;
case 2 :
do_login(connectfd, &msg, db);
break;
case 3 :
do_query(connectfd, &msg, db);
break;
case 4 :
do_history(connectfd, &msg, db);
break;

    }


}
printf("client quit!!!\n");
exit(0);
return;

}

int main(int argc, char *argv[])
{
int listenfd, connectfd;
struct sockaddr_in server_addr;
pid_t pid;
sqlite3 *db;

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);
}
printf("listenfd=%d\n",listenfd);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(8888);
printf("[Port]=%d\n[IP]=%s\n",htons(server_addr.sin_port),inet_ntoa(server_addr.sin_addr));
if (bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
    perror("fail to bind");
    exit(-1);
}
printf("server bind succeed\n");


if (listen(listenfd, 5) < 0)
{
    perror("fail to listen");
    exit(-1);


}
printf("server listen succeed\n");
signal(SIGCHLD, SIG_IGN);  // 为了不生成僵尸进程
while (1)
{
    if ((connectfd = accept(listenfd, NULL, NULL)) < 0)
    {
        perror("fail to accept");
        exit(-1);
    }

    if ((pid = fork()) < 0)         //多进程
    {
        perror("fail to fork");
        exit(-1);


    }
    if (pid == 0)             //子进程
    {
        do_client(connectfd, db);


    }
    close(connectfd);
}

return 0;

}

client.c

#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 DATABASE “my.db”
typedef struct
{
int type;
char name[20];
char data[256];

}MSG;

void do_register(int socketfd, MSG *msg)
{
msg->type = 1;
printf(“input name : “);
scanf(”%s”, msg->name);
printf(“input password : “);
scanf(”%s”, msg->data);
send(socketfd, msg, sizeof(MSG), 0);
recv(socketfd, msg, sizeof(MSG), 0);
printf(“register : %s\n”, msg->data);
return;

}

int do_login(int socketfd, MSG *msg)
{
msg->type = 2;
printf(“input name : “);
scanf(”%s”, msg->name);
printf(“input password : “);
scanf(”%s”, msg->data);
send(socketfd, msg, sizeof(MSG), 0);
recv(socketfd, msg, sizeof(MSG), 0);
if (strncmp(msg->data, “OK”, 3) == 0)
{
printf(“login : OK\n”);
return 1;

 }
 printf("login : %s\n", msg->data);       
 return 0;                                

}

void do_query(int socketfd, MSG *msg)
{
msg->type = 3;
while (1)
{
printf(“Input:”);
scanf("%s", msg->data);

     if (strcmp(msg->data, "#") == 0) break;  //输入“#”退出
     send(socketfd, msg, sizeof(MSG), 0);
     recv(socketfd, msg, sizeof(MSG), 0);
     printf("%s\n",msg->data);          


 }
 return;

}

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

}

int main(int argc, char *argv[])
{
int socketfd ;
struct sockaddr_in server_addr;
MSG msg;
if ((socketfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror(“fail to socket”);

}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(8888);    
if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
    perror("fail to connect");
    exit(-1);
}
int n;
while (1) 
{
    printf("************************************\n");
    printf("*********** 1:register *************\n");  
    printf("*********** 2: login   *************\n");  
    printf("*********** 3: quit    *************\n");
    printf("************************************\n");
    printf("please choose : ");
    if (scanf("%d", &n) <= 0)
    {
        perror("scanf");
        exit(-1);


    }
    switch (n)
    {
        case 1 :
        do_register(socketfd, &msg);
        break;
        case 2 :
        if (do_login(socketfd, &msg) == 1)
        {
            printf("\n");
            goto next; 
        }
        break;
        case 3 :
        close(socketfd);
        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)
{
perror(“scanf”);
exit(-1);

    }
    switch (n)
    {
        case 1 :
        do_query(socketfd, &msg);
        break;
        case 2 :
        do_history(socketfd, &msg);
        break;
        case 3 :
        close(socketfd);
        exit(0);
    }


}
return 0;

}

转自博客:https://blog.csdn.net/u010661782/article/details/49020843?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase

相关标签: Linux学习 c++