MyTcpSocket* MyTcpSocket::acceptClient(string& clientHostName) { int newSocket; // the new socket file descriptor returned by the accept systme call // the length of the client's address struct sockaddr_in clientAddress; // Address of the client that sent data int clientAddressLen = sizeof(struct sockaddr_in); // Accepts a new client connection and stores its socket file descriptor try { if ((newSocket = accept(m_nSocketId, (struct sockaddr *)&clientAddress,&clientAddressLen)) == -1) { #ifdef WINDOWS_XP int errorCode = 0; string errorMsg = "error calling accept(): \n"; detectErrorAccept(&errorCode,errorMsg); MyException socketAcceptException(errorCode,errorMsg); throw socketAcceptException; #endif #ifdef UNIX MyException unixSocketAcceptException(0,"unix: error calling accept()"); throw unixSocketAcceptException; #endif } } catch(MyException& excp) { excp.response(); return NULL; } // Get the host name given the address char *sAddress = inet_ntoa((struct in_addr)clientAddress.sin_addr); /*******MyHostInfo from ADDRESS**********/ MyHostInfo clientInfo(string(sAddress),ADDRESS); char* hostName = clientInfo.getHostName(); clientHostName+= string(hostName); // Create and return the new MyTcpSocket object MyTcpSocket* retSocket = new MyTcpSocket(); //保存与客户端通信的句柄 retSocket->setSocketId(newSocket); return retSocket; }