/* receiver for unix domain datagram demo */ #include #include #include #include help_msg() { printf("format: ustr\n"); } main(argc, argv) int argc; char *argv[]; { int sock; int msgsock; 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_STREAM, 0); if (sock < 0) { perror("opening stream 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 stream server uses sun_path=%s\n", addr.sun_path); /* receive message */ listen(sock, 2); do { msgsock = accept(sock, 0, 0); printf("msgsock=%d\n", msgsock); if (msgsock == -1) { perror("accept"); exit(2); } else do { n = read(msgsock, fromBuf, sizeof(fromBuf)); if (n < 0) { perror("receiving datagram message"); exit(3); } if (n == 0) { printf("The Client Ending connection\n"); break; } fromBuf[n] = 0; /* null terminate */ printf("The no. of bytes received=%d\n", n); printf("received msg="); for (i=0; i