Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <sys/un.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdio.h>
- #define PORT 5555
- #define handle_error_on(stage)\
- do {\
- printf(stage); printf(" error: i\n", errno);\
- return 1;\
- }\
- while(0)
- int
- main() {
- int sock, ready = 0;
- struct sockaddr_in addr;
- struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
- fd_set read;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT);
- addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- if (0 > (sock = socket(AF_INET, SOCK_STREAM, 0)))
- handle_error_on("socket stage");
- FD_ZERO(&read);
- FD_SET(sock, &read);
- if (0 > bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)))
- handle_error_on("bind stage");
- if (0 > listen(sock, 5))
- handle_error_on("listen stage");
- for (;;) {
- ready = pselect(sock + 1, &read, NULL, NULL, &timeout, NULL);
- if (ready)
- printf("connection arrived");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement