本地套接字:
IPC: pipe、fifo、mmap、信号、本地套接字(domain)---cs 模型
对比网络编程 TCP C/S模型 注意以下几点:
1.int socket(int domain,int type,int protocol);
参数 domain:AF_INET-->AF_UNIX/AF_LOCAL
type:SOCK_STREAM/SOCK_DGRAM 都可以
2.地址结构: sockaddr_in -->sockaddr_un
struct sockaddr_in srv_addr;-->struct sockaddr_un srv_addr;
srv.addr.sin_family = AF_INET;-->srv_addr.sun_family = AF_UNIX
srv.addr.sin_port = htons(88); strcpy(srv_addr.sun_path,"srv.socket")
srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
len = offsetof(struct sockaddr_un,sun_path)+strlen("srv.socket);
bind(fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr))-->bind(fd,(struct sockaddr*)&srv_addr,len)
3.bind()函数调用成功,会创建一个 socket。因此为了保证bind成功,
通常我们在bind之前,可以使用 unlink("srv.socket");
//servir
#include<stdio.h>
#include<stddef.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<ctype.h>
#include<sys/un.h>
#define SERV_ADDR "serv.socket"
void sys_err(const char* str){
perror(str);
exit(1);
}
int main(){
int lfd = 0,cfd=0;
int size;
char buf[BUFSIZ];
struct sockaddr_un serv_addr,clit_addr;
lfd =socket(AF_UNIX,SOCK_STREAM,0);
if(lfd==-1){
sys_err("socket error");
}
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path,SERV_ADDR);
int len = offsetof(struct sockaddr_un,sun_path)+strlen(serv_addr.sun_path);
unlink(SERV_ADDR);
bind(lfd,(struct sockaddr*)&serv_addr,len);
listen(lfd,128);
printf("Accept ....\n");
while(1){
len = sizeof(clit_addr);
cfd = accept(lfd,(struct sockaddr*)&clit_addr,(socklen_t*)&len);
len -= offsetof(struct sockaddr_un,sun_path);//得到文件名长度
clit_addr.sun_path[len]= '\0';//确保打印时没有乱码出现
printf("client bind filename: %s\n",clit_addr.sun_path);
while((size=read(cfd,buf,sizeof(buf)))>0){
for(int i=0;i<size;++i)
buf[i]=toupper(buf[i]);
write(cfd,buf,size);
}
close(lfd);
}
close(cfd);
return 0;
}
======================================================================
//client
#include<stdio.h>
#include<stdlib.h>
#include<stddef.h>
#include<sys/un.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<ctype.h>
#define SERV_ADDR "serv.socket"
#define CLIT_ADDR "clit.socket"
void sys_err(const char* str){
perror(str);
exit(1);
}
int main(){
int cfd,len;
char buf[BUFSIZ];
cfd = socket(AF_UNIX,SOCK_STREAM,0);
if(cfd==-1){
sys_err("socket error");
}
struct sockaddr_un serv_addr,clit_addr; //服务器地址结构
clit_addr.sun_family = AF_UNIX;
strcpy(clit_addr.sun_path,CLIT_ADDR);
len = offsetof(struct sockaddr_un,sun_path)+strlen(clit_addr.sun_path);
unlink(CLIT_ADDR);
bind(cfd,(struct sockaddr*)&clit_addr,len);//客户端不能依赖自动绑定
///////////////////////////////////////////////////////////
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path,SERV_ADDR);
len = offsetof(struct sockaddr_un,sun_path)+strlen(serv_addr.sun_path);
connect(cfd,(struct sockaddr*)&serv_addr,len);
while(fgets(buf,sizeof(buf),stdin)!=NULL){
write(cfd,buf,strlen(buf));
len = read (cfd,buf,sizeof(buf));
write(STDOUT_FILENO,buf,len);
}
close(cfd);
return 0;
}
我喜欢的人很优秀 发布了78 篇原创文章 · 获赞 10 · 访问量 3801 私信 关注