/** ** Initialize the TCP connection to the Meta Server. ** ** @return -1 fail, 0 success. ** @todo Make a dynamic port allocation. */ int MetaInit() { const int port_range_min = 1234; const int port_range_max = 1244; for (int i = port_range_min; i < port_range_max; ++i) { MetaServerFildes = NetOpenTCP(CNetworkParameter::Instance.localHost.c_str(), i); if (MetaServerFildes != static_cast<Socket>(-1)) { if (NetConnectTCP(MetaServerFildes, NetResolveHost(MasterHost), MasterPort) != -1) { break; } MetaServerFildes = static_cast<Socket>(-1); } } if (MetaServerFildes == static_cast<Socket>(-1)) { return -1; } if (SendMetaCommand("Login", "") == -1) { return -1; } char *reply = NULL; if (RecvMetaReply(&reply) == -1) { return -1; } if (MetaServerOK(reply)) { delete[] reply; return 0; } else { delete[] reply; return -1; } }
bool CTCPSocket_Impl::Open(const CHost &host) { char ip[24]; // 127.255.255.255:65555 memset(&ip, 0, sizeof(ip)); sprintf(ip, "%d.%d.%d.%d", NIPQUAD(ntohl(host.getIp()))); this->socket = NetOpenTCP(ip, host.getPort()); return this->socket != INVALID_SOCKET; }
void SocketInterfaceInit() { int result; DebugPrint("Initializing Socket Interface.\n"); connected = false; NetInit(); listeningSocket = NetOpenTCP(SocketInterfacePort); NetSetNonBlocking(listeningSocket); result = NetListenTCP(listeningSocket); #ifdef WIN32 DebugPrint("Socket interface initialized.\nListening on port %d, result = %d, error = %d\n" _C_ SocketInterfacePort _C_ result _C_ WSAGetLastError()); #else DebugPrint("Socket interface initialized.\nListening on port %d, result = %d\n" _C_ SocketInterfacePort _C_ result); #endif }