// programa cliente6_2.cpp --> ler uma estrutura e utiliza o hostnome escolhido via teclado // forneça o nome da máquina do servidor: euler, p.e. #include #include #include #include #include #include #include #include #include #include using std::cout; using std::cin; using std::endl; int main( ) { int sockfd; int len; struct sockaddr_in address; int result; unsigned short porta; struct hostent *nome_da_maquina; char hostname[256]; int idade = 0; porta = 9734; // numero da porta // cout << " Forneça o nome da máquina a que deseja se conectar : "; //cin >> hostname; //cout << endl << " O nome do local do servidor eh " << hostname << endl; strcpy(hostname, "euler"); nome_da_maquina = gethostbyname(hostname); // pegando a máquina a ser conectada if (nome_da_maquina == (struct hostent *) 0) { fprintf(stderr, "Gethostbyname falhou\n"); exit(1); } if( (sockfd = socket(AF_INET, SOCK_STREAM,0) ) < 0) // criacao do socket { perror(" Houve erro na abertura do socket "); exit (1); } address.sin_family = AF_INET; // address.sin_addr = *(struct in_addr *) *nome_da_maquina->h_addr_list; address.sin_addr.s_addr = *((unsigned long int *)nome_da_maquina->h_addr); // address.sin_addr.s_addr = *((unsigned long int *)nome_da_maquina->h_addr_list[0]); address.sin_port = htons(porta); len = sizeof(address); result = connect(sockfd, (struct sockaddr *) &address, len); if (result == -1) { perror ("Houve erro no cliente"); exit(1); } if ( read(sockfd, &idade,sizeof(idade)) < 0 ) { perror( " Houve erro na leitura"); exit(1); } cout << " idade : " << idade <