Advertisement
Guest User

unix datagram client

a guest
Feb 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/epoll.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/un.h>
  10. #include <unistd.h>
  11.  
  12. #define SOCK_PATH "test_socket"
  13. #define MAX_EVENTS 10
  14.  
  15. /* make gcc -std=c99 -g3 -Wall -o client client.c */
  16. int
  17. main(void)
  18. {
  19. int sockfd, epollfd;
  20. struct sockaddr_un server;
  21. struct epoll_event ev;
  22. char buf[1024];
  23.  
  24. sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
  25. if (sockfd == -1) {
  26. printf("SOCKET ERROR = %d\n", errno);
  27. exit(1);
  28. }
  29.  
  30. epollfd = epoll_create(1024);
  31. if (epollfd < 0) {
  32. printf("epoll failed %d: %s", errno, strerror(errno));
  33. exit(1);
  34. }
  35.  
  36. // notify on EPOLLOUT events (ie: fd becomes writable) with edge-triggered behavior
  37. ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR | EPOLLET;
  38. ev.data.fd = sockfd;
  39. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1) {
  40. perror("epoll_ctl: sockfd");
  41. exit(EXIT_FAILURE);
  42. }
  43.  
  44. // combine edge-triggered epolling with non-blocking sockets
  45. if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) {
  46. printf("fcntl F_SETFL failed.%s", strerror(errno));
  47. exit(1);
  48. }
  49.  
  50. memset(&server, 0, sizeof(struct sockaddr_un));
  51. server.sun_family = AF_UNIX;
  52. strcpy(server.sun_path, SOCK_PATH);
  53.  
  54. for (unsigned i = 0; ; i++) {
  55. struct epoll_event events[MAX_EVENTS];
  56. int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
  57. if (nfds == -1) {
  58. perror("epoll_wait");
  59. exit(EXIT_FAILURE);
  60. }
  61.  
  62. for (unsigned n = 0; n < nfds; n++) {
  63. if (events[n].data.fd != sockfd) {
  64. continue;
  65. }
  66. if (events[n].events & EPOLLHUP) {
  67. printf("EPOLLUP! events is %u\n", events[n].events);
  68. exit(1);
  69. }
  70. if (events[n].events & EPOLLERR) {
  71. printf("EPOLLERR! events is %u\n", events[n].events);
  72. exit(1);
  73. }
  74. if (!(events[n].events & EPOLLOUT)) {
  75. exit(1);
  76. }
  77.  
  78. strcpy(buf, "this is a message in a buffer over unix datagrams\n");
  79. if (i % 10000 == 0) {
  80. printf("Sent %u packets\n", i);
  81. printf("events is %u\n", (unsigned)(events[n].events));
  82. }
  83.  
  84. while (1) {
  85. int r = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *) &server, sizeof(server));
  86. int err = errno;
  87. if (r == -1 && err == EAGAIN) {
  88. // eagain means we should break and wait
  89. break;
  90. } else if (r == -1 && err == ECONNREFUSED) {
  91. // connrefused is ok
  92. continue;
  93. } else if (r == -1 && err == ENOENT) {
  94. // enoent is ok
  95. continue;
  96. } else if (r == -1) {
  97. printf("sendto failed with %d: %s\n", err, strerror(errno));
  98. exit(1);
  99. }
  100. }
  101. }
  102. }
  103.  
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement