// Run like this: simple_client address port // Results in argv ["./simple_client", "address", "port"] #include #include #include #include #include #include #include #include struct game_header { unsigned char mtype; uint16_t points, stat_limit, description_length; } __attribute__((packed)); int main(int args, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(atoi(argv[2])); sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); struct hostent* entry = gethostbyname(argv[1]); struct in_addr **addr_list = (struct in_addr**)entry->h_addr_list; struct in_addr* c_addr = addr_list[0]; char* ip_string = inet_ntoa(*c_addr); sad.sin_addr = *c_addr; printf("Connecting to: %s\n", ip_string); connect(skt, (struct sockaddr*)&sad, sizeof(struct sockaddr_in)); unsigned char stuff[3]; read(skt, &stuff, 3); uint16_t extension_length; read(skt, &extension_length, sizeof(uint16_t)); printf("Message Type: %u\n", stuff[0]); printf("Version: %u.%u\n", stuff[1], stuff[2]); printf("Extension Length: %u\n", extension_length); // We'll use a struct this time, for variety struct game_header gh; recv(skt, &gh, sizeof(gh), MSG_WAITALL); printf("Type: %u\nInitial Points: %u\nStat Limit: %u\nDescription Length: %u\n", gh.mtype, gh.points, gh.stat_limit, gh.description_length); char description[gh.description_length + 1]; recv(skt, description, gh.description_length, 0); description[gh.description_length] = 0; printf("Description: %s\n", description); close(skt); return 0; }