server:
/*================================================================
*
- 文件名称:在线词典服务器v1.0.c
- 创 建 者:xiaowang
- 创建日期:2020年04月28日
- 描 述:
================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sqlite3.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include <unistd.h>
#define N 32
#define R 1 // user - register
#define L 2 // user - login
#define Q 3 // user - query
#define H 4 // user - history
#define DATABASE “my.db”
// 定义通信双方的信息结构体
typedef struct {
int type;
char name[N];
char data[256];
}attribute((packed)) MSG;
int do_client(int acceptfd, sqlite3 *db);
void do_register(int acceptfd, MSG *msg, sqlite3 *db);
int do_login(int acceptfd, MSG *msg, sqlite3 db);
int do_query(int acceptfd, MSG msg, sqlite3 db);
int do_history(int acceptfd, MSG msg, sqlite3 db);
int history_callback(void arg,int f_num,char f_value,char f_name);
int do_searchword(int acceptfd, MSG *msg, char word[]);
int get_date(char *date);
void sig_child_handle(int signo)
{
if(SIGCHLD == signo)
waitpid(-1, NULL, WNOHANG);
}
//0主函数
int main(int argc, const char *argv[])
{
int sockfd;
int n;
MSG msg;
sqlite3 *db;
int acceptfd;
pid_t pid;
char *errmsg;
if(argc != 3)
{
printf("Usage:%s serverip port.\n", argv[0]);
return -1;
}
/*************************************************/
//打开数据库
if(sqlite3_open(DATABASE, &db) != SQLITE_OK)
{
printf("%s\n", sqlite3_errmsg(db));
return -1;
}
else
printf("open DATABASE success.\n");
if(sqlite3_exec(db,
"create table if not exists usr(name text primary key,pass text);",
//primary key主关键字只能有一个
NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
printf("Create or open usr table success.\n");
if(sqlite3_exec(db,
"create table if not exists record(name text ,data text,word text);",
NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
printf("Create or open record table success.\n");
/*************************************************/
if((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0)
{
perror("fail to socket.\n");
return -1;
}
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
server

最低0.47元/天 解锁文章
3043

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



