12-12-2011, 03:32 PM
Hi,
This is Khushi, i'm trying with a sample code to get header separately into a file for continous streaming urls. but am not to get it.. both the header and data information are present in the body.out file.
I'm pasting my sample code here..
##
FILE *bodyfile;
FILE *headerfile;
CURL *curl_handle;
int responsebytes_written = 0;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
responsebytes_written = fwrite(ptr, size, nmemb, (FILE *)stream);
printf("bytes written %d\n", responsebytes_written);
return responsebytes_written;
}
static size_t write_header(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
printf("bytes written in header %d\n", written);
return written;
}
static size_t progressfun(void *ptr, size_t size, size_t nmemb, void *stream)
{
CURL *curl_handle1;
int ret = 0;
static int progresscnt = 0;
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
//if(responsebytes_written == 0)
{
progresscnt++;
}
/* else
{
responsebytes_written = 0;
}*/
printf("progress fun %d %d\n", written, progresscnt);
#if 0
if(progresscnt == 10)
{
printf("Interface has gone down!!!!!!!!!!!!!!!!!!\n");
#if 1
// curl_easy_cleanup(curl_handle);
curl_handle1 = curl_easy_init();
curl_easy_setopt(curl_handle1, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl_handle1, CURLOPT_INTERFACE, "em1");
//curl_easy_setopt(curl_handle1, CURLOPT_FRESH_CONNECT, "eth0");
printf("handle is %d\n", curl_handle);
curl_easy_setopt(curl_handle1, CURLOPT_URL,"http://www.google.co.in");
//curl_easy_setopt(curl_handle1, CURLOPT_URL,"http://scfire-dtc-aa03.stream.aol.com:80/stream/1010");
curl_easy_setopt(curl_handle1,CURLOPT_WRITEHEADER, headerfile);
/* we want the headers to this file handle */
curl_easy_setopt(curl_handle1, CURLOPT_HEADERFUNCTION, write_header);
curl_easy_setopt(curl_handle1, CURLOPT_WRITEDATA, bodyfile);
/* send all data to this function */
curl_easy_setopt(curl_handle1, CURLOPT_WRITEFUNCTION, write_data);
#if 1
curl_easy_setopt(curl_handle1, CURLOPT_PROXY, "proxy.tcs.com:8080");
curl_easy_setopt(curl_handle1, CURLOPT_PROXYUSERNAME , "522528");
curl_easy_setopt(curl_handle1, CURLOPT_PROXYPASSWORD , "Ggupsp@2911");
#endif
curl_easy_setopt(curl_handle1, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl_handle1, CURLOPT_PROGRESSFUNCTION, progressfun);
printf("before easy perform handle1\n");
ret = curl_easy_perform(curl_handle1);
printf("after easy perform handle1 %d\n", ret);
#endif
progresscnt = 31;
}
#endif
return written;
}
//curl_sockopt_callback fsockcb;
int fsockcb (void *clientp,curl_socket_t curlfd,curlsocktype purpose)
{
int ret =0 ;
printf("socket id is %d\n", curlfd);
if(ret = setsockopt(curlfd, SOL_SOCKET, SO_BINDTODEVICE,
"em1", (curl_socklen_t)(strlen(("em1")+1))) != 0) {
// error = SOCKERRNO;
}
printf("am hereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee %d\n", errno);
return 0;
}
int main(void)
{
static const char *headerfilename = "head.out";
static const char *bodyfilename = "body.out";
int ret = 0;
/* open the files */
headerfile = fopen(headerfilename,"w");
if (headerfile == NULL) {
// curl_easy_cleanup(curl_handle);
printf("headerfile open failed\n");
return -1;
}
bodyfile = fopen(bodyfilename,"w");
if (bodyfile == NULL) {
// curl_easy_cleanup(curl_handle);
printf("bodyfile open failed\n");
return -1;
}
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
//curl_easy_setopt(curl_handle, CURLOPT_INTERFACE, "em1");
// curl_easy_setopt(curl_handle, CURLOPT_INTERFACE, "172.19.94.192");
// curl_easy_setopt(curl_handle, CURLOPT_SOCKOPTFUNCTION,&fsockcb);
//curl_easy_setopt(curl_handle, CURLOPT_URL,"74.125.236.81");
curl_easy_setopt(curl_handle, CURLOPT_URL,"http://91.121.155.39:80");
// curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL,0);
// curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT,35);
/* no progress meter please */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, progressfun);
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
/* we want the headers to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, write_header);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
#if 1
//curl_easy_setopt(curl_handle, CURLOPT_PROXY, "proxy.tcs.com:8080");
curl_easy_setopt(curl_handle, CURLOPT_PROXY, "172.19.193.33:8080");
curl_easy_setopt(curl_handle, CURLOPT_PROXYUSERNAME , "INDIA\\522528");
curl_easy_setopt(curl_handle, CURLOPT_PROXYPASSWORD , "Ggupsp@2911");
#endif
/*
* Notice here that if you want the actual data sent anywhere else but
* stdout, you should consider using the CURLOPT_WRITEDATA option. */
/* get it! */
printf("before main easy perform\n");
ret = curl_easy_perform(curl_handle);
printf("after main easy perform %d %d\n", ret, errno);
/* close the header file */
fclose(headerfile);
fclose(bodyfile);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}
Please do go through and help me in finding a solutions..thanks in advance...
waiting for responses..
This is Khushi, i'm trying with a sample code to get header separately into a file for continous streaming urls. but am not to get it.. both the header and data information are present in the body.out file.
I'm pasting my sample code here..
##
FILE *bodyfile;
FILE *headerfile;
CURL *curl_handle;
int responsebytes_written = 0;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
responsebytes_written = fwrite(ptr, size, nmemb, (FILE *)stream);
printf("bytes written %d\n", responsebytes_written);
return responsebytes_written;
}
static size_t write_header(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
printf("bytes written in header %d\n", written);
return written;
}
static size_t progressfun(void *ptr, size_t size, size_t nmemb, void *stream)
{
CURL *curl_handle1;
int ret = 0;
static int progresscnt = 0;
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
//if(responsebytes_written == 0)
{
progresscnt++;
}
/* else
{
responsebytes_written = 0;
}*/
printf("progress fun %d %d\n", written, progresscnt);
#if 0
if(progresscnt == 10)
{
printf("Interface has gone down!!!!!!!!!!!!!!!!!!\n");
#if 1
// curl_easy_cleanup(curl_handle);
curl_handle1 = curl_easy_init();
curl_easy_setopt(curl_handle1, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl_handle1, CURLOPT_INTERFACE, "em1");
//curl_easy_setopt(curl_handle1, CURLOPT_FRESH_CONNECT, "eth0");
printf("handle is %d\n", curl_handle);
curl_easy_setopt(curl_handle1, CURLOPT_URL,"http://www.google.co.in");
//curl_easy_setopt(curl_handle1, CURLOPT_URL,"http://scfire-dtc-aa03.stream.aol.com:80/stream/1010");
curl_easy_setopt(curl_handle1,CURLOPT_WRITEHEADER, headerfile);
/* we want the headers to this file handle */
curl_easy_setopt(curl_handle1, CURLOPT_HEADERFUNCTION, write_header);
curl_easy_setopt(curl_handle1, CURLOPT_WRITEDATA, bodyfile);
/* send all data to this function */
curl_easy_setopt(curl_handle1, CURLOPT_WRITEFUNCTION, write_data);
#if 1
curl_easy_setopt(curl_handle1, CURLOPT_PROXY, "proxy.tcs.com:8080");
curl_easy_setopt(curl_handle1, CURLOPT_PROXYUSERNAME , "522528");
curl_easy_setopt(curl_handle1, CURLOPT_PROXYPASSWORD , "Ggupsp@2911");
#endif
curl_easy_setopt(curl_handle1, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl_handle1, CURLOPT_PROGRESSFUNCTION, progressfun);
printf("before easy perform handle1\n");
ret = curl_easy_perform(curl_handle1);
printf("after easy perform handle1 %d\n", ret);
#endif
progresscnt = 31;
}
#endif
return written;
}
//curl_sockopt_callback fsockcb;
int fsockcb (void *clientp,curl_socket_t curlfd,curlsocktype purpose)
{
int ret =0 ;
printf("socket id is %d\n", curlfd);
if(ret = setsockopt(curlfd, SOL_SOCKET, SO_BINDTODEVICE,
"em1", (curl_socklen_t)(strlen(("em1")+1))) != 0) {
// error = SOCKERRNO;
}
printf("am hereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee %d\n", errno);
return 0;
}
int main(void)
{
static const char *headerfilename = "head.out";
static const char *bodyfilename = "body.out";
int ret = 0;
/* open the files */
headerfile = fopen(headerfilename,"w");
if (headerfile == NULL) {
// curl_easy_cleanup(curl_handle);
printf("headerfile open failed\n");
return -1;
}
bodyfile = fopen(bodyfilename,"w");
if (bodyfile == NULL) {
// curl_easy_cleanup(curl_handle);
printf("bodyfile open failed\n");
return -1;
}
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
//curl_easy_setopt(curl_handle, CURLOPT_INTERFACE, "em1");
// curl_easy_setopt(curl_handle, CURLOPT_INTERFACE, "172.19.94.192");
// curl_easy_setopt(curl_handle, CURLOPT_SOCKOPTFUNCTION,&fsockcb);
//curl_easy_setopt(curl_handle, CURLOPT_URL,"74.125.236.81");
curl_easy_setopt(curl_handle, CURLOPT_URL,"http://91.121.155.39:80");
// curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL,0);
// curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT,35);
/* no progress meter please */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, progressfun);
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
/* we want the headers to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, write_header);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
#if 1
//curl_easy_setopt(curl_handle, CURLOPT_PROXY, "proxy.tcs.com:8080");
curl_easy_setopt(curl_handle, CURLOPT_PROXY, "172.19.193.33:8080");
curl_easy_setopt(curl_handle, CURLOPT_PROXYUSERNAME , "INDIA\\522528");
curl_easy_setopt(curl_handle, CURLOPT_PROXYPASSWORD , "Ggupsp@2911");
#endif
/*
* Notice here that if you want the actual data sent anywhere else but
* stdout, you should consider using the CURLOPT_WRITEDATA option. */
/* get it! */
printf("before main easy perform\n");
ret = curl_easy_perform(curl_handle);
printf("after main easy perform %d %d\n", ret, errno);
/* close the header file */
fclose(headerfile);
fclose(bodyfile);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}
Please do go through and help me in finding a solutions..thanks in advance...
waiting for responses..