Advertisement
rfmonk

datasend.c

Nov 22nd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/un.h>
  4. #include <stdio.h>
  5.  
  6. #define DATA "This is from An Introductory 4.3BSD Interprocess Communication Tutorial"
  7.  
  8. /*
  9.  * Here I send a datagram to a receiver whose name I get from
  10.  * the command line arguments. The form of the command line
  11.  * is udgramsend pathname.
  12.  */
  13.  
  14. main(argc, argv)
  15.     int argc;
  16.     char *argv[];
  17. {
  18.     int sock;
  19.     struct sockaddr_un name;
  20.  
  21.     /* Create socket on which to send. */
  22.     sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  23.     if (sock < 0) {
  24.         perror("opening datagram socket");
  25.         exit(1);
  26.     }
  27.     /* Construct name of socket to send to. */
  28.     name.sun_family = AF_UNIX;
  29.     strcpy(name.sun_path, argv[1]);
  30.     /* Send message. */
  31.     if (sendto(sock, DATA, sizeof(DATA), 0,
  32.         &name, sizeof(struct sockaddr_un)) < 0) {
  33.             perror("sending datagram message");
  34.     }
  35.     close(sock);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement