/* * This is a better server than simple_server.c * I'm only going to comment changes from simple_server.c * So if there's something that doesn't make any sense in here, maybe look at that one. */ #include #include #include #include #include #include #include #include #include using namespace std; class version { public: // You don't have to use uint8_t, it's the same as "unsigned char" uint8_t type = 14; uint8_t major; uint8_t minor; uint16_t extension_length = 0; version(uint8_t p_major, uint8_t p_minor) : major(p_major), minor(p_minor) {} bool send(int skt){ return 5 == write(skt, this, 5); } } __attribute__((packed)); // we'll have to make sure this is packed class game { public: uint8_t type = 11; uint16_t initial_points; uint16_t stat_limit; uint16_t description_length; string description; game(uint16_t ip, uint16_t sl, string d) : initial_points(ip), stat_limit(sl), description(d) {} bool send(int skt){ description_length = description.length(); if(7 != write(skt, this, 7)) return false; if(description_length != write(skt, description.c_str(), description_length)) return false; return true; } } __attribute__((packed)); /* g++ says it ignores this __attribute__((packed)), but it doesn't * clang++ is more honest, says no such thing, and also doesn't ignore this */ int skt; void shut_down_server(int signal){ printf("\nDoing everything we need to do to shut down the server\n"); close(skt); exit(0); } int main(int argc, char ** argv){ uint16_t listen_port = 5141; // 5141 is the default if(argc > 1){ listen_port = atoi(argv[1]); if(listen_port < 1){ printf("Invalid port: %s\n", argv[1]); exit(1); } printf("Will listen on port %d\n", listen_port); } else { printf("No port specified, defaulting to %d\n", listen_port); } struct sigaction sa; sa.sa_handler = shut_down_server; sigaction(SIGINT, &sa, 0); struct sockaddr_in sad; sad.sin_port = htons(listen_port); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; 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; } int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); version our_version(6,17); game our_game(421, 1048, "An even better game than the last one!"); while(1){ client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); // step 4 printf("Connection made from address %s\n", inet_ntoa(client_address.sin_addr)); // send stuff here our_version.send(client_fd); our_game.send(client_fd); close(client_fd); } return 0; }