網絡編程7.17

練習:

服務器:

?

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <head.h>
#include <sqlite3.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;enum Type {TYPE_REGIST = 1,TYPE_LOGIN
};enum Operation {SUCCESS = 1,ERR
};typedef struct Pack {enum Type type;enum Operation op;char name[20];char pswd[20];
} pack_t;void client_regist(sqlite3* db, pack_t pack, int client) {char* sql = "insert into stu(name,pswd) values(?,?)";sqlite3_stmt* stmt = NULL;sqlite3_prepare_v2(db, sql, -1, &stmt, 0);sqlite3_bind_text(stmt, 1, pack.name, -1, 0);sqlite3_bind_text(stmt, 2, pack.pswd, -1, 0);int res = sqlite3_step(stmt);if (res != SQLITE_DONE && res != SQLITE_ROW) {pack.op = ERR;} else {pack.op = SUCCESS;}write(client, &pack, sizeof(pack));sqlite3_finalize(stmt); 
}void client_login(sqlite3* db, pack_t pack, int client) {char* sql = "select pswd from stu where name = ?";sqlite3_stmt* stmt = NULL;sqlite3_prepare_v2(db, sql, -1, &stmt, 0);sqlite3_bind_text(stmt, 1, pack.name, -1, 0);int res = sqlite3_step(stmt);if (res == SQLITE_DONE) {pack.op = ERR;} else if (res == SQLITE_ROW) {const char* db_pswd = sqlite3_column_text(stmt, 0);if (strcmp(db_pswd, pack.pswd) == 0) {pack.op = SUCCESS;} else {pack.op = ERR;}}write(client, &pack, sizeof(pack));sqlite3_finalize(stmt); 
}int main(int argc, const char *argv[]) {if (argc < 2) {printf("請輸入端口號\n");return 1;}int port = atoi(argv[1]);sqlite3* db = NULL;sqlite3_open("./stu.db", &db);int server = socket(AF_INET, SOCK_STREAM, 0);struct sockaddr_in addr = {0};addr.sin_family = AF_INET;addr.sin_port = htons(port);addr.sin_addr.s_addr = inet_addr("0.0.0.0");if (bind(server, (struct sockaddr*)&addr, sizeof(addr)) == -1) {perror("bind");return 1;}listen(server, 50);int epfd = epoll_create1(EPOLL_CLOEXEC);int count = 0;struct epoll_event server_ev = {0};server_ev.events = EPOLLIN;server_ev.data.fd = server;epoll_ctl(epfd, EPOLL_CTL_ADD, server, &server_ev);count++;struct epoll_event stdin_ev = {0};stdin_ev.events = EPOLLIN;stdin_ev.data.fd = 0;epoll_ctl(epfd, EPOLL_CTL_ADD, 0, &stdin_ev);count++;while (1) {struct epoll_event list[count];memset(list, 0, sizeof(list));int res = epoll_wait(epfd, list, count, -1);for (int i = 0; i < res; i++) {int fd = list[i].data.fd;if (fd == server) {int client = accept(server, 0, 0);printf("有新客戶端連接\n");struct epoll_event client_ev = {0};client_ev.events = EPOLLIN | EPOLLET;client_ev.data.fd = client;epoll_ctl(epfd, EPOLL_CTL_ADD, client, &client_ev);count++;} else if (fd == 0) {char buf[64] = "";scanf("%s", buf);getchar();printf("鍵盤輸入數據:%s\n", buf);} else {pack_t pack = {0};int client = fd;int res = read(client, &pack, sizeof(pack));if (res == 0) {printf("客戶端斷開連接\n");epoll_ctl(epfd, EPOLL_CTL_DEL, fd, 0);close(fd);break;}if (pack.type == TYPE_REGIST) {client_regist(db, pack, client);} else if (pack.type == TYPE_LOGIN) {client_login(db, pack, client);}}}}sqlite3_close(db);return 0;
}

