gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:30:11 26 4
gpt4 key购买 nike

当我必须向服务器发送套接字时,我正在用 C 语言编写第一个客户端。当我尝试获取它的地址时,我得到一个

Resource temporarily unavailable

我找不到导致此问题的原因。

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);
}

当我执行此调用时,我不发送任何套接字,等待任何数据,这发生在一开始,甚至在我的协议(protocol)的注册阶段之前。

根据要求,这是 datesSoftConfig 的打印:

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

这就是我打印它的方式:

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);
}

我尝试将“127.0.0.1”发送到 gethostbyname() 函数,并且代码完美运行,即使我将其存储到我的结构中也是如此。知道为什么发送“localhost”时它不起作用吗?

最佳答案

正如我的打印所指出的,数据结构是正确的,但它包含的数据不正确。信息应以\t\t 分隔显示,而不是\n\t\t。

正确设置解析输入的 strtok() 函数的分隔符后,问题得到解决。

关于c - gethostbyname() 中的资源暂时不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com