简体   繁体   English

gethostbyname() 中的资源暂时不可用

[英]Resource temporarily unavailable in gethostbyname()

I was writing my first client in C when I reached the point where I have to send a socket to the server.当我到达必须向服务器发送套接字的地步时,我正在用 C 编写我的第一个客户端。 When i try to get its address I get a当我尝试获取它的地址时,我得到一个

Resource temporarily unavailable资源暂时不可用

and I can't find what is causing this problem.我找不到导致此问题的原因。

dadesSoftConfig->ipServer has inside localhost dadesSoftConfig->ipServer 里面有本地主机

    struct hostent *ent;

    ent = gethostbyname(dadesSoftConfig->ipServidor);
    if (ent == NULL) {
        int errnum = errno;
        fprintf(stderr, "Client finalitzat: %s\n", strerror(errnum));
        exit(EXIT_FAILURE);
    }

I do not send any socket, wait for any data yet when I do this call , this is happening at the very beginning, even before the register phase of my protocol.当我执行此调用时,我不发送任何套接字,等待任何数据,这发生在最开始,甚至在我的协议的注册阶段之前。

As requested, this is a print of the dadesSoftConfig:根据要求,这是 dadesSoftConfig 的打印件:

DEBUG_INFO:     Nom: SW-01
                Mac: 89F107457A36
                Server: localhost
                Server-port: 2019

And this is how I print it:这就是我打印它的方式:

void print_software_configuration(TDadesSoftConfig *dades) {
    char *msg;
    msg = (char *) malloc(sizeof(char) * 75);
    if (sprintf(msg, "\tNom: %s \t\tMac: %s \t\tServer: %s \t\tServer-port: %d\n", 
                dades->nom, dades->mac, dades->ipServidor, dades->serverPort) < 0) {
        fprintf(stderr, "No s'ha pogut mostrar el contingut llegit\n");
    } else {
        print_debug_info(msg);
    }
    free(msg);
}

I tried sending "127.0.0.1" to gethostbyname() function and the code perfectly works, even when I store it to my struct.我尝试将“127.0.0.1”发送到 gethostbyname() 函数并且代码完美运行,即使我将它存储到我的结构中也是如此。 Any idea why it doesn't work when sending "localhost"?知道为什么它在发送“localhost”时不起作用?

As pointed by my prints, the data structure was correct, but the data it contained was not.正如我的打印所指出的,数据结构是正确的,但它包含的数据却不是。 Info should be displayed separated by \\t\\t not a \\n\\t\\t.信息应该以\\t\\t 分隔而不是\\n\\t\\t 分隔。

The problem was solved when setting correctly the delimiters to strtok() function which parses the input.将分隔符正确设置为解析输入的 strtok() 函数时,问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM