网络编程-在线英英词典项目

在线英英词典
项目功能描述:
用户注册和登录验证:服务器端将用户信息和历史记录保存在数据库中,客户端填写用户名和密码,服务器端在数据库中查找、匹配、返回结果。
单词在线翻译:根据客户端输入的单词在字典文件中搜索,
历史记录查询:
项目分析:
项目流程:
定义数据库中表的结构;
定义消息结构体;
分析服务器和客户端流程;
编码实现。
第一步:
先在数据库中建立两个表:usr和record
在这里插入图片描述usr表是用户账号信息表
record表是用户查询单词记录表
还有词典库:dict.txt
代码:
服务器:
dicts.h

/*===============================================
*   文件名称:dicts.h
*   创 建 者:     
*   创建日期:2022年09月18日
*   描    述:
================================================*/
 #ifndef _DICTS_H_
 #define _DICTS_H_
  #define N 64

  #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[512];
  }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);
#endif

dicts.c

/*===============================================
*   文件名称:dicts.c
*   创 建 者:     
*   创建日期:2022年09月18日
*   描    述:
================================================*/
#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 <time.h>
#include <sqlite3.h>
#include <signal.h>
#include <strings.h>
#include "dicts.h"

//服务器函数定义

int do_client(int acceptfd,sqlite3 *db)
{
   
    MSG msg;
    while(recv(acceptfd,&msg,sizeof(msg),0)>0)
    {
   
        printf("type:%d\n",msg.type);
        switch(msg.type)
        {
   
            case R:
                do_register(acceptfd,&msg,db);
                break;
            case L:
                do_login(acceptfd,&msg,db);
                break;
            case Q:
                do_query(acceptfd,&msg,db);
                break;
            case H:
                do_history(acceptfd,&msg,db);
                break;
            default:
                printf("Invalid data msg\n");
        }

    }
    printf("client exit.\n");
    close (acceptfd);
    exit(0);
    return 0;
}



void do_register(int acceptfd,MSG *msg,sqlite3*db)
{
   
    char *errmsg;
    char sql[1024]={
   0};

    //char **resultp;
    //int nrow,ncolumn;
    memset (sql,0,sizeof(sql));
    sprintf(sql,"insert into usr values('%s','%s')",msg->name,msg->data);
    printf("%s\n",sql);
  
    if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    {
   
        printf("%s\n",errmsg);
        strcpy(msg->data,"usr name already exist.");
    }

    else
    {
   
        printf("client register ok!\n");
        strcpy(msg->data,"OK!");
    }

    if(send(acceptfd,msg,sizeof(MSG),0)<0)
    {
   
        perror("send");
        return ;
    }
    return ;

}
int  do_login(int acceptfd,MSG *msg,sqlite3 *db)
{
   
    char sql[1024]={
   0};
    char *errmsg;
    int nrow;
    int ncloumn;
    char**resultp;
    memset(sql,0,sizeof(sql));
    sprintf(sql,"select * from usr where name=
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项目-在线词典源码(源码).zip安卓app开发项
在线词典项目描述: 版本号:v1.1 升级描述:1. 同时支持管理员(用户名:root,密码:1)和普通用户 2. 管理员可以查询所有用户的使用记录 服务器: 1. 支持并发服务器,每有一个客户端connect成功后,提示某某客户端已连接并打印客户端的ip和端口号。 2. 服务器程序可在任意IP地址上运行,并且允许IP地址快速重用 3. 接收到客户端的信息后,可以执行相应的操作:注册,登陆,退出 注册:接收到注册新用户指令后,可以创建sqlite3数据库,将用户名和密码存储到数据库的user表中(用户名name为primary key)。 登陆:接收到登陆命令,可以查询客户端输入的用户名和密码数据库中有没有,有的话跳到下一个菜单(查询单词,历史纪录,退出),没有的话打印错误信息。 查询单词:用户输入单词,服务器从dict.txt文件中遍历有无该单词,有的话打印释义,没有的话打印错误信息,并将用(户名,时间,单词)存储到数据库的history_record表中。(‘#’返回上一级菜单) 历史纪录:用户选择历史记录查询,服务器数据库的history_record表查询相同name的记录,每查询到一条,调用一次callback将信息发送到客户端,查询完毕后通知客户端。 退出:客户端退出,服务器打印"client exit!" 退出:客户端退出,服务器打印"client exit!" 客户端: 1. 客户端输入./client 192.168.23.128(服务器IP地址) 10000(端口号),参数格式不对或少报错,端口号不能小于5000,小于5000报错 2. 客户端支持注册,登陆,退出 注册:向服务器发送用户名和密码,接收服务器返回来的信息,注册成功/当前用户已存在 登陆:用户输入用户名和密码,客服端将用户名和密码发送给服务器,接收服务器返回的信息,如果OK,打印Login OK! 进入下一菜单(查询单词,历史纪录,退出),否则打印错误信息 查询单词:用户输入单词,客户端将单词发送给服务器服务器将释义返回给客户端,客户端将释义打印出来 历史纪录:用户选择历史记录查询,客户端将信息发送给服务器服务器循环把该用户的历史查询记录发送给客户端,客户端循环将其打印出来。 退出:客户端关闭套接字后结束进程 退出:客户端关闭套接字后结束进程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值