// Run like this: simple_client address port // Results in argv ["./simple_client", "address", "port"] #include #include #include #include #include #include #include #include int main(int argc, char ** argv){ if(argc < 4){ printf("Usage: %s hostname port weight\n", argv[0]); return 1; } struct sockaddr_in sad; sad.sin_port = htons(atoi(argv[2])); sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); // do a dns lookup struct hostent* entry = gethostbyname(argv[1]); struct in_addr **addr_list = (struct in_addr**)entry->h_addr_list; sad.sin_addr = *(addr_list[0]); connect(skt, (struct sockaddr*)&sad, sizeof(struct sockaddr_in)); int32_t nw = atoi(argv[3]); uint8_t type = 78; write(skt, &type, 1); write(skt, &nw, 4); read(skt, &type, 1); if(type == 65) printf("Squirrel Accepted\n"); type = 'Q'; write(skt, &type, 1); int32_t res[2]; read(skt, &type, 1); if(type != 'S') printf("Unexpected type: %d\n", type); else printf("Got type S\n"); ssize_t readlen = read(skt, res, 8); printf("Read %ld\n", readlen); printf("Total Squirrels: %d\nAverage Weight: %d\n", res[0], res[1]); close(skt); return 0; }