cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: can Curl use socket() created by application?

From: amit paliwal <amit.ambitions_at_gmail.com>
Date: Mon, 22 Aug 2011 10:19:24 -0400

>
> WHAT IS WRONG HERE????????????????
>>
>
> Which libcurl version on what platform is this?
>
-------------I am using curl-7.21.7 and platform is Linux

>
> Can you please submit a complete source code so that we can try to repeat
> it in our ends?

----- complete code goes like this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <sys/socket.h> /* socket definitions */
#include <sys/types.h> /* socket types */
#include <arpa/inet.h> /* inet (3) funtions */
#include <unistd.h> /* misc. UNIX functions */

#include <errno.h>

int sockfd;

curl_socket_t opensocket (void *clientp,
                            curlsocktype purpose,
                            struct curl_sockaddr *address)
{
    printf( "ECHOCLNT: socket fd: %d\n",sockfd);
    return sockfd;
}

int sockopt_callback(void *clientp, curl_socket_t curlfd, curlsocktype
purpose)
{
    printf( "Inside sockopt_callback::%d",curlfd);
      fflush(stdout);
    return CURL_SOCKOPT_ALREADY_CONNECTED;
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  long sockextr;
  size_t iolen;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "URL"); //please add ur URL here

     /* Create the listening socket */

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
    fprintf(stderr, "ECHOCLNT: Error creating listening socket.\n");
    exit(EXIT_FAILURE);
    }

    struct sockaddr_in servaddr; /* socket address structure */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8080);
 /* Set the remote IP address */

    if ( inet_aton("IP", &servaddr.sin_addr) <= 0 ) { //please add ur IP
here
    printf("ECHOCLNT: Invalid remote IP address.\n");
    exit(EXIT_FAILURE);
    }

printf( "ECHOCLNT: socket fd before connect: %d\n",sockfd);
   if (connect(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr)) ==
-1) {
            close(sockfd);
            printf("client error: connect: %s", strerror(errno));

        }

    /* no progress meter please */
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);

  /* send all data to this function */
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  curl_easy_setopt(curl, CURLOPT_URL, "URL"); //please add ur URL here

   res = curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
   printf("\nRdvr: Option CURLOPT_OPENSOCKETFUNCTION Set with status:
%s\n",curl_easy_strerror(res));
   fflush(stdout);

   res = curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
   printf("\nRdvr: Option CURLOPT_SOCKOPTFUNCTION Set with status:
%s\n",curl_easy_strerror(res));
   fflush(stdout);

   curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    res = curl_easy_perform(curl);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", strerror(res));
      return 1;
    }

   return 1;

  }

}

>
>
> --
>
> / daniel.haxx.se
> ------------------------------**------------------------------**-------
> List admin: http://cool.haxx.se/list/**listinfo/curl-library<http://cool.haxx.se/list/listinfo/curl-library>
> Etiquette: http://curl.haxx.se/mail/**etiquette.html<http://curl.haxx.se/mail/etiquette.html>
>

-- 
Regards,
Amit

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-08-22