/* connect to site/port */ int Connect (char *hostname, int port) { struct hostent *remote; unsigned long addr; if (strspn (hostname, " .0123456789") == strlen (hostname)) { addr = inet_addr (hostname); return ConnectIP (addr, port); } else { /* resolve hostname */ remote = gethostbyname (hostname); if (remote == NULL) { /* warning1 ("cannot resolve %s\n", hostname); */ return -1; } return ConnectIP (*((unsigned long *)(remote->h_addr)), port); } }
bool TCPConnection::Connect(const char* irAddress, uint16 irPort, char* errbuf) { if (errbuf) errbuf[0] = 0; uint32 tmpIP = ResolveIP(irAddress); if (!tmpIP) { if (errbuf) { #ifdef _WINDOWS snprintf(errbuf, TCPConnection_ErrorBufferSize, "TCPConnection::Connect(): Couldnt resolve hostname. Error: %i", WSAGetLastError()); #else snprintf(errbuf, TCPConnection_ErrorBufferSize, "TCPConnection::Connect(): Couldnt resolve hostname. Error #%i: %s", errno, strerror(errno)); #endif } return false; } return ConnectIP(tmpIP, irPort, errbuf); }
/* This is always called from an IO thread. Either the server socket's thread, or a * special thread we create when we make an outbound connection. */ bool TCPConnection::Process() { char errbuf[TCPConnection_ErrorBufferSize]; switch(GetState()) { case TCPS_Ready: case TCPS_Connecting: if (ConnectionType == Outgoing) { if (GetAsyncConnect()) { if (charAsyncConnect) rIP = ResolveIP(charAsyncConnect); ConnectIP(rIP, rPort); } } return(true); case TCPS_Connected: // only receive data in the connected state, no others... if (!RecvData(errbuf)) { struct in_addr in; in.s_addr = GetrIP(); //cout << inet_ntoa(in) << ":" << GetrPort() << ": " << errbuf << endl; return false; } /* we break to do the send */ break; case TCPS_Disconnecting: { //waiting for any sending data to go out... MSendQueue.lock(); if(sendbuf) { if(sendbuf_used > 0) { //something left to send, keep processing... MSendQueue.unlock(); break; } //else, send buffer is empty. safe_delete_array(sendbuf); } //else, no send buffer, we are done. MSendQueue.unlock(); } /* Fallthrough */ case TCPS_Disconnected: FinishDisconnect(); MRunLoop.lock(); pRunLoop = false; MRunLoop.unlock(); // SetState(TCPS_Ready); //reset the state in case they want to use it again... return(false); case TCPS_Closing: //I dont understand this state... case TCPS_Error: MRunLoop.lock(); pRunLoop = false; MRunLoop.unlock(); return(false); } /* we get here in connected or disconnecting with more data to send */ bool sent_something = false; if (!SendData(sent_something, errbuf)) { struct in_addr in; in.s_addr = GetrIP(); std::cout << inet_ntoa(in) << ":" << GetrPort() << ": " << errbuf << std::endl; return false; } return true; }