#include #include #include #include #include #include #include #include #include #include #include #define SERVER_PORT 1400 #define MAX_COMPUTATION 10 #define FALSE 0 #define TRUE (!FALSE) #define SUCCESS 0 #define FAILED -1 struct incomedata { double income; /* income */ double family; /* the number of members of family */ }; double calc_tax(struct incomedata data) { double tax; double income = data.income; double f = data.family; double i_f = income / f; if (i_f <= 22610.0) { tax = 0; } else if (i_f <= 49400.0) { tax = (income * 0.12) - (2713.20 * f); } else if (i_f <= 87020.0) { tax = (income * 0.25) - (9140.40 * f); } else if (i_f <= 140900.0) { tax = (income * 0.35) - (17842.40 * f); } else if (i_f <= 229260.0) { tax = (income * 0.45) - (31932.40 * f); } else if (i_f <= 282730.0) { tax = (income * 0.50) - (43395.40 * f); } else { tax = (income * 0.568) - (62621.04 * f); } return tax; } int main(int argc, char *argv[]) { int fd; /* socket descriptor */ int newfd; struct sockaddr_in srv; struct sockaddr_in cli; int cli_len = sizeof(cli); struct incomedata idata; double tax; int i; /* Create the Socket */ if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == FAILED) { perror("socket"); exit(FAILED); } /* Prepare for Bind */ srv.sin_family = AF_INET; srv.sin_port = htons(SERVER_PORT); srv.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind */ if (bind(fd, (struct sockaddr *)&srv, sizeof(srv)) == FAILED) { perror("bind"); exit(FAILED); } for (i = 0; i < MAX_COMPUTATION; i++) { /* Listen */ if (listen(fd, 5) == FAILED) { perror("listen"); exit(FAILED); } /* Accept */ if ((newfd = accept(fd, (struct sockaddr*)&cli, &cli_len)) == FAILED) { perror("accept"); exit(FAILED); } /* Read */ if (read(newfd, &idata, sizeof(struct incomedata)) < sizeof(struct incomedata)) { perror("read"); exit(FAILED); } tax = calc_tax(idata); /* Write */ if (write(newfd, &tax, sizeof(tax)) < sizeof(tax)) { perror("write"); exit(FAILED); } if (shutdown(newfd, SHUT_RDWR) == FAILED) { perror("shutdown(newfd)"); } if (close(newfd) == FAILED) { perror("close(newfd)"); } } #if 0 if (shutdown(fd, SHUT_RDWR) == FAILED) { perror("shutdown(fd)"); } #endif if (close(fd) == FAILED) { perror("close(fd)"); } return (SUCCESS); }