UNIX域套接字
socket同样可以用于本地通信
创建套接字时使用本地协议PF_UNIX(或PF_LOCAL)PF_LOCAL
分为流式套接字和用户数据报套接字
和其他进程间通信方式相比使用方便。效率更高
用于前后台进程通信
本地地址结构:
struct sockaddr_un
{
sa_family_t sun_family;
char sun_path[108];
};
填充地址结构
struct sockaddr_un myaddr;
bzero(&myaddr,sizeof(myaddr));
myaddr.sun_family=PF_UNIX;
strcpy(myaddr.sun_path,"mysocket");
UNIX域流式套接字
服务器端:
socket(PF_UNIX,SOCK_STREAM,0);
bind(本地地址);
listen()
aceept()
recv/send
客户端
socket(PF_UNIX,SOCK_STREAM,0);
bind(本地地址);
connect();
recv/send();
server.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/un.h> #define err_log(errlog) do{perror(errlog); exit(1);}while(0) #define N 128 int main(int argc, const char *argv[]) { int sockfd; int confd; struct sockaddr_un serveraddr, clientaddr; char buf[N] = {}; if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { err_log("fail to socket"); } printf("sockfd = %d ", sockfd); serveraddr.sun_family = AF_UNIX; strcpy(serveraddr.sun_path, "mysocket"); if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) { err_log("fail to bind"); } if(listen(sockfd, 10) < 0) { err_log("fail to listen"); } socklen_t addrlen = sizeof(struct sockaddr); if((confd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen)) < 0) { err_log("fail to accept"); } while(1) { if(recv(confd, buf, N, 0) < 0) { err_log("fail to recv"); } printf("From client:%s ", buf); strcpy(buf, " Message from server"); if(send(confd, buf, N, 0) < 0) { err_log("fail to send"); } } close(sockfd); return 0; }
client.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/un.h> #define err_log(errlog) do{perror(errlog); exit(1);}while(0) #define N 128 // ./client 192.168.0.196 10000 int main(int argc, const char *argv[]) { int sockfd; int confd; struct sockaddr_un serveraddr, clientaddr; char buf[N] = {0}; if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { err_log("fail to socket"); } printf("sockfd = %d ", sockfd); serveraddr.sun_family = AF_UNIX; strcpy(serveraddr.sun_path, "mysocket"); if(connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) { err_log("fail to connect"); } while(1) { printf("Input >"); fgets(buf, N, stdin); buf[strlen(buf)-1] = '