gethostbyname на удаленном клиенте, но иногда появляется сообщение «Ресурс временно недоступен» - PullRequest
0 голосов
/ 01 марта 2020

иногда , особенно когда клиент и сервер находятся на разных машинах (одна в моей Ubuntu, а другая в виртуальной машине Ubuntu), клиент, который пытается подключиться к серверу через имя хоста, получает

Ошибка getHostEntry: ресурс временно недоступен.

Это сервер. c file:

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <malloc.h>
#include <netdb.h> 

void checkHostname(int);

void checkHostEntry(struct hostent*);
void checkIpBuffer(char*);

int main(int argc, char **argv) {
    struct sockaddr_in socketAddress;
    int socketDescriptor, clientDescriptor, nread;
    char buffer[1000];
    pthread_t threadId;
    char hostbuffer[256];
    char *IPbuffer;
    struct hostent *hostEntry;
    int hostname;

if((socketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Errore durante la creazione della socket\n");
    exit(1);
}

socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
socketAddress.sin_port = htons(atoi(argv[1]));

if(bind(socketDescriptor, (struct sockaddr*) &socketAddress, sizeof(socketAddress)) != 0) {
    printf("Errore di bind()\n");
    close(socketDescriptor);
    exit(1);
}

hostname = gethostname(hostbuffer, sizeof(hostbuffer)); 
checkHostname(hostname); 

hostEntry = gethostbyname(hostbuffer); 
checkHostEntry(hostEntry); 

IPbuffer = inet_ntoa(*((struct in_addr*) hostEntry->h_addr_list[0])); 

printf("Hostname - %s\n", hostbuffer); 
printf("Host IP - %s:%d\n", IPbuffer, ntohs(socketAddress.sin_port)); 

if(listen(socketDescriptor, 100) < 0) {
    printf("Errore di listen()\n");
    close(socketDescriptor);
    exit(1);
}

return 0;
}

void checkHostname(int hostname) {
    if (hostname == -1) {
        perror("Errore gethostname");
        exit(1);
    }
}

void checkHostEntry(struct hostent *hostentry) {
    if (hostentry == NULL) {
        perror("Errore getHostEntry");
        exit(1);
    }
}

void checkIpBuffer(char *ip) {
    if (ip == NULL) {
        perror("Errore inet_ntoa");
        exit(1);
    }
}

И это мой клиент. c file

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h> 


int main(int argc, char **argv) {
    struct sockaddr_in clientAddress;
    int choice, nread, serverIp;
    char *IPbuffer; 
    struct hostent *host_entry; 


    if((clientDescriptor = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        printf("Errore nella creaione del socket\n");
        exit(1);
    }

    host_entry = gethostbyname(argv[1]);
   checkHostEntry(host_entry)

    IPbuffer = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));                     

    clientAddress.sin_family = AF_INET;
    clientAddress.sin_addr.s_addr = inet_addr(IPbuffer); // indirizzo IP
    clientAddress.sin_port = htons(atoi(argv[2]));  // porta

    if(connect(clientDescriptor, (struct sockaddr*) &clientAddress, sizeof(clientAddress)) < 0) {
        printf("Errore connect()\n");
        close(clientDescriptor);
        exit(1);
    }

    return 0;
}
void checkHostEntry(struct hostent *hostentry) {
        if (hostentry == NULL) {
            perror("Errore getHostEntry");
            exit(1);
        }
    }

Если I

./server.out 8888

, отображается:

IP: 1.2.3.4
Hostname: johnmayer

и если я пытаюсь подключиться с другого компьютера (с установленной Ubuntu на виртуальном ящике) на этот сервер, используя

./client johnmayer 8888

I иногда get

Ошибка getHostEntry: ресурс временно недоступен.

Что-то не так в коде?

...