zoukankan      html  css  js  c++  java
  • unix socket share different docker

    unix server 

    root@ubuntu:~/docker# docker run -it --name debian  -v /data1/unix_domain:/data1/unix_domain debian
    root@b1a3bef38722:/# ls
    bin  boot  data1  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  server  srv  sys  tmp  usr  var

    unix client 

    root@ubuntu:~/docker# docker run -it --name debian2  -v /data1/unix_domain:/data1/unix_domain debian
    root@d92cea9a459c:/# ls
    bin  boot  client  data1  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

    docker cp server 和client

    root@ubuntu:~/docker/unix-domain-socket-example# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    b1a3bef38722        debian              "bash"                   23 seconds ago      Up 22 seconds                           debian
    176fb79cdcef        nginx:1.17.4        "nginx -g 'daemon of…"   3 weeks ago         Up 3 weeks          80/tcp              test-vm
    root@ubuntu:~/docker/unix-domain-socket-example# docker cp bin/server debian:/
    root@ubuntu:~/docker/unix-domain-socket-example# docker cp bin/client debian2:/
    root@ubuntu:~/docker/unix-domain-socket-example# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    d92cea9a459c        debian              "bash"                   2 minutes ago       Up 2 minutes                            debian2
    b1a3bef38722        debian              "bash"                   4 minutes ago       Up 4 minutes                            debian
    176fb79cdcef        nginx:1.17.4        "nginx -g 'daemon of…"   3 weeks ago         Up 3 weeks          80/tcp              test-vm
    root@ubuntu:~/docker/unix-domain-socket-example# 

    run server

    root@b1a3bef38722:/# ./server
    recvfrom: iccExchangeAPDU
    root@ubuntu:~/docker/unix-domain-socket-example# docker exec -it debian bash
    root@b1a3bef38722:/# ls /data1/unix_domain/
    server.sock
    root@b1a3bef38722:/# 

    run client 

    root@d92cea9a459c:/# ./client
    sent iccExchangeAPDU
    receive 15 transmit good!
    root@d92cea9a459c:/# 

    宿主机上

    root@ubuntu:~/docker/unix-domain-socket-example# ls /data1/unix_domain/
    server.sock
    root@ubuntu:~/docker/unix-domain-socket-example# docker exec -it debian bash
    root@b1a3bef38722:/# ls /data1/unix_domain/
    server.sock
    root@b1a3bef38722:/# 

    一对多

    server

    root@ubuntu:~/docker# docker run -it --name debian_s  -v /data1/unix_domain:/data1/unix_domain debian
    root@c66e0ec0c311:/# ./server 
    recvfrom: iccExchangeAPDU
    recvfrom: iccExchangeAPDU
    root@ubuntu:~/docker# docker run -it --name debian_s  -v /data1/unix_domain:/data1/unix_domain debian
    root@c66e0ec0c311:/# ./server 
    recvfrom: iccExchangeAPDU
    recvfrom: iccExchangeAPDU
    recvfrom: /data1/unix_domain/client.sockclient2
    recvfrom: /data1/unix_domain/client.sockclient3
    recvfrom: /data1/unix_domain/client.sockclient2
    recvfrom: /data1/unix_domain/client.sockclient3

    client1

    root@ubuntu:~/docker# docker run -it --name debian2  -v /data1/unix_domain:/data1/unix_domain debian
    root@7369904e52f4:/# ls
    bin  boot  client  data1  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    root@7369904e52f4:/# ./client client2
    sent iccExchangeAPDU
    receive 15 transmit good!
    root@7369904e52f4:/# ./client client2
    sent /data1/unix_domain/client.sockclient2
    receive 15 transmit good!

    client2

    root@ubuntu:~# docker run -it --name debian3  -v /data1/unix_domain:/data1/unix_domain debian
    root@6373fc51aeae:/# ./client client3
    sent iccExchangeAPDU
    receive 15 transmit good!
    root@6373fc51aeae:/# ./client client3
    sent /data1/unix_domain/client.sockclient3
    receive 15 transmit good!

    查看套接字

    root@ubuntu:~# ls /data1/unix_domain/
    root@ubuntu:~# ls /data1/unix_domain/
    client.sockclient2  client.sockclient3  server.sock
    root@ubuntu:~# 

    client 代码

    unix-domain-socket-example

    #include <stdio.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include "ipc.h"
    
    int main(int argc, char *argv[]) {
            int fd;
            struct sockaddr_un addr;
            int ret;
            char buff[8192];
            struct sockaddr_un from;
            int ok = 1;
            int len;
            char buffer[128];
    
            if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
                    perror("socket");
                    ok = 0;
            }
    
            if (ok) {
                    memset(&addr, 0, sizeof(addr));
                    snprintf(buffer,sizeof(buffer)-1,"%s%s",CLIENT_SOCK_FILE,argv[1]);
                    addr.sun_family = AF_UNIX;
                    strcpy(addr.sun_path, buffer);
                    unlink(buffer);
                    if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
                            perror("bind");
                            ok = 0;
                    }
            }
    
            if (ok) {
                    memset(&addr, 0, sizeof(addr));
                    addr.sun_family = AF_UNIX;
                    strcpy(addr.sun_path, SERVER_SOCK_FILE);
                    if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
                            perror("connect");
                            ok = 0;
                    }
            }
    
            if (ok) {
                    //strcpy (buff, "iccExchangeAPDU");
                    if (send(fd, buffer, strlen(buffer)+1, 0) == -1) {
                            perror("send");
                            ok = 0;
                    }
                    printf ("sent %s
    ", buffer);
            }
    
            if (ok) {
                    if ((len = recv(fd, buff, 8192, 0)) < 0) {
                            perror("recv");
                            ok = 0;
                    }
                    printf ("receive %d %s
    ", len, buff);
            }
    
            if (fd >= 0) {
                    close(fd);
            }
    
            getchar();
            unlink (buffer);
            return 0;
    }
  • 相关阅读:
    python练习1--求和
    python--文件操作
    【省选】SDOI2017_树点涂色_LuoguP3703/LOJ2001_LCT/线段树
    【洛谷】P1784 数独
    【洛谷】P2671 求和
    【洛谷】P2261 [CQOI2007]余数求和
    【转载】利用向量积(叉积)计算三角形的面积和多边形的面积
    【洛谷】P1090 合并果子
    【转载】学习C++ -> 类(Classes)的定义与实现
    【洛谷】P2142 高精度减法
  • 原文地址:https://www.cnblogs.com/dream397/p/15079844.html
Copyright © 2011-2022 走看看