Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <sys/select.h>
  2. #include <sys/socket.h>
  3. #include <sys/time.h>
  4. #include <sys/un.h>
  5.  
  6. #include <netinet/in.h>
  7.  
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12.  
  13. #define PORT 5555
  14.  
  15. #define handle_error_on(stage)\
  16. do {\
  17. printf(stage); printf(" error: i\n", errno);\
  18. return 1;\
  19. }\
  20. while(0)
  21.  
  22. int
  23. main() {
  24.  
  25. int sock, ready = 0;
  26. struct sockaddr_in addr;
  27. struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
  28.  
  29. fd_set read;
  30.  
  31. addr.sin_family = AF_INET;
  32. addr.sin_port = htons(PORT);
  33. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  34.  
  35. if (0 > (sock = socket(AF_INET, SOCK_STREAM, 0)))
  36. handle_error_on("socket stage");
  37.  
  38. FD_ZERO(&read);
  39. FD_SET(sock, &read);
  40.  
  41. if (0 > bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)))
  42. handle_error_on("bind stage");
  43.  
  44. if (0 > listen(sock, 5))
  45. handle_error_on("listen stage");
  46.  
  47. for (;;) {
  48. ready = pselect(sock + 1, &read, NULL, NULL, &timeout, NULL);
  49. if (ready)
  50. printf("connection arrived");
  51. }
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement