1 .\" Copyright (c) 1986 Regents of the University of California. 2 .\" All rights reserved. The Berkeley software License Agreement 3 .\" specifies the terms and conditions for redistribution. 4 .\" 5 .\" @(#)udgramread.c 6.1 (Berkeley) 05/04/86 6 .\" 7 #include <sys/types.h> 8 #include <sys/socket.h> 9 #include <sys/un.h> 10 11 /* 12 * In the included file <sys/un.h> a sockaddr_un is defined as follows 13 * struct sockaddr_un { 14 * short sun_family; 15 * char sun_path[109]; 16 * }; 17 */ 18 19 #include <stdio.h> 20 21 #define NAME "socket" 22 23 /* 24 * This program creates a UNIX domain datagram socket, binds a name to it, 25 * then reads from the socket. 26 */ 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 close(sock); 45 perror("binding name to datagram socket"); 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 unlink(NAME); 53 close(sock); 54 } 55