客戶端

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <head.h>
#include <sqlite3.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;enum Type {TYPE_REGIST = 1,TYPE_LOGIN
};enum Operation {SUCCESS = 1,ERR
};typedef struct Pack {enum Type type;enum Operation op;char name[20];char pswd[20];
} pack_t;void* thread_main(void* arg) {int client = *(int*)arg;while (1) {pack_t pack = {0};int res = read(client, &pack, sizeof(pack));if (res == 0) break;printf("接收到服務器回包\n");if (pack.type == TYPE_REGIST) {if (pack.op == ERR) {printf("該賬號已存在\n");} else {printf("注冊成功\n");}} else if (pack.type == TYPE_LOGIN) {if (pack.op == ERR) {printf("賬號或密碼錯誤\n");} else {printf("登錄成功\n");}}}return NULL;
}int main(int argc, const char *argv[]) {if (argc < 2) {printf("請輸入端口號\n");return 1;}int port = atoi(argv[1]);int client = socket(AF_INET, SOCK_STREAM, 0);struct sockaddr_in addr = {0};addr.sin_family = AF_INET;addr.sin_port = htons(port);addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(client, (struct sockaddr*)&addr, sizeof(addr)) == -1) {perror("connect");return 1;}pthread_t id;pthread_create(&id, 0, thread_main, &client);pthread_detach(id);while (1) {int ch = 0;pack_t pack = {0};printf("1:注冊\n");printf("2:登錄\n");printf("請選擇:");scanf("%d", &ch);getchar();if (ch == 1) {pack.type = TYPE_REGIST;} else if (ch == 2) {pack.type = TYPE_LOGIN;}printf("請輸入賬號:");scanf("%s", pack.name);getchar();printf("請輸入密碼:");scanf("%s", pack.pswd);getchar();write(client, &pack, sizeof(pack));}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/914865.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/914865.shtml
英文地址,請注明出處:http://en.pswp.cn/news/914865.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++ 模板元編程

聽說模板元編程能在編譯時計算出常量&#xff0c;簡單測試下看看&#xff1a;template<int N> struct Summation {static constexpr int value N Summation<N - 1>::value; // 計算 1 2 ... N 的值 };template<> struct Summation<1> { // 遞歸終…

【深度學習】神經網絡過擬合與欠擬合-part5

八、過擬合與欠擬合訓練深層神經網絡時&#xff0c;由于模型參數較多&#xff0c;數據不足的時候容易過擬合&#xff0c;正則化技術就是防止過擬合&#xff0c;提升模型的泛化能力和魯棒性 &#xff08;對新數據表現良好 對異常數據表現良好&#xff09;1、概念1.1過擬合在訓練…

JavaScript的“硬件窺探術”:瀏覽器如何讀取你的設備信息?

JavaScript的“硬件窺探術”&#xff1a;瀏覽器如何讀取你的設備信息&#xff1f; 在Web開發的世界里&#xff0c;JavaScript一直扮演著“幕后魔術師”的角色。從簡單的頁面跳轉到復雜的實時數據處理&#xff0c;它似乎總能用最輕巧的方式解決最棘手的問題。但你是否想過&#…

論安全架構設計(層次)

安全架構設計&#xff08;層次&#xff09; 摘要 2021年4月&#xff0c;我有幸參與了某保險公司的“優車險”項目的建設開發工作&#xff0c;該系統以車險報價、車險投保和報案理賠為核心功能&#xff0c;同時實現了年檢代辦、道路救援、一鍵挪車等增值服務功能。在本項目中&a…

滾珠導軌常見的故障有哪些?

在自動化生產設備、精密機床等領域&#xff0c;滾珠導軌就像是設備平穩運行的 “軌道”&#xff0c;為機械部件的直線運動提供穩準導向。但導軌使用時間長了&#xff0c;難免會出現這樣那樣的故障。滾珠脫落&#xff1a;可能由安裝不當、導軌損壞、超負荷運行、維護不當或惡劣環…

機器視覺的包裝盒絲印應用

在包裝盒絲網印刷領域&#xff0c;隨著消費市場對產品外觀精細化要求的持續提升&#xff0c;傳統印刷工藝面臨多重挑戰&#xff1a;多色套印偏差、曲面基材定位困難、異形結構印刷失真等問題。雙翌光電科技研發的WiseAlign視覺系統&#xff0c;通過高精度視覺對位技術與智能化操…

Redis學習-03重要文件及作用、Redis 命令行客戶端

Redis 重要文件及作用 啟動/停止命令或腳本 /usr/bin/redis-check-aof -> /usr/bin/redis-server /usr/bin/redis-check-rdb -> /usr/bin/redis-server /usr/bin/redis-cli /usr/bin/redis-sentinel -> /usr/bin/redis-server /usr/bin/redis-server /usr/libexec/red…

SVN客戶端(TortoiseSVN)和SVN-VS2022插件(visualsvn)官網下載

