bool CSocket::ReadMessage(string & msg) const { if(m_socket == -1) { //bad socket throw CSocketError(); } int msglen; int numread = recv(m_socket, &msglen, sizeof(msglen), MSG_NOSIGNAL); if(numread == -1 && errno == EWOULDBLOCK) return false; if(numread <= 0) { //bad socket throw CSocketError(); } char * buffer = new char[msglen+1]; do { numread = recv(m_socket, buffer, msglen, MSG_NOSIGNAL); } while(numread == -1 && errno == EWOULDBLOCK); if(numread != msglen) { //bad packet delete[] buffer; throw CSocketError(); } buffer[numread] = '\0'; msg = buffer; delete[] buffer; return true; }
void CSocket::SendMessage(const string & msg) const { if(m_socket == -1) { //bad socket throw CSocketError(); } int len = msg.length(); int numsend; numsend = send(m_socket, &len, sizeof(len), MSG_NOSIGNAL); if(numsend == -1 && errno != EWOULDBLOCK) throw CSocketError(); numsend = send(m_socket, msg.c_str(), len, MSG_NOSIGNAL); if(numsend == -1 && errno != EWOULDBLOCK) throw CSocketError(); }
void CSocketHTTPDataSource::SendHelper(const char *pzRequest, int nRequestLen, GString &destinationStream, const char *pzNamespace) { int fd, numbytes; #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD( 1, 0 ), &wsaData); #endif int nNamespaceLen = (int)strlen(pzNamespace); struct sockaddr_in their_addr; their_addr.sin_family = AF_INET; their_addr.sin_port = htons(m_nPort); their_addr.sin_addr.s_addr = inet_addr (GetServerAddress()); if (their_addr.sin_addr.s_addr == -1) { // resolve a DNS server name if inet_addr() came up empty. struct hostent *pHE = (struct hostent *)gethostbyname(GetServerAddress()); if (pHE == 0) { GString strError; strError.Format("gethostbyname() failed to resolve[%s]",GetServerAddress()); throw CSocketError(errno,(const char *)strError); } memcpy((char *)&(their_addr.sin_addr), pHE->h_addr,pHE->h_length); } memset(&(their_addr.sin_zero),0, 8);//zero the rest of the struct if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { throw CSocketError(0,"socket"); } CSocketWrap sockfd(fd); // local bind() is not required on WIN32 platform struct sockaddr_in localAddr; localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0); int rc = bind(fd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) { GString strError; strError.Format("failed to bind([%s]:%d)",GetServerAddress(),(int)m_nPort); throw CSocketError(errno,(const char *)strError); } if (connect(fd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { GString strError; strError.Format("failed to connect([%s]:%d)",GetServerAddress(),(int)m_nPort); throw CSocketError(errno,(const char *)strError); } // 200 bytes in excess char *pSendBuf = new char[200 + nRequestLen + nNamespaceLen]; sprintf(pSendBuf,"POST /XMLServer/UBT.dll?%s HTTP/1.1\r\nUser-Agent: XML_Object_Framework_DataSource\r\nHost: 100.100.100.100\r\nContent-Length: ",pzNamespace); GString strTemp; strTemp.Format("%d\r\n\r\n%s",(int)nNamespaceLen + nRequestLen,pzNamespace); strcat(&pSendBuf[120],(const char *)strTemp); int nHeaderLen = (int)strlen(&pSendBuf[120]) + 120; strcat(&pSendBuf[120],pzRequest); int nTotalMessageLength = nHeaderLen + nRequestLen; if ((numbytes=sendto(sockfd, pSendBuf, nTotalMessageLength, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { delete[] pSendBuf; throw CSocketError(sockfd,"sendto"); } delete[] pSendBuf; int nCumulativeBytes = 0; numbytes = 0; do { if ((numbytes=recv(sockfd, m_winsockBuffer, sizeof(m_winsockBuffer), 0)) == -1) { continue; } destinationStream.write(m_winsockBuffer,numbytes); nCumulativeBytes += numbytes; }while( numbytes != 0 ); }