【发布时间】:2013-06-20 14:09:04
【问题描述】:
我在绑定套接字时遇到问题。没有这样的错误,不知道为什么如果条件失败。
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 %s", strerror(errno));
}
else
{
printf("All done");
}
WSACleanup();
return 0;
我在线路上遇到错误
if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))== SOCKET_ERROR)
但是strerror(errno)) 给了我:“没有错误”。但它仍然在 if 条件下失败并且不绑定。
有什么帮助吗?
【问题讨论】:
-
msdn.microsoft.com/en-us/library/windows/desktop/… : "如果没有发生错误,则bind返回零。否则返回SOCKET_ERROR,可以通过调用WSAGetLastError获取特定的错误代码。"跨度>
-
好的,我明白了。它以 10014 失败......我在 64 位系统上......它会有所作为吗?
-
嗯,你查过这个错误是什么意思吗?你当时做了什么调查?
-
好吧!我看了stackoverflow.com/questions/861154/winsock-error-code-10014。我想,我与 64 位和 32 位有关,我们看起来更有可能对我来说......你有什么建议吗?
-
您的错误与 32 位和 64 位完全无关。它与代码中的逻辑错误有关。详情见我的回答。