gpt4 book ai didi

c - 绑定(bind)错误 (99) : Cannot assign requested address

转载 作者:太空狗 更新时间:2023-10-29 15:19:57 26 4
gpt4 key购买 nike

我试图让下面的代码工作,但收到上述错误。我有理由相信我输入的地址是正确的,因为它在一个单独的程序中工作,该程序正在执行类似的任务。这让我相信我犯了一些愚蠢的错误,我们将不胜感激!

   /*Create TCP socket*/
int tcp_socket(void)
{
int s;
while((s = socket(PF_INET,SOCK_STREAM,0))==-1 && errno == EINTR){
continue;
}
return s;
}

/*Bind tcp*/
int tcp_bind(int s, char *server, uint16_t port)
{
int rv;
struct sockaddr_in sin;
struct hostent *host_addr;

assert(s >= 0);

host_addr = gethostbyname(server);
if (!host_addr) {
perror("gethostbyname(server)");
return -1;
}


memset(&sin, '\0', sizeof sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);

sin.sin_addr = *((struct in_addr *)host_addr->h_addr);

while((rv = bind(s,(struct sockaddr *)&sin, sizeof sin)) == -1
&& errno == EINTR) {
continue;
}
return (rv == 0) ? 0 : errno;
}

int connect_to_server()
{
int rv;
int socket = tcp_socket();
_logf(LOG_DEBUG, "LOG %s:%d (%s) - %s:%d", __FILE__, __LINE__, __func__, "Socket created:", socket);

rv = tcp_bind(socket,options.server_p, options.dest_port);
if(rv != 0) {
_logf(LOG_ALERT, "LOG %s:%d (%s) - %s:%s", __FILE__, __LINE__, __func__, "tcp_bind failed, error", strerror(rv));
return -1;
}
else {
_logf(LOG_ALERT, "LOG %s (%s) - %s:%d", __FILE__, __LINE__, __func__, "TCP bind success");
}
}

int main(int argc, char **argv)
{
if (sysconf() == -1) {
exit(-1);
}

sysinit();

if (parse_options(argc, argv) == -1) {
usage();
exit(1);
}

connect_to_server();
}

错误信息:

Sep 10 10:59:21 michael-VirtualBox mld: LOG mld.c:685 (connect_to_server) - tcp_bind failed, error:Cannot assign requested address

最佳答案

您只能将套接字绑定(bind)到本地接口(interface)的地址。

关于c - 绑定(bind)错误 (99) : Cannot assign requested address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18715985/

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