/* receiver for unix domain datagram demo */ #include #include #include #include help_msg() { printf("format: idgr\n"); } main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_un to; struct sockaddr_un addr; int n, i; char toBuf[2048]; char fromBuf[2048]; int length; int flag = 1; /* create socket */ sock = socket(AF_UNIX, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } addr.sun_family = AF_UNIX; sprintf(addr.sun_path, "/tmp/cs522.%s.server", getlogin()); /* unlink(addr.sun_path); if previous incarnation of server still exists */ if (bind(sock, &addr, sizeof(struct sockaddr_un))) { perror("binding name to datagram socket"); exit(1); } /* ls -F /tmp you will find a file with the same name there */ printf("unix domain datagram server uses sun_path=%s\n", addr.sun_path); to.sun_family = AF_UNIX; sprintf(to.sun_path, "/tmp/cs522.%s.client", getlogin()); printf("unix domain datagram client uses sun_path=%s\n", to.sun_path); /* receive message */ while (flag) { n = recv(sock, fromBuf, sizeof(fromBuf), 0); if (n < 0) perror("receiving datagram message"); fromBuf[n] = 0; /* null terminate */ printf("The no. of bytes received=%d\n", n); printf("received msg="); for (i=0; i