cURL / Mailing Lists / curl-library / Single Mail

curl-library

Unable to use CURLOPT_OPENSOCKETDATA and CURLOPT_OPENSOCKETFUNCTION successfully

From: Sachin Nikumbh <sanikumbh_at_gmail.com>
Date: Thu, 17 Jul 2014 15:39:53 -0400

Hi,

I am trying to use CURLOPT_OPENSOCKETDATA and CURLOPT_OPENSOCKETFUNCTION
options on my debian 7 (64-bit) machine. Following is the code that I am
using:

***********************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <iostream>

#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define close closesocket
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif

#include <errno.h>

#define IPADDR "127.0.0.1"
#define PORTNUM 38319

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

static curl_socket_t opensocket(void *clientp,
                                curlsocktype purpose,
                                struct curl_sockaddr *address)
{
  sockaddr_in* clientaddr = (sockaddr_in*)clientp;

  curl_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);

  std::cout << "Socket descriptor : " << fd << std::endl;

  int status = bind(fd, (struct sockaddr *)clientaddr, sizeof(struct
sockaddr));

  if( status == -1 ){
      std::cout << "Binding to the socket failed with status : " << status
<< std::endl;
  }
  return fd;
}

void callCurl( sockaddr_in* clientaddr )
{
  CURL *curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_ALL);

  curl = curl_easy_init();

  if(curl) {
    curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

    curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, clientaddr);
    curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);

    /* get the first document */
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");

    res = curl_easy_perform(curl);

    if( res != CURLE_OK ){
        std::cout << curl_easy_strerror(res) << std::endl;
    }else{
        std::cout << "Successful" << std::endl;
    }
    curl_easy_cleanup(curl);
  }
}

int main(void)
{
  struct sockaddr_in clientaddr;

  memset(&clientaddr, 0, sizeof(sockaddr_in));
  clientaddr.sin_family = AF_INET;
  clientaddr.sin_port = htons(PORTNUM);

  if (INADDR_NONE == (clientaddr.sin_addr.s_addr = inet_addr(IPADDR))){
    std::cout << "Trouble using : " << IPADDR << std::endl;
    return 2;
  }

  callCurl( &clientaddr );
  return 0;
}
***********************************************************

The application fails with "Couldn't connect to server" error. If I comment
the calls to CURLOPT_OPENSOCKETDATA and CURLOPT_OPENSOCKETFUNCTION, it
works fine. Also, I see that opensocket callback gets called twice.

Can someone please help?

Thanks
Sachin

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-07-18