rendered paste body#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/wait.h>#include <signal.h>#define MYPORT 12040#define BACKLOG 10using namespace std;int tcpip_bindsocket(char s_address[16], int s_port);void tcpip_listen(int socketfd, int backlog);int tcpip_accept(int socketfd);void tcpip_close(int socketfd);void server_mainlistener(int socketfd);void server_mainclienthandler(int socketfd);void server_reaper();void server_reaper_handler(int s);int tcpip_bindsocket(char s_address[16], int s_port){ int sockfd; int yes=1; struct sockaddr_in my_addr; if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("TCPIP Error: Failed to create socket"); exit(1); } if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { perror("TCPIP Error: setsockopt failed"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = inet_addr(s_address); my_addr.sin_port = htons(s_port); memset(&(my_addr.sin_zero), '\0', 8); bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("TCPIP Error: Can't bind to port"); exit(1); } return sockfd;}void tcpip_listen(int socketfd, int backlog){ if (listen(socketfd, backlog) == -1) { perror("TCPIP Error: Listen Failed"); exit(1); }}int tcpip_accept(int socketfd){ socklen_t sin_size; int new_fd; struct sockaddr_in their_addr; // connector's address information if ((new_fd = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("TCPIP Error: Accept Failed"); exit(0); } return new_fd;}void tcpip_close(int socketfd){ close(socketfd);}void server_mainclienthandler(int socketfd){ send(socketfd, "Hello, world!\n", 14, 0);}void server_mainlistener(int socketfd){ socklen_t sin_size; int client_sockfd; struct sockaddr_in their_addr; // connector's address information while(1) { sin_size = sizeof(struct sockaddr_in); printf("TCPIP: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); if (!fork()) { // this is the child client handler process client_sockfd = tcpip_accept(socketfd); tcpip_close(socketfd); // child doesn't need the listener server_mainclienthandler(client_sockfd); tcpip_close(client_sockfd); exit(0); //kill the child } tcpip_close(client_sockfd); // parent doesn't need this }}void server_reaper_handler(int s){ while(waitpid(-1, NULL, WNOHANG) > 0);}void server_reaper(){ struct sigaction sa; sa.sa_handler = server_reaper_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("ERROR: Server Child Cleanup Failed"); exit(1); }}main(){ int sockfd, new_fd; // listen on sock_fd, new connection on new_fd sockfd = tcpip_bindsocket("192.168.0.8",MYPORT); // open the socket tcpip_listen(sockfd,BACKLOG); // start listening server_reaper(); // reap dead child threads server_mainlistener(sockfd); // run the main listener loop exit(1);}