基本上,就是這樣。同時,客戶的連接呼叫是成功的。這怎麼可能?我沒有添加任何代碼,因爲我不知道錯誤在哪裏。FD_ACCEPT檢測到,但然後調用'接受'失敗,10038 - WSAENOTSOCK
服務器:檢測FD_ACCEPT。呼叫接受()失敗。
客戶端:呼叫連接()成功。然後它檢測到FD_CONNECT。以下send()成功。之後的send()失敗(10053 - WSAECONNABORTED)。
void Server::get_addressinfo() {
// Resolve the local address and port to be used by the server
const char * p = port.c_str();
int iResult = getaddrinfo(NULL, p, &hints, &result);
if (iResult != 0) {
throw MyException("getaddrinfo failed.");
}
}
void Server::create_socket() {
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
throw MyException("socket creation failed.");
}
}
void Server::bind_socket() {
int iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ListenSocket);
throw MyException("bind failed.");
}
}
void Server::listen_for_connection() {
int iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
closesocket(ListenSocket);
throw MyException("listen failed.");
}
}
void Server::goLive() {
get_addressinfo();
create_socket();
bind_socket();
listen_for_connection();
wait_for(FD_ACCEPT);
accept_connection();
}
void Server::wait_for(u_int event_type) {
WSAEVENT event = WSACreateEvent();
WSAEventSelect(ListenSocket, event, event_type);
WSAEVENT lphEvents[1] = {event};
WSANETWORKEVENTS NetworkEvents = {0};
int nReturnCode = WSAWaitForMultipleEvents(1, &lphEvents[0], false, WSA_INFINITE, false);
if (nReturnCode==WSA_WAIT_FAILED)
throw MyException("WSA__WAIT_FAILED.\n");
if (WSAEnumNetworkEvents(ListenSocket, lphEvents[0], &NetworkEvents) == SOCKET_ERROR)
throw MyException("WSAEnumNetworkEvents => SOCKET_ERROR.\n");
WSACloseEvent(event); ***// THIS WAS THE BUG !!!***
}
void Server::accept_connection() {
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
closesocket(ListenSocket);
throw MyException("accept failed.");
}
}
錯誤信息幾乎不可能更清晰,'錯誤在哪裏'顯然是在調用'accept()'。但如果你不想要答案,不要發佈代碼。隨你便。 – EJP
添加了代碼。 – Earnie
如果接受失敗,爲什麼要關閉偵聽套接字?這是否應該只接受一個連接或者是處理多個連接的方案的一部分? –