0

我正在尝试绑定一个本地套接字,它给了我错误。不确定问题出在哪里。有什么帮助吗?

int result = WSAStartup(MAKEWORD(2,2), &wsadata);
    if(result != NO_ERROR)
    {
      printf("\nThere is a problem at WSAStartup");
    }
    else{
        printf("\nWSAStartup was ok");
    }
    list_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    if(list_sock == -1)
    {
        printf("\n Socket not created %d\n", list_sock);
    }
    else
    {
    printf("\nSocket was created Succesfully");
    }
HOSTENT *thishost;
char *ip;
u_short port;
port = 55555;
thishost = gethostbyname("localhost");
printf("\n");
printf("\nHost name is:%s ",thishost->h_name);
ip = inet_ntoa(*(struct in_addr*)*thishost->h_addr_list);
printf("\nip address is %s\n ", ip);
printf("Address type is:%i",thishost->h_addrtype);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))==SOCKET_ERROR)
{
    printf("Error during bind");
}
else
{
        printf("All done");
    }

它创建套接字,我可以显示 IP 地址、名称等。但是当绑定发生时,它给了我错误。

问候,小号

4

1 回答 1

0

如果它在您第二次运行程序时发生,请检查 SO_REUSE_ADDRESS。

于 2013-06-17T11:30:16.623 回答