filecli.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Example of Reading Datagrams
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <sys/socket.h>
  19. #include <sys/un.h>
  20. #define SERVER "/tmp/serversocket"
  21. #define CLIENT "/tmp/mysocket"
  22. #define MAXMSG 512
  23. #define MESSAGE "Yow!!! Are we having fun yet?!?"
  24. int
  25. main (void)
  26. {
  27. extern int make_named_socket (const char *name);
  28. int sock;
  29. char message[MAXMSG];
  30. struct sockaddr_un name;
  31. size_t size;
  32. int nbytes;
  33. /* Make the socket. */
  34. sock = make_named_socket (CLIENT);
  35. /* Initialize the server socket address. */
  36. name.sun_family = AF_LOCAL;
  37. strcpy (name.sun_path, SERVER);
  38. size = strlen (name.sun_path) + sizeof (name.sun_family);
  39. /* Send the datagram. */
  40. nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
  41. (struct sockaddr *) & name, size);
  42. if (nbytes < 0)
  43. {
  44. perror ("sendto (client)");
  45. exit (EXIT_FAILURE);
  46. }
  47. /* Wait for a reply. */
  48. nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
  49. if (nbytes < 0)
  50. {
  51. perror ("recfrom (client)");
  52. exit (EXIT_FAILURE);
  53. }
  54. /* Print a diagnostic message. */
  55. fprintf (stderr, "Client: got message: %s\n", message);
  56. /* Clean up. */
  57. remove (CLIENT);
  58. close (sock);
  59. }