嵌入式Linux C语言 应用程序开发之通讯录(网络版)

2019-07-13 02:47发布

该程序为上一个版本的升级版,可以实现TCP网络通信之间的连接
效果图如下
在这里插入图片描述

目录

库函数

#ifndef ADDRESS_H #define ADDRESS_H #define ADDINFO 1000 #define DELINFO 1001 #define UPDATEINFO 1002 #define SEARCHINFO 1003 #define SHOWINFO 1004 #define EXIT 1005 #define DELETEALL 1006 struct ChatInfo { int cmd; char name[32]; char tel[32]; char sex[10]; char loc[15]; }; typedef struct ChatInfo Chat; #endif

服务端

database.c
#include #include #include #include #include "../address.h" void InitDataBase() { sqlite3 *ppdb; //数据库句柄 int ret = sqlite3_open("address.db",&ppdb); if(ret!=SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[128] = {0}; sprintf(sql,"create table if not exists address(name text,tel text,sex text,loc text);"); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); exit(1); } sqlite3_close(ppdb); } void add_info(Chat * c) { sqlite3 *ppdb; int ret = sqlite3_open("address.db",&ppdb); if(ret != SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[120] = {0}; sprintf(sql,"insert into address values('%s','%s','%s','%s');",c->name,c->tel,c->sex,c->loc); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); } sqlite3_close(ppdb); } int sendinfo(void *pare,int columnCount,char **columnValue,char **columnName) { Chat c; int fd=*(int *)pare; strcpy(c.name,columnValue[0]); strcpy(c.tel,columnValue[1]); strcpy(c.sex,columnValue[2]); strcpy(c.loc,columnValue[3]); int ret = send(fd,&c,sizeof(c),0); if(-1 == ret) { perror("send"); } return 0; } void show_info(int fd) { sqlite3 *ppdb; char **result; int ret = sqlite3_open("address.db",&ppdb); if(ret!=SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[128] = "select * from address;"; ret = sqlite3_exec(ppdb,sql,sendinfo,&fd,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); exit(1); } Chat c; strcpy(c.name,"bye"); strcpy(c.tel,"bye"); ret = send(fd,&c,sizeof(c),0); if(-1 == ret) { perror("send"); exit(1); } sqlite3_close(ppdb); } void search_info(int fd,Chat * c) { sqlite3 *ppdb; char *errmsg; int row,column; char **result; Chat a; int ret = sqlite3_open("address.db",&ppdb); if(ret != SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[120] = {0}; sprintf(sql,"select name,tel,sex,loc from address where name='%s';",c->name); /*ret = sqlite3_exec(ppdb,sql,sendinfo,&fd,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); exit(1); }*/ if(SQLITE_OK != sqlite3_get_table(ppdb,sql,&result,&row,&column,&errmsg)) { printf("查询出错:%s! ",errmsg); exit (0); } if(row == 0 || column == 0) { strcpy(a.name,"bye"); strcpy(a.tel,"bye"); } else { strcpy(a.name,result[4]); strcpy(a.tel,result[5]); strcpy(a.sex,result[6]); strcpy(a.loc,result[7]); } ret = send(fd,&a,sizeof(a),0); if(-1 == ret) { perror("send"); } sqlite3_free_table(result); sqlite3_close(ppdb); } void del_info(Chat * c) { sqlite3 *ppdb; int ret = sqlite3_open("address.db",&ppdb); if(ret != SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[120] = {0}; sprintf(sql,"delete from address where name='%s';",c->name); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); } sqlite3_close(ppdb); } void update_info(int fd,Chat * c) { sqlite3 *ppdb; char *errmsg; int row,column; char **result; Chat a; int ret = sqlite3_open("address.db",&ppdb); if(ret != SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[120] = {0}; sprintf(sql,"update address set tel='%s',sex='%s',loc='%s' where name='%s';",c->tel,c->sex,c->loc,c->name); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); } sqlite3_close(ppdb); } void deleteall(int fd) { sqlite3 *ppdb; int ret = sqlite3_open("address.db",&ppdb); if(ret!=SQLITE_OK) { printf("sqlite3_open:%s ",sqlite3_errmsg(ppdb)); exit(1); } char sql[128] = {0}; sprintf(sql,"delete from address;"); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret!=SQLITE_OK) { printf("sqlite3_exec:%s ",sqlite3_errmsg(ppdb)); exit(1); } sqlite3_close(ppdb); }
socket.c
#include #include #include #include #include #include #include #include #include "../address.h" int fd[1024] = {0}; pthread_t tid; //线程号 int InitNet() { struct sockaddr_in server_addr;//客户端信息 //socket int sockfd = socket(AF_INET,SOCK_STREAM,0); if(-1 == sockfd) { perror("socket"); exit(1); } int opt = 1; setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(2222); server_addr.sin_addr.s_addr = INADDR_ANY; //bind int ret = bind(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)); if(-1 == ret) { perror("bind"); exit(1); } //listen ret = listen(sockfd,10); if(-1 == ret) { perror("listen"); exit(1); } return sockfd; } void exit_client(int fd) { printf("客户端%d下线 ",fd); close(fd); pthread_exit((void *)0); } void * client_handler(void *arg) //线程函数 { Chat c; int fd = *(int *)arg; while(1) { int ret = recv(fd,&c,sizeof(c),0); if(-1== ret) { perror("recv"); } switch(c.cmd) { case ADDINFO:add_info(&c); break; case DELINFO:search_info(fd,&c);del_info(&c); break; case UPDATEINFO:search_info(fd,&c);update_info(fd,&c); break; case SEARCHINFO:search_info(fd,&c); break; case SHOWINFO:show_info(fd); break; case EXIT:exit_client(fd); break; case DELETEALL:deleteall(fd); break; default: break; } } } void main_handler(int sockfd) { struct sockaddr_in client_addr; int i = 0; int length = sizeof(client_addr); printf("等待客户端连接 "); while(1) { for(i=0;i<1024;i++) { if(0 == fd[i]) { break; } } fd[i] = accept(sockfd,(struct sockaddr *)&client_addr,&length); printf("接受客户端连接%d ",fd[i]); int ret = pthread_create(&tid,NULL,client_handler,&fd[i]); if(-1 ==ret) { perror("pthread_create"); exit(1); } } }
main.c
#include int main() { int sockfd; sockfd = InitNet(); InitDataBase(); main_handler(sockfd); return 0; }

客户端

address.c
#include #include "../address.h" void menu() { printf(" ************************通讯录功能菜单************************* "); printf(" * * "); printf(" * 1.添加通讯录 2.查看全部联系人 * "); printf(" * 3.查找联系人资料 4.删除联系人 * "); printf(" * 5.修改联系人 6.清空全部联系人 * "); printf(" * 0.退出 * "); printf(" * * "); printf(" *************************************************************** "); printf("请输入0-6使用功能: "); } void main_handler(int fd) { char choice[32]={0}; while(1) { menu(); scanf("%s"