c - What is the correct way to listen to both UDS and TCP sockets in a `fork()` based server? -
c - What is the correct way to listen to both UDS and TCP sockets in a `fork()` based server? -
i'm writing fork()
based server, tcp sockets communication channel of clients server , uds socket (datagram, if makes difference) communication channel of management console server.
what right way hear both socket types? server looks pretty much fork()
server in beej's example:
while(1) { // main accept() loop sin_size = sizeof their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection %s\n", s); if (!fork()) { // kid process close(sockfd); // kid doesn't need listener if (send(new_fd, "hello, world!", 13, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); // parent doesn't need }
how add together above code ability hear , wait connection in uds socket (that binded).
use select()
, poll()
, or epoll()
(epoll()
assume linux.)
or utilize multiple threads.
c sockets network-programming posix unix-domain-sockets
Comments
Post a Comment