发布网友 发布时间:2024-10-23 19:25
共1个回答
热心网友 时间:2024-11-01 01:55
那和你的防火墙配置有关,和你的程序没有关系!
查找一下你的防火墙设置,把和你程序相关的“规则”删除
源代码是这样的
BOOL CServerDlg::InitNetwork()
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,1), &wsaData) != 0)
{
WSACleanup();
AfxMessageBox( "初始化网络协议失败! ");
return FALSE;
}
ServerSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ServerSocket == INVALID_SOCKET)
{
closesocket(ServerSocket);
WSACleanup();
AfxMessageBox( "创建套接字失败! ");
return FALSE;
}
sockaddr_in localAddr;
//memset((void*)&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(8888);
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(ServerSocket, (struct sockaddr*)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
{
//就在这里绑定不起,错误代码为10049
closesocket(ServerSocket);
WSACleanup();
AfxMessageBox( "绑定地址失败! ");
return FALSE;
}
if(WSAAsyncSelect(ServerSocket, m_hWnd, NETWORK_EVENT, FD_ACCEPT|FD_READ|FD_CLOSE)
== SOCKET_ERROR)
{
closesocket(ServerSocket);
WSACleanup();
AfxMessageBox( "注册网络异步事件失败! ");
return FALSE;
}
if(listen(ServerSocket, 5) == SOCKET_ERROR)
{
closesocket(ServerSocket);
AfxMessageBox( "设置监听失败! ");
WSACleanup();
return FALSE;
}
return TRUE;
}