当前位置:网站首页>Pxxx local socket communication
Pxxx local socket communication
2022-06-13 09:36:00 【LuckyDog0623】
Interprocess communication
PIPE FIFO Implement a simple
mmap Between unrelated processes
The signal Low overhead
domain Best stability
Local socket
1、 File format :pipe 、fifo

2、 Local socket s--> Fake documents

3、 Server process
Create socket :lfd = socket(AF_UNIX, SOCK_STREAM,0)
binding :
struct sockaddr_un serv;
serv.sun_famliy = AF_UNIX;
strcpy(serv.sun_path, "server.socket"); Not yet created
bind(lfd, (struct sockaddr*)&serv, len); If the binding is successful, create server.socket file
Set listening
listen()
Waiting to receive connection request
struct sockaddt_un client;
int cfd = accept(lfd, &client, &len);
signal communication
send()
recv()
disconnect
close()
4、 Client process
① Create socket :fd = socket(AF_UNIX, SOCK_STREAM,0)
② binding :
struct sockaddr_un client;
serv.sun_famliy = AF_UNIX;
strcpy(serv.sun_path, "client.socket"); Not yet created
bind(lfd, (struct sockaddr*)&client, len); If the binding is successful, create client.socket file
③ Connect to server
struct sockaddr_un serv;
serv.sun_family=AF_UNIX;
strcpy(serv.sun_path, "server.socket"); Not yet created
connect(fd, &serv, sizeof(server));
④ signal communication
recv
send
⑤ close
close

Local socket and network socket
The header file :sys/un.h
#define UNIX_PATH_MAX 128
struct sockaddr_un{
__kernel_sa_family_t sun_famliy;
char sun_pathname[UNIX_PATH_MAX];
};
Service programming process :
① Create socket int lfd = sockfd(AF_UNIX, SOCK_STREAM, 0);
② binding :
struct sockaddr_un server;
server.sun_family = AF_LOCAL;
strcpy(server.sun_path, "/tmp/server.socket");
bind(lfd, (struct sockaddr)&server, len);
③ Set listening
listen()
④ Waiting to receive request
int cfd=accetp(lfd, &client, &len);

