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.3 (Berkeley) 05/08/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[108]; 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 main() 28 { 29 int sock, length; 30 struct sockaddr_un name; 31 char buf[1024]; 32 33 /* Create socket from which to read. */ 34 sock = socket(AF_UNIX, SOCK_DGRAM, 0); 35 if (sock < 0) { 36 perror("opening datagram socket"); 37 exit(1); 38 } 39 /* Create name. */ 40 name.sun_family = AF_UNIX; 41 strcpy(name.sun_path, NAME); 42 if (bind(sock, &name, sizeof(struct sockaddr_un))) { 43 perror("binding name to datagram socket"); 44 exit(1); 45 } 46 printf("socket -->%s\en", NAME); 47 /* Read from the socket */ 48 if (read(sock, buf, 1024) < 0) 49 perror("receiving datagram packet"); 50 printf("-->%s\en", buf); 51 close(sock); 52 unlink(NAME); 53 } 54