wisock中bind的10049错误
代码如下:
scokaddr_in addr = {0};
int nRet = 0;
...
memset(&addr, 0, sizeof(scokaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; //(*)
addr.sin_port = htons(nPort);
nRet = bind(sock, (SOCKADDR*)&addr, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
cout << "bind failed! err code = " << ::WSAGetLastError() << endl;
return -1;
}
...
运行结果: bind failed! err code = 10049
error lookup 的解释:在其上下文中,该请求的地址无效。
用netstat -a -n 查看发现所设置的端口没有被使用.
修改:
将(*)那一行修改为: sin.sin_addr.S_un.S_addr = INADDR_ANY;