c/c++语言开发共享c/c++ linux 进程间通信系列2,使用UNIX_SOCKET

linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传。 使用stream,server端: c++ include include include include include …


linux 进程间通信系列2,使用unix_socket

1,使用stream,实现进程间通信

2,使用dgram,实现进程间通信

关键点:使用一个临时的文件,进行信息的互传。

  s_un.sun_family = af_unix;   strcpy(s_un.sun_path, "/tmp/afunix_text");

使用stream,server端:

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/un.h>  #define filepath "/tmp/afunix_text"  int main(){   int s0, sock;   sockaddr_un s_un;   sockaddr_un s_un_accept;   socklen_t addrlen;    s0 = socket(af_unix, sock_stream, 0);   if(s0 < 0){     perror("socket");     return 1;   }    s_un.sun_family = af_unix;   strcpy(s_un.sun_path, filepath);    if(bind(s0, (sockaddr*)&s_un, sizeof(s_un)) != 0){     perror("bind");     return 1;   }    if(listen(s0, 5) != 0){     perror("listen");     return 1;   }    addrlen = sizeof(s_un_accept);   sock = accept(s0, (sockaddr*)&s_un_accept, &addrlen);   if(sock < 0){     perror("accept");     return 1;   }    printf("after acceptn");    write(sock, "the msg is send from server", 27);   close(sock);   close(s0);    unlink(filepath);    return 0; } 

使用stream,client端:

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/un.h>  int main(){   int sock;   sockaddr_un s_un;   int n;   char buf[128];    sock = socket(af_unix, sock_stream, 0);   if(sock < 0){     perror("socket");     return 1;   }    s_un.sun_family = af_unix;   strcpy(s_un.sun_path, "/tmp/afunix_text");    if(connect(sock, (sockaddr*)&s_un, sizeof(s_un)) != 0){     perror("connect");     return 1;   }   printf("after connectn");   memset(buf, 0, sizeof(buf));   n = read(sock, buf, sizeof(buf));   if(n < 0){     perror("read");     return 1;   }    printf("%sn", buf);   close(sock);   return 0; } 

使用dgram,发送端:

#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h>  int main(){   int sock;   sockaddr_un addr;   socklen_t addrlen;    sock = socket(af_unix, sock_dgram, 0);   addr.sun_family = af_unix;   strcpy(addr.sun_path, "/tmp/afu_dgram");    int n = sendto(sock, "hellon", 6, 0, (sockaddr*)&addr, sizeof(addr));   printf("send datan");   close(sock);   return 0; } 

github源代码

使用dgram,接收端:

#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h>  int main(){   int sock;   sockaddr_un addr;   socklen_t addrlen;   char buf[1024];   int n;    sock = socket(af_unix, sock_dgram, 0);    addr.sun_family = af_unix;   strcpy(addr.sun_path, "/tmp/afu_dgram");    bind(sock, (sockaddr*)&addr, sizeof(addr));    while(1){     memset(buf, 0, sizeof(buf));     n = recv(sock, buf, sizeof(buf) - 1, 0);     printf("recv:%sn", buf);   }    close(sock);   return 0; } 

github源代码

c/c++ 学习互助qq群:877684253

c/c++ linux 进程间通信系列2,使用UNIX_SOCKET

本人微信:xiaoshitou5854

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/606854.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