1 .\" Copyright (c) 1986, 1993 2 .\" The Regents of the University of California. All rights reserved. 3 .\" 4 .\" %sccs.include.redist.roff% 5 .\" 6 .\" @(#)udgramread.c 8.1 (Berkeley) 06/08/93 7 .\" 8 #include <sys/types.h> 9 #include <sys/socket.h> 10 #include <sys/un.h> 11 12 /* 13 * In the included file <sys/un.h> a sockaddr_un is defined as follows 14 * struct sockaddr_un { 15 * short sun_family; 16 * char sun_path[108]; 17 * }; 18 */ 19 20 #include <stdio.h> 21 22 #define NAME "socket" 23 24 /* 25 * This program creates a UNIX domain datagram socket, binds a name to it, 26 * then reads from the socket. 27 */ 28 main() 29 { 30 int sock, length; 31 struct sockaddr_un name; 32 char buf[1024]; 33 34 /* Create socket from which to read. */ 35 sock = socket(AF_UNIX, SOCK_DGRAM, 0); 36 if (sock < 0) { 37 perror("opening datagram socket"); 38 exit(1); 39 } 40 /* Create name. */ 41 name.sun_family = AF_UNIX; 42 strcpy(name.sun_path, NAME); 43 if (bind(sock, &name, sizeof(struct sockaddr_un))) { 44 perror("binding name to datagram socket"); 45 exit(1); 46 } 47 printf("socket -->%s\en", NAME); 48 /* Read from the socket */ 49 if (read(sock, buf, 1024) < 0) 50 perror("receiving datagram packet"); 51 printf("-->%s\en", buf); 52 close(sock); 53 unlink(NAME); 54 } 55