SVN服務端官網下載地址&#xff1a;https://sourceforge.net/projects/win32svn/ SVN客戶端工具(TortoiseSVN):https://plan.io/tortoise-svn/ SVN-VS2022插件(visualsvn)官網下載地址&#xff1a;https://www.visualsvn.com/downloads/

990. 等式方程的可滿足性

題目&#xff1a;第一次思考&#xff1a; 經典并查集 實現&#xff1a;class UnionSet{public:vector<int> parent;public:UnionSet(int n) {parent.resize(n);}void init(int n) {for (int i 0; i < n; i) {parent[i] i;}}int find(int x) {if (parent[x] ! x) {pa…

HTML--教程

<!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>菜鳥教程(runoob.com)</title> </head> <body><h1>我的第一個標題</h1><p>我的第一個段落。</p> </body> </html&g…

Leetcode刷題營第二十七題:二叉樹的最大深度

104. 二叉樹的最大深度 給定一個二叉樹 root &#xff0c;返回其最大深度。 二叉樹的 最大深度 是指從根節點到最遠葉子節點的最長路徑上的節點數。 示例 1&#xff1a; 輸入&#xff1a;root [3,9,20,null,null,15,7] 輸出&#xff1a;3示例 2&#xff1a; 輸入&#xff…

微信小程序翻書效果

微信小程序翻書效果 wxml <viewwx:for"{{imgList}}" hidden"{{pagenum > imgList.length - index - 1}}"wx:key"index"class"list-pape" style"{{index imgList.length - pagenum - 1 ? clipPath1 : }}"bindtouchst…

個人IP的塑造方向有哪些?

在內容創業和自媒體發展的浪潮下&#xff0c;個人IP的價值越來越受到重視。個人IP不僅是個人品牌的延伸&#xff0c;更是吸引流量來實現商業變現的重要工具。想要塑造個人IP&#xff0c;需要我們有明確的內容方向和策略&#xff0c;下面就讓我們來簡單了解下。一、展現自我形象…

Spring之【BeanDefinition】

目錄 BeanDefinition接口 代碼片段 作用 BeanDefinitionRegistry接口 代碼片段 作用 RootBeanDefinition實現類 GenericBeanDefinition實現類 BeanDefinition接口 代碼片段 public interface BeanDefinition {// ...void setScope(Nullable String scope);NullableSt…

GD32VW553-IOT LED呼吸燈項目

GD32VW553-IOT LED呼吸燈項目項目簡介這是一個基于GD32VW553-IOT開發板的LED呼吸燈演示項目。通過PWM技術控制LED亮度&#xff0c;實現多種呼吸燈效果&#xff0c;展示RISC-V MCU的PWM功能和實時控制能力。功能特性1. 多種呼吸燈效果正弦波呼吸&#xff1a;自然平滑的呼吸效果線…

Linux(Ubuntu)硬盤使用情況解析(已房子舉例)

文章目錄前言輸出字段詳解1.核心字段說明2.生活化的方式解釋&#xff08;已房間為例&#xff09;3.重點理解①主臥室 (/)??②??臨時房 (tmpfs)??總結前言 “df -h” 是在 Linux ??檢查磁盤空間狀態的最基本、最常用的命令之一??。當發現系統變慢、程序報錯說“磁盤空…

vue中的this.$set

在 Vue 2 中&#xff0c;this.$set 是一個用于響應式地添加新屬性到已有對象的全局 API。它的主要作用是解決 Vue 無法檢測到對象屬性添加或刪除的限制&#xff08;由于 Vue 2 的響應式系統基于 Object.defineProperty 實現&#xff09;。1. 為什么需要 this.$set&#xff1f; …

python爬蟲技術——基礎知識、實戰

參考文獻&#xff1a; Python爬蟲入門(一)&#xff08;適合初學者&#xff09;-CSDN博客 一、常用爬蟲工具包 Scrapy 語言: Python特點: 高效、靈活的爬蟲框架&#xff0c;適合大型爬蟲項目。 BeautifulSoup 語言: Python特點: 用于解析HTML和XML&#xff0c;簡單易用。 Sel…

QT 交叉編譯環境下,嵌入式設備顯示字體大小和QT Creator 桌面顯示不一致問題解決

第一步&#xff1a; 發送fc-list 命令 &#xff0c;查找嵌入式環境下支持的字庫第二步 為每個控件指定字庫文件&#xff0c;以label控件為例&#xff1a;int fontId QFontDatabase::addApplicationFont("/usr/share/fonts/source-han-sans-cn/SourceHanSansCN-Normal.otf…