Advertisement
rfmonk

dataread.c

Nov 22nd, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/un.h>
  4.  
  5. /*
  6.  * In the included file <sys/un.h> a sockaddr_un is defined
  7.  * as follows:
  8.  * struct sockaddr_un {
  9.     short sun_family;
  10.     char sun_path[108];
  11.  };
  12.  */
  13.  
  14.  #include <stdio.h>
  15.  
  16.  #define NAME "socket"
  17.  
  18.  /*
  19.   * This program creates a UNIX domain datagram socket,
  20.   * binds a name to it, then reads from the socket.
  21.   */
  22.  
  23.  main()
  24.  {
  25.     int sock, length;
  26.     struct sockaddr_un name;
  27.     char buf[1024];
  28.  
  29.     /* Create socket from which to read. */
  30.     sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  31.     if (sock < 0) {
  32.         perror("opening datagram socket");
  33.         exit(1);
  34.     }
  35.     /* Create name. */
  36.     name.sun_family = AF_UNIX;
  37.     strcpy(name.sun_path, NAME);
  38.     if (bind(sock, &name, sizeof(struct sockaddr_un))) {
  39.         perror("binding name to datagram socket");
  40.         exit(1);
  41.     }
  42.     printf("socket -->%s\n", NAME);
  43.     /* Read from the socket */
  44.     if (read(sock, buf, 1024) < 0)
  45.         perror("receiving datagram packet");
  46.     printf("-->%s\n", buf);
  47.     close(sock);
  48.     unlink(NAME);
  49.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement