BugInTheSYS

Untitled

Oct 25th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. bool Client::Connect( const char *ServerAddress, const char *Port ) {
  2.   if( !_connected ) {
  3.     WSADATA wsaData;
  4.     WSAStartup( MAKEWORD(2, 2), &wsaData );
  5.     ConnectionState = PS_DISCONNECTED;
  6.  
  7.     int nConnResult;
  8.  
  9.     ZeroMemory( &_AddrInfo.hints, sizeof( _AddrInfo.hints ) );
  10.     _AddrInfo.hints.ai_family = AF_INET;
  11.     _AddrInfo.hints.ai_socktype = SOCK_STREAM;
  12.     _AddrInfo.hints.ai_protocol = IPPROTO_TCP;
  13.  
  14.     // Resolve address
  15.     nConnResult = getaddrinfo( ServerAddress, Port, &_AddrInfo.hints, &_AddrInfo.result );
  16.     if( nConnResult ) {
  17.       Error = nConnResult;
  18.       WSACleanup();
  19.       return false;
  20.     }
  21.  
  22.     // Create socket
  23.     _sock = INVALID_SOCKET;
  24.     _AddrInfo.ptr = _AddrInfo.result;
  25.     _sock = socket( _AddrInfo.ptr->ai_family, _AddrInfo.ptr->ai_socktype, _AddrInfo.ptr->ai_protocol );
  26.     if( _sock == INVALID_SOCKET ) {
  27.       Error = WSAGetLastError();
  28.       freeaddrinfo( _AddrInfo.result );
  29.       WSACleanup();
  30.       return false;
  31.     }
  32.  
  33.     // Connect
  34.     nConnResult = connect( _sock, _AddrInfo.ptr->ai_addr, static_cast< int >( _AddrInfo.ptr->ai_addrlen ) );
  35.     if( nConnResult == SOCKET_ERROR ) {
  36.       closesocket( _sock );
  37.       _sock = INVALID_SOCKET;
  38.     }
  39.     freeaddrinfo( _AddrInfo.result );
  40.     if( _sock == INVALID_SOCKET ) {
  41.       Error = 0;
  42.       WSACleanup();
  43.       return false;
  44.     }
  45.    
  46.     unsigned long iMode = 1;
  47.     ioctlsocket( _sock, FIONBIO, &iMode );
  48.  
  49.     ConnectionState = PS_WAIT;
  50.     _connected = true;
  51.  
  52.     SendPackage( PT_INIT, ( char* )&BgEdn, PNW_BUFLEN );
  53.  
  54.     return true;
  55.   } else {
  56.     return false;
  57.   }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment