// 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 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]); if(!entry){ if(h_errno == HOST_NOT_FOUND){ printf("This is our own message that says the host wasn't found\n"); } herror("gethostbyname"); return 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); if( connect(skt, (struct sockaddr*)&sad, sizeof(struct sockaddr_in)) ){ perror("connect"); return 1; } float results[2]; ssize_t actual_length = 0; /* We could do it this way: while(1){ actual_length += read(skt, ((void*)&results) + actual_length, sizeof(results)); // 8 bytes printf("Received %ld bytes\n", actual_length); if(actual_length == sizeof(results)) break; } */ actual_length = recv(skt, &results, 8, MSG_WAITALL); printf("Received number: %f and %f\n", results[0], results[1]); close(skt); return 0; }