#include #include #include #include #include #include #include #include #include int32_t total_weight = 0; int32_t total_squirrels = 0; void* handle_client(void *p){ int client_fd = (size_t)p; uint8_t type; while(read(client_fd, &type, 1) == 1){ if(type == 78) { int32_t weight; read(client_fd, &weight, 4); total_weight += weight; total_squirrels++; type = 65; write(client_fd, &type, 1); } else if(type == 81) { type = 83; write(client_fd, &type, 1); // In your lurk server, check to make sure write worked write(client_fd, &total_weight, 4); write(client_fd, &total_squirrels, 4); } else break; } close(client_fd); return 0; } int main(){ int server_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(5162); addr.sin_addr.s_addr = INADDR_ANY; bind(server_fd, (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)); listen(server_fd, 5); for(;;){ size_t client_fd = accept(server_fd, 0, 0); pthread_t T; pthread_create(&T, 0, handle_client, (void*)client_fd); } return 0; }