.\" Copyright (c) 1986 The Regents of the University of California. .\" All rights reserved. .\" .\" %sccs.include.redist.roff% .\" .\" @(#)udgramread.c 6.5 (Berkeley) 04/17/91 .\" #include #include #include /* * In the included file a sockaddr_un is defined as follows * struct sockaddr_un { * short sun_family; * char sun_path[108]; * }; */ #include #define NAME "socket" /* * This program creates a UNIX domain datagram socket, binds a name to it, * then reads from the socket. */ main() { int sock, length; struct sockaddr_un name; char buf[1024]; /* Create socket from which to read. */ sock = socket(AF_UNIX, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* Create name. */ name.sun_family = AF_UNIX; strcpy(name.sun_path, NAME); if (bind(sock, &name, sizeof(struct sockaddr_un))) { perror("binding name to datagram socket"); exit(1); } printf("socket -->%s\en", NAME); /* Read from the socket */ if (read(sock, buf, 1024) < 0) perror("receiving datagram packet"); printf("-->%s\en", buf); close(sock); unlink(NAME); }