코드 3-11 TCP 서버 루프 구동하기
void DoTCPLoop() { TCPSocketPtr listenSocket = SocketUtil::CreateTCPSocket(INET); SocketAddress receivingAddres(INADDR_ANY, 48000); if (listenSocket->Bind(receivingAddres) != NO_ERROR) return; vector<TCPSocketPtr> readBlockSockets; readBlockSockets.push_back(listenSocket); vector<TCPSocketPtr> readableSockets; while (gIsGameRunning) { if (!SocketUtil::Select(&readBlockSockets, &readableSockets, nullptr, nullptr, nullptr, nullptr)) continue; // readableSockets에 받은 패킷을 하나씩 순회한다. for (const TCPSocketPtr& socket : readableSockets) { if (socket == listenSocket) { // 리스닝 소켓에서 새 연결을 받는다. SocketAddress newClientAddress; auto newSocket = listenSocket->Accept(newClientAddress); readBlockSockets.push_back(newSocket); ProcessNewClient(newSocket, newClientAddress); } else { // 일반 소켓이므로 데이터를 수신한다. char segment[GOOD_SEGMENT_SIZE]; int dataReceived = socket->Receive( segment, GOOD_SEGMENT_SIZE ); if(dataReceived > 0) ProcessDataFromClient(socket, segment, dataReceived); } } } }