Running C code in erlang - c

How to run c code in Erlang? Please explain with simple example.
Basically I am trying to post data on a server with this.I will trigger the C code through Erlang and then this C code will post data to server.

This question comes back very often: one of the best answers is here
I would also add ports to the mix. Do not confuse them with port drivers: ports vs port drivers
For your use case ports should be the best option. Nifs can crash Erlang VM, so they are only for optimizing critical code. Port drivers have similar problems. C nodes mimic Erlang VM and I haven't seen good use case for them yet.
Ports are just normal OS processes with a protocol used to communicate with Erlang VM. This is usually the default way to go, when you want to call some C code from Erlang.

Related

ODBC drivers run in kernel space or user space? Why?

I'm studying databases now and I'd like to know if ODBC drivers really run in kernel space. Wouldn't it be easier just to use sockets for client connections? The only reasonable explanation I can think is that it would be performance issue with sockets. Or there are other reasons?
ODBC isn't a protocol! It's an API. ODBC isn't about how to talk with DBMS. It's about how to talk with a thing that know how to talk with DBMS. That thing is a C library. It's called "DBMS-specific library" or "ODBC-driver". ODBC specifies what C functions (and names of this functions) a library must have so that can be used as an ODBC-driver. ODBC-driver implements DBMS-specific protocol. It can use whatever it wants to talk with DBMS: sockets, pipes, shared-memory, pigeons, whatever
Also there is an interesting thing called "ODBC-ODBC bridge". It allows you to connect to DBMS remotely without having locally installed DBMS-specific library. Similar to a protocol, isn't it?
User-space driver is pretty common thing. So it's not weird that ODBC-libraries are called "drivers". It's very convenient to implement all logic in user-space and use kernel-space only to send data to device.

Migrating particular TCP Connection using CRIU tools

Is it possible to migrate a single and particular TCP connection inside a running process in one machine to another machine using CRIU tools in Linux?
What I want is to dump a particular TCP Connection information in a memory and transfer this information to a peer machine. Inside this machine, I will use the dumped information to recreate the the migrated TCP connection. Does anyone have an example or tutorial in c language?
I am aware about different solutions like SockMi which provides Kernel Module + User Space APIs to migrate a certain TCP Socket. However, I want to use CRIU tools since it is part of Linux Mainline.
Right now we only have the TCP migration functionality integrated into CRIU tool. It sits in the sk-tcp.c file, the whole TCP-repair code is there, though it's bound to the rest of CRIU.
On the other hand, we've been asked for TCP-only migration for quite a while, it's possible to pull this code into smth like libcriutcp.so, but it will require patching. You're welcome to participate to the https://github.com/xemul/criu/issues/72

establish bluetooth piconet connection between server and two clients using c on linux platform?

I want to establish Bluetooth network where one server can communicate to two clients (ie piconet) using C programm on linux platform, rfcomm based communication.
Can any one please share your guidance or sample source code if have.
I newbie to the bluetooth technology, have not found any useful info or code from internet source so far. so please.
Thank you
Basu
Linux runs open source BlueZ Bluetooth Stack, which works quite well (unless you need Bluetooth Low Energy). You can check out this tutorial: http://people.csail.mit.edu/albert/bluez-intro/c404.html
PS. Mind the GPL license when using #include like in those examples.
Edit:
As for creating piconet specifically, I'm afraid I don't have any snippets. However, after quick search, I would look into using bluez library to open not one but many RFCOMM sockets. So you can listen to and accept multiple connections.

send data internet using C

I need to send a signal via my remote PC to the Internet that let me know if this pc is connected.
I could send a link with GET values to my page and then from that php page make a query to the database.
How do I send this value through a C program that runs on this remote PC?
thanks!
(it's a windows pc)
For making HTTP requests I recommend libcurl, which is the library that almost everybody seems to be using.
http://curl.haxx.se/libcurl/
What operating system? Linux? Windows? Does the program need to be cross-platform? The reason I ask is that it influences whether you should use a library, or TCP/IP sockets, given that the request will be very simple.
Also, why not use Perl, or better yet, wget? You could schedule a task in windows, or a cronjob in unix, to wget http://yoururl/path?pcname=`uname` or similar..
What about using a client like dyndns? I'm not sure using a C program would be such a good idea for that purpose; it's a system administration task and using scripting for this would work best, unless you have a specific need in mind.

Using telnet in a C Program

I am working on a robot automation project and I have run into a road block. To control the robot, one needs to connect with it wirelessly via telnet and send commands through the tcp/ip protocol. (ex. The 'Mabc' command moves it forward based on the left wheel speed (a), the right wheel speed (b) and time (c)). What I am trying to do is do some calculations in a C program and then send a message to the robot based on the value of the calculation.
How can send commands via tcp/ip protocol in a C program?
Thanks!
Erik
You are looking for sockets. This is a comprehensive guide to socket programming in C. Telnet is also a well defined protocol, although I don't know if this robot would use telnet or not (it's extra processing overhead for a protocol that wouldn't have much added benefit for a robot control program). Telnet is covered in detail by RFC 854
Expect would allow you to interact with external programs, but I am not aware of a C port of expect. Otherwise you would find a telnet library in C or write your own using socket programming.
I would use libcurl: http://curl.haxx.se/libcurl/. It'll do what you want, and handle all the telnet goo that you really don't want to handle.
Expect was designed to do exactly this - hold conversations with interactive programs. It's written in Tcl, extending the Tcl interpreter with various commands. Tcl is very easy to extend; it was designed to be an embedded scripting language right from the word go. The main C API uses argv-style constructs to pass parameters to Tcl commands and is very easy to use. The best guide to the C API is Ousterhout's original book and it took me one two-hour lab session to get my first embedded Tcl interpreter up and running.
As a bonus you also get a built-in Tcl interpereter, which you can use to add a scripting capability to your application. You'll probably find that quite a bit of it can be implemented in Tcl if you feel so inclined, so it will probably save you time overall.
I would be:
writing some simple shell scripts containing the telnet interractions written as here documents.
using a .telnetrc file in your home directory to control aspects of your telnet session, e.g. crmod
calling the script using system calls.
This way your turnaround time to change your interractions with the robot won't involve having to recompile your programm all the time.
BTW This sounds like fun.
HTH.
cheers,
Rob

Resources