UNIX Domain Socket Characteristics
1.UNIX Domain Socket Why is it better than TCP/IP Faster communication locally
because UNIX Domain Socket No network protocol stack / No need to pack and unpack / Calculate the check sum / Maintain serial number and response , Just copy application layer data from one process to another
2.UNIX Domain Socket It also provides two types of services: stream oriented and packet oriented API Interface , Be similar to TCP and UDP, But message oriented UNIX Domain Socket It's also reliable , Messages are neither lost nor out of order .
3. full duplex
4. at present UNIX Domain Socket It has become the most widespread IPC Mechanism
4. Commonly used API
1) Used to create something like an anonymous pipe (pipe) Local socket for
int socketpair(int domain, int type, int protocol, int sv[2]);
2) When you no longer need this Unix Domain socket , The file corresponding to the path name should be deleted
int unlink(const char *pathname);
int remove(const char *pathname);
Be careful , If it is an abstract pathname , There is no need to manually delete the corresponding socket file after using the local socket , Because when the local socket is closed , The kernel will automatically delete the abstract name .
3) Get the address information of the local socket
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
client 3.c
client 3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <stddef.h>
int main(void)
{
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
struct sockaddr_un client;
client.sun_family = AF_LOCAL;
strcpy(client.sun_path, "client.sock");
bind(fd, (struct sockaddr *)&client, sizeof(client));
// initialization server Information
struct sockaddr_un serv;
serv.sun_family = AF_LOCAL;
strcpy(serv.sun_path, "serv.sock");
// Connect to server
connect(fd, (struct sockaddr*)&serv, sizeof(serv));
while(1)
{
char buf[1024]={0};
fgets(buf, sizeof(buf), stdin);
send(fd, buf, strlen(buf)+1, 0);
recv(fd, buf, sizeof(buf), 0);
printf("recv buff:%s\n", buf);
}
close(fd)
return 0;
}The service period of 3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <stddef.h>
int main(void)
{
// establish
int lfd = socket(AF_UNIX, SOCK_STREAM, 0);
// binding
struct sockaddr_un serv;
serv.sun_family = AF_UNIX;
strcpy(serv.sun_path, "socket.server");
bind(lfd, (struct sockaddr*)&serv, sizeof(serv));
// monitor
listen(lfd, 36);
// Waiting for connection request
struct sockaddr_un client;
socklen_t len = sizeof(client);
int cfd = accept(lfd, (struct sockaddr*)&client, &len);
// signal communication
while(1)
{
char buf[1024] = {0};
int recvlen = recv(cfd, buf, sizeof(buf), 0);
send(cfd, buf, recvlen, 0);
}
close(cfd);
close(lfd);
return 0;
}
TCP( Stream socket )
【unixstr_cli.c】
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define UNIXSTR_PATH "/tmp/unix.str"
#define LISTENQ 5
#define BUFFER_SIZE 256
int main(void)
{
int sockfd;
struct sockaddr_un servaddr;
sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_LOCAL;
strcpy(servaddr.sun_path, UNIXSTR_PATH);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
char buf[BUFFER_SIZE];
while(1)
{
bzero(buf, sizeof(BUFFER_SIZE));
printf(">> ");
if(fgets(buf, BUFFER_SIZE, stdin) == NULL)
{
break;
}
write(sockfd, buf, strlen(buf));
}
close(sockfd);
return 0;
}
【unixstr_serv.c】
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define UNIXSTR_PATH "/tmp/unix.str"
#define LISTENQ 5
#define BUFFER_SIZE 256
int main(void)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_un servaddr, cliaddr;
if(-1 == (listenfd = socket(AF_LOCAL, SOCK_STREAM, 0)))
{
perror("socket");
exit(EXIT_FAILURE);
}
unlink(UNIXSTR_PATH);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_LOCAL;
strcpy(servaddr.sun_path, UNIXSTR_PATH);
if(-1 == bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)))
{
perror("bind");
exit(EXIT_FAILURE);
}
listen(listenfd, LISTENQ);
len = sizeof(cliaddr);
if(-1 == (connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len)))
{
perror("accept");
exit(EXIT_FAILURE);
}
char buf[BUFFER_SIZE];
while(1)
{
bzero(buf, sizeof(buf));
if(read(connfd, buf, BUFFER_SIZE) == 0) break;
printf("Receive: %s", buf);
}
close(listenfd);
close(connfd);
unlink(UNIXSTR_PATH);
return 0;
}
The server 2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <stddef.h>
#define SERV_ADDR "serv.socket"
#define offsetof(type, member) ((int)&((type *)0)->MEMBER)
int main(void)
{
int lfd, cfd, len, size, i;
struct sockaddr_un, servaddr, clieaddr;
char buff[4096];
lfd = socket(AF_UNIX, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, SERV_ADDR);
/* Calculated members sun_path In the structure sockaddr_un Offset in */
len=offsetof(struct sockaddr_un, sun_path) + strlen(servaddr.sun_path);
unlink(SERV_ADDR);
bind(lfd, (struct sockaddr *)&servaddr, len);
listen(lfd, 20);
printf("Accept ...\n");
while(1)
{
len=sizeof(cliaddr);
cfd=accept(lfd, (struct sockaddr*)&cliaddr, (socklen_t *)&len);
len -=offsetof(struct sockaddr_un, sun_path);
cliaddr.sun_path[len]='\0';
printf("client bind filename %s\n", cliaddr.sun_path);
while((size = read(cfd, buf, sizeof(buf))) > 0)
{
for(i =0; i<size; i++)
buf[i]=toupper(buf[i]);
write(cfd, buf, size);
}
close(cfd);
}
close(lfd);
return 0;
}client 2.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <sys/inet.h>
#include <stddef.h>
#define SERV_ADDR "serv.socket"
#define CLIE_ADDR "clie.socket"
int main(void)
{
int cfd, len;
struct sockaddr_un servaddr,cliaddr;
char buf[4096];
cfd=socket(AF_UNIX, SOCK_STREAM, 0);
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sun_family = AF_UNIX;
strcpy(cliaddr.sun_path, CLIE_ADDR);
/* Why do local sockets need to be bound ?? I don't need it */
len = offsetof(struct sockaddr_un, sun_path) + strlen(cliaddr.sun_path);
unlink(CLIE_ADDR);
bind(cfd, (struct sockaddr *)&cliaddr, len);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family=AF_UNIX;
strcpy(servaddr.sun_path, SERV_ADDR);
len=offsetof(struct sockaddr_un, sun_path) + strlen(servaddr.sun_path);
connet(cfd, (struct sockaddr *)&servaddr, 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;
}
边栏推荐
- 1-4 message passing interface [CSP authentication]
- turtle库的使用数字时钟模拟时钟动态显示
- 攻防世界PWN play 条件竞争漏洞的利用
- turtle库显示系统时间
- Exercise 7-7 string replacement (15 points)
- 批量讀取文件夾下的全部語音文件
- @Value不生效及extend/implement其他类无法注入bean的手动注入
- 马斯克的「元宇宙」梦
- LeetCode 583. Deleting two strings
- Tree and binary tree: storage structure of binary tree
猜你喜欢

C language: dynamic memory management
![[51nod p2106] an odd number of times [bit operation]](/img/f4/600395cd0ecb44235c65805f379bd4.jpg)
[51nod p2106] an odd number of times [bit operation]

Figure: concept of figure

VGA common resolution and calculation method

The turtle library displays the system time

Storage mode of drawings

C language: deep understanding of pointers and arrays

C/s model and P2P model

Heap

acwing 790. The third root of a number (dichotomy)
随机推荐
Collection of articles on virtualization and cloud computing
批量讀取文件夾下的全部語音文件
谨记! 写代码别太自信! 一定要写点关键日志info输出,不然问题都定位不到。
LeetCode 5289. 公平分发饼干(DFS)
acwing 788. Number of pairs in reverse order
[implementation of depth first search]
LeetCode 6097. Match after replacing characters (Dictionary)
Jenkins集成Ldap,Ldap配置错误导致jenkins用户登录失败问题解决
Sequential representation of linear tables
C language: callback function
Class and object -- friend
Alibaba senior experts analyze the standard design of protocol
Tree and binary tree: storage structure of binary tree
C language: deep understanding of character functions and string functions (1)
turtle库的使用数字时钟模拟时钟动态显示
C language: summary of question brushing (1)
Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
acwing 790. The third root of a number (dichotomy)
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
Trees and binary trees: the concept of binary trees