Want to establish TCP/IP connection and send/receive data to server

Hi,

Currently I am using socket programming to send and receive data

I want to use TCP/IP methods i.e. HTTP methods to send and receive data to server.

Is there standard methods available on linux environment or any available api service in legato for HTTP connection and send/Receive ?

Kindly let me know about it.

libcurl is often used in C-based programs for Linux that need to make HTTP requests. I believe libcurl is included as part of the toolchain distributed with Legato.

Hello @dfrey
Thank you for solution.
I have used curl in my project for http send and receive.
In code, “curl/curl.h” and “curl/easy.h” are included. But still reference error has occurred to all following apiswhile building project:

undefined reference to curl_easy_cleanup' undefined reference tocurl_easy_init’
undefined reference to curl_easy_perform' undefined reference tocurl_easy_setopt’
undefined reference to curl_easy_strerror' undefined reference tocurl_global_cleanup’

console logs:

Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_easy_setopt
Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_global_cleanup
Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_easy_init
Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_easy_perform
Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_easy_cleanup
Target_Legato_Debug/staging/read-only/lib/libComponent_CurlBasedProjectComponent.so: undefined reference to curl_easy_strerror

How can I solve this error?

Hi,

The above issue related to ‘undefined reference’ get resolved.
Now I have used curl methods to send data on server link.
But there is one error at curl_easy_perform(curl) : could not resolve host name

My aim is to send Url with GET method so as to receive response of sent link.
along with link, there is some data input to update on server and response is sent back from server.

Code flow:

  • connected to gprs using le_data api and connection handler
  • when gprs get connected, curl is initialized and following curl methods :

static const char * Url = “https://httpbin.org/get”;
curl_global_init(CURL_GLOBAL_NOTHING);
curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL,Url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, PrintCallback);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();

I have also checked by adding “htttp://www.google.com”, but same error exist
Kindly help me to solve issue

Is your app sandboxed? I have seen a problem in the past where hostname lookup fails because the sandboxed app doesn’t see /etc/resolv.conf

Thank you so much @dfrey
Now issue get resolved when i added sandboxed:false
But what does sandbox mean in this developer studio actually ?

Setting sandboxing to false isn’t a great solution because it means that your app runs as root and thus any vulnerabilities in your app could lead to a total system takeover. An alternate solution is to bundle an /etc/resolv.conf into the app. I have used this before and listed google DNS servers (eg 8.8.8.8) in the file so that host resolution is likely to work regardless of how you are connected to the internet. Another possible solution is to use “requires file” function of legato to map resolv.conf into the sandbox. I found that this didn’t work the last time I tried because the resolv.conf file is modified when the data connection is established and the modifications aren’t properly reflected inside the sandbox. Maybe give it a try and see if that bug has been fixed though…

Hi @dfrey

How to bundle an /etc/resolv.conf into app ?

Here’s an example: http://legato.io/legato-docs/latest/defFilesAdef.html#defFilesAdef_bundles

Hope the app is sandboxed. So you need to give access to some of the libraries in .adef:
file:
{

// needed for networking:
/lib/libnss_compat.so.2 /lib/
/lib/libnss_files.so.2 /lib/
/lib/libnss_dns.so.2 /lib/
/lib/libresolv.so.2 /lib/
/etc/nsswitch.conf /etc/
/etc/hosts /etc/
/etc/resolv.conf /etc/

}