cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Mapping Linux socket API's to libcurl API's

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Wed, 14 Sep 2011 10:37:44 +0200 (CEST)

On Wed, 14 Sep 2011, swati mishra wrote:

> I need to know how I can map linux socket API's to libcurl API's

There's no 1:1 mapping like that. libcurl provides a more high level API than
the socket API to abstract transfers more.

When you use libcurl in your application you're better off trying to detach
your transfer from thinking about sockets and socket operations. libcurl takes
care of all the socket operations if you just tell it what to get or send.

A good first glance on how libcurl works is here:
   http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

and you can of course check out the examples:
   http://curl.haxx.se/libcurl/c/example.html

> 1.) Create(Callback_s *callback)    {      s = socket( PF_INET, SOCK_STREAM,
> 0)    }

If you can't allow libcurl to create its own socket, you can use the
CURLOPT_OPENSOCKETFUNCTION to feed libcurl your socket:

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTOPENSOCKETFUNCTION

> 2.) Connect( void *h, Scheme_e scheme/*HTTP or HTTPS*/, const char *addr, unsigned         short PortNum, unsigned long TimeOut)    {          struct sockaddr_in sin;  SOCKET s;
>        connect( s, (struct sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)}

libcurl prefers to connect to the given host by itself. If you necessarily
need to do the connect yourself, you should do it before you invoke libcurl
and pass in the external socket and tell libcurl its already connected.

You can get some idea of how it can be done by reading this example:
http://curl.haxx.se/libcurl/c/externalsocket.html

> 3.) Send( void *h, char *SendBuf, unsigned long SendLen, unsigned long
> timeOut)     {      SSL_write(ssl, SendBuf, SendLen )     OR    send(socket,
> SendBuf, SendLen, 0);   }
> 4.) Recv( void *h, char* RecvBuf, unsigned long *RecvLen, unsigned long timeout)    {       SSL_read(ssl, RecvBuf, TargetSize )       OR       recv(socket, RecvBuf, TargetSize, 0);}
> rgds,Swati

This is *really* something you should let libcurl do on its own. You use
libcurl because you want to use libcurl's abilities to speak a particular
protocol. If you don't want to use libcurl to speak one or more of those
protocols that it knows, but instead you do your own send()s then perhaps you
should reconsider your choice to use libcurl to begin with.

This said, you _can_ tell curl_easy_perform() to only do the connect by
setting the CURLOPT_CONNECT_ONLY option and after that you can use
curl_easy_send() and curl_easy_recv() to send data over the socket - or even
just use send() and recv() once you know the underlying real socket.

But again, I strongly recommend against using this approach.

-- 
  / daniel.haxx.se

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