IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> TCP-电子词典 -> 正文阅读

[网络协议]TCP-电子词典

#ifndef DIC_H
#define DIC_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sqlite3.h>

#define? R? 1?? //? user register
#define? L? 2?? //? user login
#define? Q? 3?? //? query word
#define? H? 4?? //? history record
#define? E? 5?? //? exit

typedef struct
{
?? ?int type;?? ?? ??? ?? ?
?? ?char name[20];?? ?? ?
?? ?char data[1024];? ?
}MSG_t;
typedef struct
{
?? ?int fd;
?? ?char IP[20];
}ThreadParam_t;

#endif

#include "dic.h"
void error_Handling(char* func,int retval);
void break_handling(int retval,char* IP,int fd,MSG_t* pbuf);
void do_register(int newconfd,MSG_t* pbuf);
void do_login(int newconfd,MSG_t* pbuf);
void do_query(int newconfd,MSG_t* pbuf);
void do_history(int newconfd,MSG_t* pbuf);
void do_exit(int newconfd,MSG_t* pbuf);
void recordInQueryLog(MSG_t* pbuf);
void recordInloginLog(MSG_t* pbuf,char* operation);
void createDb_table();
int checkLogout(MSG_t* pbuf,int flag);

int count,row,col,ret;
sqlite3* db = NULL;
char sql[100] = {};
char** result;

