#include #include #include #include #include #include #include #include #include int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5143); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); // Step 1 if(skt == -1){ perror("socket"); return 1; } if( bind(skt, (struct sockaddr*)(&sad), sizeof(struct sockaddr_in)) ){ // step 2 perror("bind"); return 1; } if( listen(skt, 5) ){ // step 3 perror("listen"); return 1; } while(1){ int client_fd = accept(skt, 0, 0); // step 4 char request[5] = " "; int64_t nums[2]; int64_t result = 1; while(read(client_fd, request, 4) > 0){ read(client_fd, nums, 16); printf("Got request %s\n", request); if(nums[1] == 0){ write(client_fd, "LAZY", 4); write(client_fd, &result, 8); goto skip_result; } else if(request[0] == 'M'){ printf("Doing Modulus\n"); result = nums[0] % nums[1]; } else { result = nums[0] / nums[1]; } write(client_fd, "RESL", 4); write(client_fd, &result, 8); skip_result: } close(client_fd); } return 0; }