我正在爲大學項目編寫網絡遊戲,雖然我在客戶端和服務器之間發送和接收消息,但我不確定如何執行writeable
fd_set(我的講師的示例代碼僅包含在內一個readable
fd_set)以及這兩個fd_sets與select()
的函數是什麼。你可以提供的任何洞察力都能幫助我理解這一點。有人可以用WinSock解釋可寫和可讀fd_sets的功能嗎?
我的服務器代碼是這樣的:
bool ServerSocket::Update() {
// Update the connections with the server
fd_set readable;
FD_ZERO(&readable);
// Add server socket, which will be readable if there's a new connection
FD_SET(m_socket, &readable);
// Add connected clients' sockets
if(!AddConnectedClients(&readable)) {
Error("Couldn't add connected clients to fd_set.");
return false;
}
// Set timeout to wait for something to happen (0.5 seconds)
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
// Wait for the socket to become readable
int count = select(0, &readable, NULL, NULL, &timeout);
if(count == SOCKET_ERROR) {
Error("Select failed, socket error.");
return false;
}
// Accept new connection to the server socket if readable
if(FD_ISSET(m_socket, &readable)) {
if(!AddNewClient()) {
return false;
}
}
// Check all clients to see if there are messages to be read
if(!CheckClients(&readable)) {
return false;
}
return true;
}
不以任何方式回答問題。 – EJP
@EJP,怎麼樣?它完全告訴OP如何輪詢可寫入的套接字,並將OP指向適當的功能。未能看到它'不回答問題'。 – SergeyA
問題是'有人可以解釋可寫和可讀fd_sets的功能'。你在這裏沒有解釋什麼。 – EJP