#include #include #include #include #include #include #include #include #include #include #include #include #define SERVER_PORT 1400 #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 */ }; int main(int argc, char *argv[]) { int fd; /* socket descriptor */ struct sockaddr_in srv; struct hostent *hp; struct incomedata idata; double tax; if (argc != 4) { fprintf(stderr, "Usage: %s [Server] [Income(in dollar)] [the number of members of family(adult is 1, child is 0.5)]\n", argv[0]); fprintf(stderr, "Ex. : %s unix7.andrew.cmu.edu 646870 3.5\n", argv[0]); exit(FAILED); } /* Set data */ idata.income = atof(argv[2]); idata.family = atof(argv[3]); /* 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); hp = gethostbyname(argv[1]); if (hp) { srv.sin_addr.s_addr = *(unsigned long *)(*hp -> h_addr_list); } else { srv.sin_addr.s_addr = inet_addr(argv[1]); if (srv.sin_addr.s_addr == (in_addr_t)FAILED) { fprintf(stderr, "Server Address is invalid.\n"); exit(FAILED); } } /* Connect */ if (connect(fd, (struct sockaddr*)&srv, sizeof(srv)) == FAILED) { perror("connect"); exit(FAILED); } /* Write */ if (write(fd, &idata, sizeof(struct incomedata)) < sizeof(struct incomedata)) { perror("write"); exit(FAILED); } /* Read */ if (read(fd, &tax, sizeof(tax)) < sizeof(tax)) { perror("read"); exit(FAILED); } printf("Income = %.2f, the number of members of the Family = %.1f\n", idata.income, idata.family); printf("Tax = %.2f\n", tax); if (shutdown(fd, SHUT_RDWR) == FAILED) { perror("shutdown"); } close(fd); return (SUCCESS); }