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\n", buffer); } if (ok) { if ((len = recv(fd, buff, 8192, 0)) < 0) { perror("recv"); ok = 0; } printf ("receive %d %s\n", len, buff); } if (fd >= 0) { close(fd); } getchar(); unlink (buffer); return 0; }