在线词典(服务器,客服端,Makefile)

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值