/*----------------------------------------------------------------- 【函数介绍】: 发送数据 【入口参数】: pCustomCE :客户端对象指针 buf : 缓冲区 dwBufLen : 缓冲区长度 【出口参数】: (无) 【返回 值】: TRUE : 发送成功 ; FALSE : 发送失败 ------------------------------------------------------------------*/ BOOL CTCPServer_CE::SendData(CTCPCustom_CE* pCustomCE, const char * buf , DWORD dwBufLen) { BOOL bResult = FALSE; BOOL bExisted = FALSE; if (pCustomCE == NULL) { return FALSE; } //判断此客户端是否存在 POSITION pos = m_ListClientSocket.GetHeadPosition(); while (pos != NULL) { CTCPCustom_CE *pTcpCustom = (CTCPCustom_CE*)m_ListClientSocket.GetNext(pos); if (pCustomCE == pTcpCustom) { bExisted = TRUE; break; } } if (!bExisted) { return FALSE; } bResult = pCustomCE->SendData(buf,dwBufLen); if (!bResult) { // RemoteClient(pCustomCE); } return bResult; }
void InteractionHost::register_client(local_client_t, const cup::PlayerDefinition* players, std::size_t player_count) { register_client(RemoteClient(local_client), players, player_count); }
void LocalServer::ServerLoop() { sockaddr_in sinRemote; int addrSize = sizeof(sinRemote); fd_set ReadFDs, WriteFDs, ExceptFDs; PopulateFDSets(&ReadFDs, &WriteFDs, &ExceptFDs); int iResult = select(0, &ReadFDs, &WriteFDs, &ExceptFDs, 0); if (iResult != SOCKET_ERROR && iResult) { //Event on the listen socket if (FD_ISSET(listenSocket, &ReadFDs)) { SOCKET acceptSocket = accept(listenSocket, (sockaddr*)&sinRemote, &addrSize); if (acceptSocket != INVALID_SOCKET) { printf("Accepted connection from %s: %d, socket %hd\n", inet_ntoa(sinRemote.sin_addr), ntohs(sinRemote.sin_port), acceptSocket); clientList.push_back(RemoteClient(acceptSocket)); u_long unblock = 1; ioctlsocket(acceptSocket, FIONBIO, &unblock); } else { printf("Error: accept() code %d\n", WSAGetLastError()); return; } } //Get the error that last occured on the specific socket else if (FD_ISSET(listenSocket, &ExceptFDs)) { int error; int errLen = sizeof(int); getsockopt(listenSocket, SOL_SOCKET, SO_ERROR, (char*)&error, &errLen); printf("Error: listening socket code %d\n", error); return; } //Event on a client socket std::vector<RemoteClient>::iterator it = clientList.begin(); while (it != clientList.end()) { bool bOK = true; if (FD_ISSET(it->clientSocket, &ExceptFDs)) { bOK = false; FD_CLR(it->clientSocket, &ExceptFDs); } else { if (FD_ISSET(it->clientSocket, &ReadFDs)) { printf("Client socket %d now readable\n", it->clientSocket); bOK = RecvData(&*it); FD_CLR(it->clientSocket, &ReadFDs); } if (FD_ISSET(it->clientSocket, &WriteFDs)) { printf("Client socket %d now writeable\n", it->clientSocket); bOK = SendData(&*it); FD_CLR(it->clientSocket, &WriteFDs); } } //Get the error that last occured on the specific socket if (!bOK) { int error; int errLen = sizeof(int); getsockopt(it->clientSocket, SOL_SOCKET, SO_ERROR, (char*)&error, &errLen); if (error != NO_ERROR) { printf("Error: client socket %d code %d\n", it->clientSocket, error); } CloseConnection(it->clientSocket); it = clientList.erase(it); } else ++it; } } else { printf("Error: select() failed\n"); return; } }