Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 远程客户端上的gethostbyname,但有时会获得';资源暂时不可用';_C_Sockets_Tcp_Connection_Client Server - Fatal编程技术网

C 远程客户端上的gethostbyname,但有时会获得';资源暂时不可用';

C 远程客户端上的gethostbyname,但有时会获得';资源暂时不可用';,c,sockets,tcp,connection,client-server,C,Sockets,Tcp,Connection,Client Server,有时,特别是当客户机和服务器位于不同的机器上时(一台在我的ubuntu中,另一台在虚拟机ubuntu中),试图通过主机名连接到服务器的客户机会收到 getHostEntry错误:资源暂时不可用 这是server.c文件: #include <stdio.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/stat.h> #

有时,特别是当客户机和服务器位于不同的机器上时(一台在我的ubuntu中,另一台在虚拟机ubuntu中),试图通过主机名连接到服务器的客户机会收到

getHostEntry错误:资源暂时不可用

这是server.c文件:

#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);
    }
}
它显示:

IP: 1.2.3.4
Hostname: johnmayer
如果我尝试从另一台计算机(在虚拟机上安装了ubuntu)连接到此服务器,使用

./client johnmayer 8888
有时我会变得

getHostEntry错误:资源暂时不可用


代码中是否有错误?

附加一个调试器,找出错误的确切位置。使用调试器是找到您自己的bug的最佳方法。对于初学者,请进行适当的错误检查。例如,在使用前检查
gethostbyname
的返回值。同样,调试器几乎可以立即告诉您是否返回NULL,从而导致NULL指针取消引用。@已选中kaylum gethostbyname:
hostEntry=gethostbyname(hostbuffer);检查招待所(招待所)其中
checkHostEntry()
函数是在我所附的代码中指定的。它在服务器中。这是不相关的,因为是客户在崩溃,对吗?检查客户端代码。我添加了该检查,它会返回暂时不可用的资源
IP: 1.2.3.4
Hostname: johnmayer
./client johnmayer 8888