void* dealClient(void* p)
{
?? ?pthread_detach(pthread_self());
?? ?int newconfd = (*(ThreadParam_t*)p).fd;

?? ?time_t t = time(NULL);
?? ?printf("??????????? -------------New connection information-------------\n\n");
?? ?printf("????? System time??????????????????????? :? %s\n",ctime(&t));
?? ?printf("????? Establish a connection with the IP :?? %s\n",(*(ThreadParam_t*)p).IP);
?? ?printf("????? New connected sockfd?????????????? :?? %d\n",newconfd);
?? ?printf("????? Processing ThreadID??????????????? :?? %lu\n",pthread_self());
?? ?printf("????? The number of currently connected? :?? %d\n\n\n",count);

?? ?MSG_t message = {0,0,0};
?? ?while(1)
?? ?{
?? ??? ?ssize_t ret_read = read(newconfd,&message,sizeof(MSG_t));
?? ??? ?break_handling(ret_read,(*(ThreadParam_t*)p).IP,newconfd,&message);

?? ??? ?switch(message.type)
?? ??? ?{
?? ??? ?case R:
?? ??? ??? ?do_register(newconfd,&message);
?? ??? ??? ?break;
?? ??? ?case L:
?? ??? ??? ?do_login(newconfd,&message);
?? ??? ??? ?break;
?? ??? ?case Q:
?? ??? ??? ?do_query(newconfd,&message);
?? ??? ??? ?break;
?? ??? ?case H:
?? ??? ??? ?do_history(newconfd,&message);
?? ??? ??? ?break;
?? ??? ?case E:
?? ??? ??? ?do_exit(newconfd,&message);
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}

int main(int argc,char* argv[])
{
?? ?if(argc != 2)
?? ?{
?? ??? ?printf("%s--Port\n",argv[0]);
?? ??? ?exit(1);
?? ?}
?? ?/*创建数据库,用户信息表,历史记录表*/
?? ?createDb_table();


?? ?/*创建套接字——监听套接字*/
?? ?int listenfd = socket(AF_INET,SOCK_STREAM,0);
?? ?error_Handling("socket",listenfd);

?? ?/*绑定监听套接字与主机网络地址信息*/
?? ?int on = 1;
?? ?int ret_set = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
?? ?error_Handling("setsockopt",ret_set);
?? ?struct sockaddr_in serverAddr;
?? ?serverAddr.sin_family = AF_INET;
?? ?serverAddr.sin_port = htons(atoi(argv[1]));
?? ?serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
?? ?int ret_bind = bind(listenfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
?? ?error_Handling("bind",ret_bind);

?? ?/*设置监听队列的大小*/
?? ?int ret_listen = listen(listenfd,10);
?? ?error_Handling("listen",ret_listen);

?? ?/*监听等待连接*/
?? ?struct sockaddr_in buf_addr;
?? ?socklen_t buf_addrlen = sizeof(buf_addr);
?? ?pthread_t tid;
?? ?printf("??????????? -------------The server is running-------------\n\n");
?? ?while(1)
?? ?{
?? ??? ?int newconfd = accept(listenfd,(struct sockaddr*)&buf_addr,&buf_addrlen);
?? ??? ?error_Handling("accept",newconfd);

?? ??? ?ThreadParam_t param;
?? ??? ?param.fd = newconfd;
?? ??? ?strcpy(param.IP,inet_ntoa(buf_addr.sin_addr));
?? ??? ?pthread_create(&tid,NULL,dealClient,&param);

?? ??? ?count++;
?? ?}

}
void createDb_table()
{
?? ?/*创建库*/
?? ?ret = sqlite3_open("usrInfo.db",&db);
?? ?if(ret != 0)
?? ?{
?? ??? ?perror("sqlite3_open");
?? ??? ?exit(1);
?? ?}
?? ?/*创建表*/
?? ?char* sql = "create table accountInfo(usr_name varchar(20),password varchar(20))";
?? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
?? ?if(ret != 0)
?? ?{
?? ??? ?printf("Server_Infomation: %s\n",sqlite3_errmsg(db));
?? ?}
?? ?sql = "create table queryLog(usr_name varchar(20),words varchar(20),time varchar(40))";
?? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
?? ?if(ret != 0)
?? ?{
?? ??? ?printf("Server_Infomation: %s\n",sqlite3_errmsg(db));
?? ?}
?? ?sql = "create table loginLog(usr_name varchar(20),operation varchar(10),time varchar(40))";
?? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
?? ?if(ret != 0)
?? ?{
?? ??? ?printf("Server_Infomation: %s\n",sqlite3_errmsg(db));
?? ?}

?? ?printf("Server_Infomation: The database and tables are ready\n");
}
void do_register(int newconfd,MSG_t* pbuf)
{
?? ?sprintf(sql,"select * from accountInfo where usr_name = '%s'",pbuf->name);
?? ?ret = sqlite3_get_table(db,sql,&result,&row,&col,NULL);
?? ?if(row > 0)
?? ?{
?? ??? ?strcpy(pbuf->data,"Message:账号已存在,重新注册!");
?? ??? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ?}
?? ?else
?? ?{
?? ??? ?bzero(sql,sizeof(sql));
?? ??? ?sprintf(sql,"insert into accountInfo values('%s','%s')",pbuf->name,pbuf->data);
?? ??? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
?? ??? ?strcpy(pbuf->data,"Message:注册成功!");
?? ??? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ?}
}
void do_login(int newconfd,MSG_t* pbuf)
{
?? ?sprintf(sql,"select * from accountInfo where usr_name = '%s' and password = '%s'",pbuf->name,pbuf->data);
?? ?sqlite3_get_table(db,sql,&result,&row,&col,NULL);
?? ?if(row == 0)
?? ?{
?? ??? ?strcpy(pbuf->data,"Message:账号不存在或密码错误!");
?? ??? ?pbuf->type = -1;
?? ??? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ?}
?? ?else if(row == 1)
?? ?{
?? ??? ?if( checkLogout(pbuf,1) )
?? ??? ?{
?? ??? ??? ?strcpy(pbuf->data,"Message:该账号已在另外一台设备上登录!");
?? ??? ??? ?pbuf->type = -1;
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?recordInloginLog(pbuf,"login");?? ?
?? ??? ??? ?strcpy(pbuf->data,"Message:登录成功!");
?? ??? ?}?? ?
?? ??? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ?}
}
void do_query(int newconfd,MSG_t* pbuf)
{
?? ?FILE *fp = fopen("dict.txt","r");
?? ?if(fp == NULL)
?? ?{
?? ??? ?perror("open");
?? ??? ?exit(-1);
?? ?}
?? ?char buf[100] = "\0";
?? ?char word[50] = "\0";
?? ?int flag = 0;
?? ?while(fgets(buf,sizeof(buf),fp)!=NULL)
?? ?{
?? ??? ?bzero(word,sizeof(word));
?? ??? ?int i = 0;
?? ??? ?while(buf[i]>='a'&&buf[i]<='z')
?? ??? ?{
?? ??? ??? ?word[i] = buf[i++];
?? ??? ?}
?? ??? ?if(strcmp(word,pbuf->data)==0)
?? ??? ?{
?? ??? ??? ?flag = 1;
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?if(!flag)
?? ?{
?? ??? ?strcpy(pbuf->data,"Message:Sorry,Word Not found");
?? ?}
?? ?else
?? ?{
?? ??? ?recordInQueryLog(pbuf);
?? ??? ?strcpy(pbuf->data,buf);
?? ?}?? ?
?? ?fclose(fp);
?? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
}
void do_history(int newconfd,MSG_t* pbuf)
{
?? ?sprintf(sql,"select * from queryLog where usr_name = '%s'",pbuf->name);
?? ?ret = sqlite3_get_table(db,sql,&result,&row,&col,NULL);
?? ?int i;
?? ?for(i=0;i<(row+1)*col;i++)
?? ?{
?? ??? ?strcpy(pbuf->data,result[i]);
?? ??? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ?}
?? ?strcpy(pbuf->data,"end");
?? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
}

void recordInloginLog(MSG_t* pbuf,char* operation)
{
?? ?char time_q[40];
?? ?time_t t = time(NULL);
?? ?sprintf(time_q,"%s",ctime(&t));
?? ?sprintf(sql,"insert into loginLog values('%s','%s','%s')",pbuf->name,operation,time_q);
?? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
}

void recordInQueryLog(MSG_t* pbuf)
{
?? ?char time_q[40];
?? ?time_t t = time(NULL);
?? ?sprintf(time_q,"%s",ctime(&t));
?? ?sprintf(sql,"insert into queryLog values('%s','%s','%s')",pbuf->name,pbuf->data,time_q);
?? ?ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
}

int checkLogout(MSG_t* pbuf,int flag)
{
?? ?if(strlen(pbuf->name) == 0) ?
?? ??? ?return 0;
?? ?sprintf(sql,"select * from loginLog where usr_name = '%s'",pbuf->name);
?? ?ret = sqlite3_get_table(db,sql,&result,&row,&col,NULL);

?? ?if(row == 0)?? ???????????? ?
?? ??? ?return 0;

?? ?if(strcmp(result[(row+1)*col-2],"login") == 0)
?? ?{?? ?
?? ??? ?if(flag)
?? ??? ??? ?return 1;
?? ??? ?recordInloginLog(pbuf,"logout");
?? ?}
?? ?return 0;
}
void break_handling(int retval,char* IP,int fd,MSG_t* pbuf)
{
?? ?if(retval<0)
?? ?{
?? ??? ?perror("read");
?? ?}
?? ?if(retval == 0)
?? ?{
?? ??? ?checkLogout(pbuf,0);
?? ??? ?count--;
?? ??? ?time_t t = time(NULL);
?? ??? ?printf("??????????? -------------New Disconnect information-------------\n\n");
?? ??? ?printf("????? System time??????????????????????? :? %s\n",ctime(&t));
?? ??? ?printf("????? Disconnected to IP???????????????? :? %s\n",IP);
?? ??? ?printf("????? The socket is closed?????????????? :? %d\n",fd);
?? ??? ?printf("????? ThreadID is terminated???????????? :? %lu\n",pthread_self());
?? ??? ?printf("????? The number of currently connected? :? %d\n\n",count);

?? ??? ?pthread_exit(NULL);
?? ?}
}
void do_exit(int newconfd,MSG_t* pbuf)
{
?? ?strcpy(pbuf->data,"Message:退出成功!");
?? ?ssize_t ret_write = write(newconfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
}
void error_Handling(char* func,int retval)
{
?? ?if(retval == -1)
?? ?{
?? ??? ?perror(func);
?? ??? ?exit(1);
?? ?}
}

?

#include "dic.h"
void error_Handling(char* func,int retval);
void do_register(int sockfd,MSG_t* pbuf);
int? do_login(int sockfd,MSG_t* pbuf);
void do_query(int sockfd,MSG_t* pbuf);
void do_history(int sockfd,MSG_t* pbuf);
void do_exit(int sockfd,MSG_t* pbuf);
void dictionary(int sockfd,MSG_t* pbuf);
int transform(char* choose);

int main(int argc,char* argv[])
{
?? ?if(argc != 3)
?? ?{
?? ??? ?printf("%s--IP--Port\n",argv[0]);
?? ??? ?exit(1);
?? ?}
?? ?/*创建套接字*/
?? ?int sockfd = socket(AF_INET,SOCK_STREAM,0);
?? ?error_Handling("socket",sockfd);

?? ?/*连接*/
?? ?struct sockaddr_in serverAddr;
?? ?serverAddr.sin_family = AF_INET;
?? ?serverAddr.sin_port = htons(atoi(argv[2]));
?? ?serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
?? ?int ret_connect = connect(sockfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
?? ?error_Handling("connect",ret_connect);
?? ?MSG_t message = {0,0,0};

?? ?while(1)
?? ?{
?? ??? ?system("clear");
?? ??? ?printf("**********************************************************************\n");
?? ??? ?printf("*??????????????????? NewFuture Electronic Dictionary???????????????? *\n");
?? ??? ?printf("*????????????????????????????????????????????? Version :? 0.0.1????? *\n");
?? ??? ?printf("*?????????? 1: register?????????? 2: login??????????? 3: exit??????? *\n");
?? ??? ?printf("**********************************************************************\n");
?? ??? ?printf("please choose a mode: ");
?? ??? ?char choose[100];
?? ??? ?scanf("%s", choose);

?? ??? ?switch (transform(choose))
?? ??? ?{
?? ??? ?case 1:
?? ??? ??? ?do_register(sockfd,&message);
?? ??? ??? ?break;
?? ??? ?case 2:
?? ??? ??? ?if(do_login(sockfd,&message))
?? ??? ??? ?{
?? ??? ??? ??? ?dictionary(sockfd,&message);
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?case 3:
?? ??? ??? ?do_exit(sockfd,&message);
?? ??? ??? ?exit(0);
?? ??? ?default:
?? ??? ??? ?{
?? ??? ??? ??? ?printf("Message:无效的输入\n");?? ??? ??? ??? ?
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?sleep(1);
?? ?}
}
void do_register(int sockfd,MSG_t* pbuf)
{
?? ?pbuf->type = R;
?? ?printf("\nInput User Name???? :");
?? ?scanf("%s",pbuf->name);
?? ?printf("Input? Password???? :");
?? ?scanf("%s",pbuf->data);
?? ?ssize_t ret_write = write(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
?? ?ssize_t ret_read = read(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("read",ret_read);
?? ?puts(pbuf->data);
}
int do_login(int sockfd,MSG_t* pbuf)
{
?? ?pbuf->type = L;
?? ?printf("\nInput User Name???? :");
?? ?scanf("%s",pbuf->name);
?? ?printf("Input? Password???? :");
?? ?scanf("%s",pbuf->data);
?? ?ssize_t ret_write = write(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
?? ?ssize_t ret_read = read(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("read",ret_read);
?? ?puts(pbuf->data);
?? ?sleep(1);
?? ?if(pbuf->type == -1)
?? ?{
?? ??? ?return 0;
?? ?}
?? ?return 1;
}
void dictionary(int sockfd,MSG_t* pbuf)
{
?? ?while(1)
?? ?{
?? ??? ?system("clear");
?? ??? ?printf("**********************************************************************\n");
?? ??? ?printf("*??????????????????? NewFuture Electronic Dictionary???????????????? *\n");
?? ??? ?printf("*????????????????????????????????????????????? Version :? 0.0.1????? *\n");
?? ??? ?printf("*?????? 1: Words_Query???????? 2: History_Reco????????? 3: exit????? *\n");
?? ??? ?printf("**********************************************************************\n");
?? ??? ?printf("please choose a mode: ");
?? ??? ?char choose[1000];
?? ??? ?scanf("%s", choose);
?? ??? ?switch (transform(choose))
?? ??? ?{
?? ??? ?case 1:
?? ??? ??? ?do_query(sockfd,pbuf);?? ?
?? ??? ??? ?break;
?? ??? ?case 2:
?? ??? ??? ?{
?? ??? ??? ??? ?do_history(sockfd,pbuf);
?? ??? ??? ?}?? ??? ??? ??? ?
?? ??? ??? ?break;
?? ??? ?case 3:
?? ??? ??? ?do_exit(sockfd,pbuf);
?? ??? ??? ?exit(0);
?? ??? ?default:
?? ??? ??? ?{
?? ??? ??? ??? ?printf("Message:无效的输入\n");
?? ??? ??? ??? ?sleep(1);
?? ??? ??? ?}
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}

void do_query(int sockfd,MSG_t* pbuf)
{
?? ?pbuf->type = Q;
?? ?while(1)
?? ?{
?? ??? ?printf("Input words (quit by '#') :");
?? ??? ?scanf("%s",pbuf->data);
?? ??? ?if(strcmp(pbuf->data,"#") == 0)
?? ??? ??? ?break;
?? ??? ?ssize_t ret_write = write(sockfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("write",ret_write);
?? ??? ?ssize_t ret_read = read(sockfd,pbuf,sizeof(MSG_t));
?? ??? ?error_Handling("read",ret_read);
?? ??? ?putchar('\n');
?? ??? ?puts(pbuf->data);
?? ?}
}
void do_history(int sockfd,MSG_t* pbuf)
{
?? ?pbuf->type = H;
?? ?ssize_t ret_write = write(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
?? ?ssize_t ret_read;
?? ?int i = 0;
?? ?while( ret_read = read(sockfd,pbuf,sizeof(MSG_t)) )
?? ?{
?? ??? ?error_Handling("read",ret_read);
?? ??? ?if( strcmp(pbuf->data,"end") == 0)
?? ??? ??? ?break;
?? ??? ?printf("%s??? ",pbuf->data);
?? ??? ?i++;
?? ??? ?if(i%3 == 0)
?? ??? ??? ?printf("\n");
?? ?}
?? ?printf("Message:按回车返回上一层\n");
?? ?getchar();
?? ?getchar();
}

int transform(char* choose)
{
?? ?if(!strcmp(choose,"1"))
?? ??? ?return 1;
?? ?else if(!strcmp(choose,"2"))
?? ??? ?return 2;
?? ?else if(!strcmp(choose,"3"))
?? ??? ?return 3;
?? ?else
?? ??? ?return 0;
}

void error_Handling(char* func,int retval)
{
?? ?if(retval == -1)
?? ?{
?? ??? ?perror(func);
?? ??? ?exit(1);
?? ?}
}

void do_exit(int sockfd,MSG_t* pbuf)
{
?? ?pbuf->type = E;
?? ?ssize_t ret_write = write(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("write",ret_write);
?? ?ssize_t ret_read = read(sockfd,pbuf,sizeof(MSG_t));
?? ?error_Handling("read",ret_read);
?? ?puts(pbuf->data);
}

?

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-12-25 11:42:15  更:2022-12-25 11:43:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/27 13:47:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计