Adilol

Packets in C++

Jul 9th, 2011
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. //Raw socket
  2. SOCKET s = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
  3. char name[ 256 ];
  4. hostent * pHE;
  5. sockaddr_in sa;
  6. gethostbyname( name ); // get the name of localhost
  7. pHE = gethostbyname( name ); // get info about localhost inc. the local IP
  8.  
  9. ZeroMemory( &sa, sizeof( sockaddr_in ) );
  10. sa.sin_family = AF_INET;
  11. sa.sin_addr.s_addr =        // fill the struct: the IP to bind to
  12. (  (in_addr *) // cast
  13. pHE->h_addr_list[ 0 ] // the first occurence
  14.   ) -> s_addr; // it holds various info. We need the IP
  15. // then you bind() like this:
  16. bind( s, (SOCKADDR *) /* cast again */ &sa, sizeof( SOCKADDR ) )
  17. for(;; )
  18.      {
  19.          i /* how much received */ = recv( s, Buffer, sizeOfTheBuffer, 0 );
  20.          if( i <= 0 )
  21.          {
  22.              // end of the game.
  23.          }
  24.        
  25.          printf( "\n <<< A new packet has arrived >>>\n" );
  26.          for( j = 0; j < i; j ++ )
  27.          {
  28.              if( (unsigned int) Buffer[ j ] >= 32 &&
  29.                  (unsigned int) Buffer[ j ] <= 126
  30.              ) // if it's a writable char
  31.              {
  32.                  putchar( Buffer[ j ] );
  33.              }
  34.              else
  35.              {
  36.                  putchar( '?' );
  37.              }
  38.          }
  39.          printf( "\n [End of Packet]\n" );
  40.      }
Advertisement
Add Comment
Please, Sign In to add comment