網(wǎng)站首頁 編程語言 正文
一、概述
使用本地socket通訊可以實現(xiàn)進程之間的通訊。
相關函數(shù)描述如下:
int socket(int domain, int type, int protocol); 函數(shù)說明: 創(chuàng)建本地域socket 函數(shù)參數(shù): domain: AF_UNIX or AF_LOCAL type: SOCK_STREAM或者SOCK_DGRAM protocol: 0 表示使用默認協(xié)議 函數(shù)返回值: 成功: 返回文件描述符. 失敗: 返回-1, 并設置errno值. 創(chuàng)建socket成功以后, 會在內核創(chuàng)建緩沖區(qū), 下圖是客戶端和服務端內核緩沖區(qū)示意圖. int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 函數(shù)說明: 綁定套接字 函數(shù)參數(shù): socket: 由socket函數(shù)返回的文件描述符 addr: 本地地址 addlen: 本地地址長度 函數(shù)返回值: 成功: 返回文件描述符. 失敗: 返回-1, 并設置errno值. 需要注意的是: bind函數(shù)會自動創(chuàng)建socket文件, 若在調用bind函數(shù)之前socket文件已經(jīng)存在, 則調用bind會報錯, 可以使用unlink函數(shù)在bind之前先刪除文件. struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX or AF_LOCAL*/ char sun_path[108]; /* pathname */ };
整體使用步驟和網(wǎng)絡通訊的socket是差不多的,如下所示:
tcp的本地套接字服務器流程: 創(chuàng)建套接字 socket(AF_UNIX,SOCK_STREAM,0) 綁定 struct sockaddr_un &強轉 偵聽 listen 獲得新連接 accept 循環(huán)通信 read-write 關閉文件描述符 close tcp本地套接字客戶端流程: 調用socket創(chuàng)建套接字 調用bind函數(shù)將socket文件描述和socket文件進行綁定. 不是必須的, 若無顯示綁定會進行隱式綁定,但服務器不知道誰連接了. 調用connect函數(shù)連接服務端 循環(huán)通信read-write 關閉文件描述符 close
二、代碼示例
1.服務端代碼示例
//本地socket通訊服務端 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/un.h> int main(){ //創(chuàng)建socket int lfd = socket(AF_UNIX,SOCK_STREAM,0); if(lfd<0){ perror("socket error"); return -1; } //刪除socket文件,避免bind失敗 unlink("./server.sock"); //綁定 struct sockaddr_un serv; bzero(&serv,sizeof(serv)); serv.sun_family = AF_UNIX; strcpy(serv.sun_path,"./server.sock"); int ret = bind(lfd,(struct sockaddr *)&serv,sizeof(serv)); if(ret<0){ perror("bind error"); return -1; } //監(jiān)聽 listen(lfd,10); //接收新的鏈接-accept struct sockaddr_un client; bzero(&client,sizeof(client)); socklen_t len = sizeof(client); int cfd = accept(lfd,(struct sockaddr*)&client,&len); if(cfd<0){ perror("accept error"); return -1; } printf("cient->[%s]\n",client.sun_path); int n; char buf[1024]; while(1){ //讀取數(shù)據(jù) memset(buf,0x00,sizeof(buf)); n = read(cfd,buf,sizeof(buf)); if(n<=0){ printf("read error or client close ,n=[%d]\n",n); break; } printf("n=[%d],buf=[%s]\n",n,buf); //發(fā)送數(shù)據(jù) write(cfd,buf,n); } //關閉套接字 close(lfd); return 0; }
2.客戶端代碼示例
//本地socket通信客戶端 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/un.h> int main(){ //創(chuàng)建socket int cfd = socket(AF_UNIX,SOCK_STREAM,0); if(cfd<0){ perror("socket error"); return -1; } //刪除socket文件,避免bind失敗 unlink("./client.sock"); //綁定 struct sockaddr_un client; bzero(&client,sizeof(client)); client.sun_family= AF_UNIX; strcpy(client.sun_path,"./client.sock"); int ret = bind(cfd,(struct sockaddr*)&client,sizeof(client)); if(ret<0){ perror("bind error"); return -1; } struct sockaddr_un serv; bzero(&serv,sizeof(serv)); serv.sun_family = AF_UNIX; strcpy(serv.sun_path,"./server.sock"); ret = connect(cfd,(struct sockaddr*)&serv,sizeof(serv)); if(ret<0){ perror("connect error"); return -1; } int n; char buf[1024]; while(1){ memset(buf,0x00,sizeof(buf)); n = read(STDIN_FILENO,buf,sizeof(buf)); //發(fā)送數(shù)據(jù) write(cfd,buf,n); //讀取數(shù)據(jù) memset(buf,0x00,sizeof(buf)); n = read(cfd,buf,sizeof(buf)); if(n<=0){ printf("read error or client close ,n=[%d]",n); break; } printf("n=[%d],buf=[%s]",n,buf); } close(cfd); return 0; }
原文鏈接:https://www.cnblogs.com/tony-yang-flutter/p/15709973.html
相關推薦
- 2022-04-12 Redis?Server啟動過程的詳細步驟_Redis
- 2022-01-29 寶塔Linux面板的ftp無法使用連接解決方案
- 2022-11-07 Flink?側流輸出源碼示例解析_服務器其它
- 2022-06-21 Android?studio實現(xiàn)兩個界面間的切換_Android
- 2024-07-15 Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SDS)
- 2022-09-21 Python?Ast抽象語法樹的介紹及應用詳解_python
- 2023-01-26 詳解Python手寫數(shù)字識別模型的構建與使用_python
- 2021-12-09 Typora自動編號的具體操作_其它綜合
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支