libcurl program no output

This is a discussion on libcurl program no output within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I wrote the following code:::::::::: Code: #include "stdio.h" #include "curl/curl.h" int main () { CURL *curl=NULL; CURLcode res; curl=curl_easy_init(); ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    14

    libcurl program no output

    Hi,
    I wrote the following code::::::::::
    Code:
    #include "stdio.h"
    #include "curl/curl.h"
    
    int main ()
    {
    CURL *curl=NULL;
    CURLcode res;
    
    curl=curl_easy_init();
    
    if(curl!=NULL)
    {
    curl_easy_setopt(curl,CURLOPT_URL,"http://www.google.com");
    res=curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    }
    
    else{printf(" error\n%d",res);}
    return 0;
    }
    compiled it using:::
    gcc -o exp1 exp1.c -lcurl
    everything worked out fine...

    but when i m executing the program...
    i m getting no output...
    plz help me...
    urgent....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you need to set a write-function? I'm not familiar with curl, but it seems like you are "missing something", and it seems that from libcurl tutorial that a "write function" is what you are missing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    14
    I downloaded the source code::::
    Code:
    /*
    
    sample for O'ReillyNet article on libcURL:
    	{TITLE}
    	{URL}
    	AUTHOR: Ethan McCallum
    
    Scenario: use http/GET to fetch a webpage
    
    
    This code was built/tested under Fedora Core 3,
    libcURL version 7.12.3.  As libcURL is under 
    active development, features may be added or
    removed between releases.  Be sure to check your
    library version, and its corresponding documentation,
    if these examples fail to build on your system.
    
    */
    
    #include<iostream>
    
    extern "C" {
    	#include<curl/curl.h>
    }
    
    // - - - - - - - - - - - - - - - - - - - -
    
    enum {
    	ERROR_ARGS = 1 ,
    	ERROR_CURL_INIT = 2
    } ;
    
    enum {
    	OPTION_FALSE = 0 ,
    	OPTION_TRUE = 1
    } ;
    
    enum {
    	FLAG_DEFAULT = 0 
    } ;
    
    // - - - - - - - - - - - - - - - - - - - -
    
    
    int main( const int argc , const char** argv ){
    
    	if( argc != 2 ){
    		std::cerr << "test of libcURL: fetch data via HTTP." << std::endl ;
    		std::cerr << "This sends header data to stderr," << std::endl ;
    		std::cerr << "and body data to stdout." << std::endl ;
    		std::cerr << " Usage: " << argv[0] << " {url} [debug]" << std::endl ;
    		return( ERROR_ARGS ) ;
    	}
    
    	const char* url = argv[1] ;
    
    	// global libcURL init
    	curl_global_init( CURL_GLOBAL_ALL ) ;
    
    	// create a context, sometimes known as a handle.
    	// Think of it as a lookup table, or a source of config data.
    	CURL* ctx = curl_easy_init() ;
    
    	if( NULL == ctx ){
    		std::cerr << "Unable to initialize cURL interface" << std::endl ;
    		return( ERROR_CURL_INIT ) ;
    	}
    
    	/* BEGIN: configure the handle: */
    
    	// handy for debugging: see *everything* that goes on
    	// curl_easy_setopt( ctx , CURLOPT_VERBOSE, OPTION_TRUE ) ;
    
    	// target url:
    	curl_easy_setopt( ctx , CURLOPT_URL,  url ) ;
    
    	// no progress bar:
    	curl_easy_setopt( ctx , CURLOPT_NOPROGRESS , OPTION_TRUE ) ;
    
    	/*
    	// for curl 7.11 and earlier
    	char errorBuf[ CURL_ERROR_SIZE ] ;
    	std::fill( errorBuf , errorBuf + CURL_ERROR_SIZE , '\0' ) ;
    	curl_easy_setopt( ctx , CURLOPT_ERRORBUFFER , errorBuf ) ;
    	*/
    
    
    	// what to do with returned data
    
    	// received headers:
    	/*
    	By default, headers are stripped from the output.
    	They can be:
    
    	- passed through a separate FILE* (CURLOPT_WRITEHEADER)
    
    	- included in the body's output (CURLOPT_HEADER -> nonzero value)
    		(here, the headers will be passed to whatever function
    		 processes the body, along w/ the body)
    
    	- handled with separate callbacks (CURLOPT_HEADERFUNCTION)
    		(in this case, set CURLOPT_WRITEHEADER to a
    		 matching struct for the function)
    
    	*/
    	
    	// (sending response headers to stderr)
    	curl_easy_setopt( ctx , CURLOPT_WRITEHEADER , stderr ) ;
    
    
    	// response content: same choices as headers
    	// send it to stdout to prove that libcURL differentiates the two
    	curl_easy_setopt( ctx , CURLOPT_WRITEDATA , stdout ) ;
    
    	/* END: configure the handle */
    
    
    	// action!
    
    	const CURLcode rc = curl_easy_perform( ctx ) ;
    
    	// for curl v7.11.x and earlier, look into
    	// curl_easy_setopt( ctx , CURLOPT_ERRORBUFFER , /* char array */ ) ;
    	if( CURLE_OK != rc ){
    
    		std::cerr << "Error from cURL: " << curl_easy_strerror( rc ) << std::endl ;
    
    	}else{
    
    		// get some info about the xfer:
    		double statDouble ;
    		long statLong ;
    		char* statString = NULL ;
    
    		// known as CURLINFO_RESPONSE_CODE in later curl versions
    		if( CURLE_OK == curl_easy_getinfo( ctx , CURLINFO_HTTP_CODE , &statLong ) ){
    			std::cout << "Response code:  " << statLong << std::endl ;
    		}
    
    		if( CURLE_OK == curl_easy_getinfo( ctx , CURLINFO_CONTENT_TYPE , &statString ) ){
    			std::cout << "Content type:   " << statString << std::endl ;
    		}
    
    		if( CURLE_OK == curl_easy_getinfo( ctx , CURLINFO_SIZE_DOWNLOAD , &statDouble ) ){
    			std::cout << "Download size:  " << statDouble << "bytes" << std::endl ;
    		}
    
    		if( CURLE_OK == curl_easy_getinfo( ctx , CURLINFO_SPEED_DOWNLOAD , &statDouble ) ){
    			std::cout << "Download speed: " << statDouble << "bytes/sec" << std::endl ;
    		}
    
    	}
    
    	// cleanup
    	curl_easy_cleanup( ctx ) ;
    	curl_global_cleanup() ;
    
    	return( 0 ) ;
    
    } // main()
    It gives the following output:::

    Error from cURL: couldn't resolve host name
    What should i do now???
    plz help....
    Last edited by shady_Dev; 01-02-2008 at 11:25 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Well did you supply a command line parameter?

    ./myprog http://www.google.com

    Works for me
    Code:
    $ g++ foo.cpp -lcurl
    $ ./a.exe http://www.google.com
    HTTP/1.1 302 Found
    Location: http://www.google.co.uk/
    <<snipped for brevity>>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    14
    I supplied the same url u gave....
    But the output was:::
    Error from cURL: couldn't resolve host name
    Although i tried it using my system as webserver[by running httpd]..
    and provivded the url "128.0.3.16" ie my pc 's ip....
    it worked well.....
    Although still it could not resolved the "http://localhost" as url....
    i dont know wats happening here...
    plz help

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Seems like cURL can't handle your DNS system.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 09:58 AM
  2. Unusual program Output, Help
    By capvirgo in forum C Programming
    Replies: 8
    Last Post: 02-06-2008, 02:13 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. Program Output
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 09:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21