Cryptography Reference
In-Depth Information
*/
int main( int argc, char *argv[ ] )
{
int client_connection;
char *host, *path;
struct hostent *host_name;
struct sockaddr_in host_address;
#ifdef WIN32
WSADATA wsaData;
#endif
if ( argc < 2 )
{
fprintf( stderr, “Usage: %s: <URL>\n”, argv[ 0 ] );
return 1;
}
if ( parse_url( argv[ 1 ], &host, &path ) == -1 )
{
fprintf( stderr, “Error - malformed URL '%s'.\n”, argv[ 1 ] );
return 1;
}
printf( “Connecting to host '%s'\n”, host );
After the URL has been parsed and the host is known, you must establish
a socket to it. In order to do this, convert it from a human-readable host name,
such as www.server.com , to a dotted-decimal IP address, such as 100.218.64.2.
You call the standard gethostbyname library function to do this, and connect
to the server. This is shown in Listing 1-4.
Listing 1-4: “http.c” main (continued)
// Step 1: open a socket connection on http port with the destination host.
#ifdef WIN32
if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != NO_ERROR )
{
fprintf( stderr, “Error, unable to initialize winsock.\n” );
return 2;
}
#endif
client_connection = socket( PF_INET, SOCK_STREAM, 0 );
if ( !client_connection )
{
perror( “Unable to create local socket” );
return 2;
}
host_name = gethostbyname( host );
if ( !host_name )
 
Search WWH ::




Custom Search