Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Client::Connect( const char *ServerAddress, const char *Port ) {
- if( !_connected ) {
- WSADATA wsaData;
- WSAStartup( MAKEWORD(2, 2), &wsaData );
- ConnectionState = PS_DISCONNECTED;
- int nConnResult;
- ZeroMemory( &_AddrInfo.hints, sizeof( _AddrInfo.hints ) );
- _AddrInfo.hints.ai_family = AF_INET;
- _AddrInfo.hints.ai_socktype = SOCK_STREAM;
- _AddrInfo.hints.ai_protocol = IPPROTO_TCP;
- // Resolve address
- nConnResult = getaddrinfo( ServerAddress, Port, &_AddrInfo.hints, &_AddrInfo.result );
- if( nConnResult ) {
- Error = nConnResult;
- WSACleanup();
- return false;
- }
- // Create socket
- _sock = INVALID_SOCKET;
- _AddrInfo.ptr = _AddrInfo.result;
- _sock = socket( _AddrInfo.ptr->ai_family, _AddrInfo.ptr->ai_socktype, _AddrInfo.ptr->ai_protocol );
- if( _sock == INVALID_SOCKET ) {
- Error = WSAGetLastError();
- freeaddrinfo( _AddrInfo.result );
- WSACleanup();
- return false;
- }
- // Connect
- nConnResult = connect( _sock, _AddrInfo.ptr->ai_addr, static_cast< int >( _AddrInfo.ptr->ai_addrlen ) );
- if( nConnResult == SOCKET_ERROR ) {
- closesocket( _sock );
- _sock = INVALID_SOCKET;
- }
- freeaddrinfo( _AddrInfo.result );
- if( _sock == INVALID_SOCKET ) {
- Error = 0;
- WSACleanup();
- return false;
- }
- unsigned long iMode = 1;
- ioctlsocket( _sock, FIONBIO, &iMode );
- ConnectionState = PS_WAIT;
- _connected = true;
- SendPackage( PT_INIT, ( char* )&BgEdn, PNW_BUFLEN );
- return true;
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment