How to send data over internet through console - c

I have to develop an application wherein I would receive data from parallel port and send it over to internet. This application is to be developed for embedded device running linux. Please suggest me how I can do that.
Regards

Sounds like a job for netcat. You can just open the device file and bind it straight to a TCP port: cat /dev/whatever | nc -l 2345 reads from a device and writes the results to a socket in case a client connects to port 2345.
If you need security, consider using a SSH tunnel.

Best solution - socat.
It can read from file and send to any socket (tcp, udp, unix, ipv4, ipv6), redirect program output, stdout. Reverse operations also posible.
Local example: read file "test", and send it content to localhost:9999
socat OPEN:test TCP:localhost:9999
If you want monitor file content and make it read only
socat OPEN:test,rdonly,ignoreeof TCP:localhost:9999
in socat you not need bash, in cat|nc some form of shell required.

I recommend sockets using C.

I would suggest either SSH or Telnet.

I would suggest using one of Perl, Python, or Ruby to do it if it has some processing to do.
Otherwise, if it is to use any console command, you can use curl or wget.

If you want to do it in C, perhaps because your embedded Linux doesn't have any of the shell tools and languages that other people have suggested, you need to look at the socket interface. The sequence of events is more or less:
create a socket using socket()
connect to a server using connect()
send your data using send(), or write() and deal with anything that comes back the other way using recv() or read().
close the socket using close().

Related

How to find an AF_UNIX socket and see the information passing through it?

There is an application running on a FreeBSD 10.1 release operating system and i need to figure out how to find the sockets it has created and is using. I know that i'm looking for an AF_MAP socket which should be similar to a AF_UNIX socket.
How do i see what sockets are open, and once i find the one im looking for i need to see what information passes through. how is this also done?
Thankyou
I'm not sure on FreeBSD specifically but you can use lsof in a way like:
$ lsof -p $(pidof your-appname)
This will give you all the files it has opened.
For AF_UNIX sockets you may refer to this.
Also, you may want to use netstat -xa to see CONNECTED state sockets (if the application uses stream oriented sockets)

IPC pipe through files /dev/ttyS0 and /dev/ttyS1

I want to perform IPC pipe through serial port dev files. here are the requirements
first of all I am trying to use
sudo socat /dev/ttyS0,raw,echo=0,crnl /dev/ttyS1,raw,echo=0,crnl
it is giving an error as follows
2014/12/xx 10:33:19 socat[17848] E tcgetattr(4, 0x7fffe76ecaa0): Input/output error
Once the ttyS0 and ttyS1 are connected, I assume we can perform read/write operation as similar as using a pipe(), I will have two programs peer0.c and peer1.c, peer0.c opens /dev/ttyS0 and peer1.c opens /dev/ttyS1,
so that read write operation should be as follows
peer0=>ttyS0--->---ttyS1=>peer1
peer0<=ttyS0---<---ttyS1<=peer1
and since /dev/ttyS(0/1) are system wide, i can run peer0.c program in one terminal and peer1.c in another,
Basically, I have linux based embedded application program which when ported to target hardware it will be controlled by linux based PC via minicom UART interface. target opens its ttyS0 to read uart data sent from PC, in PC, commands will be sent to target via minicom.
Now I want to run target application in same PC in one terminal and want to send command from another terminal/minicom. Also the communication should be bidirectional
is it possible to achieve this goal?
or is there any other similar way to achieve the same?
thanks in advance
If you want to just transfer files, use a protocol for doing so over serial links, such as ZMODEM (http://en.wikipedia.org/wiki/ZMODEM), if you want full IPC, establish a PPP connection over the link (http://en.wikipedia.org/wiki/Point-to-Point_Protocol)
peer0=>ttyS0--->---ttyS1=>peer1
peer0<=ttyS0---<---ttyS1<=peer1
You seem to want socat to take the part of the --->--- and ---<--- above. This is not possible, because socat had to open ttyS0 and ttyS1 and compete with peer0 for input from ttyS0 as well as with peer1 for input from ttyS1.
To achieve the goal of communicating with an application on one serial port via another port, just connect the two ports with a null modem cable.

How can i connect to memcached via C sys/socket.h?

How can i connect to Memcached via C sys/socket.h and set some text string to key "key"? I can't figure out how can I run .c program which will connect to cashing system via socket. I can connect to it through console by writing smth like this
memcached -l 127.0.0.1 -p 12345 -m 64 -vv
and then
set key 1 0 4
test
but i have to do it using socket in C
It looks like you're missing some knowledge regarding C sockets in general.
As an overview, a socket is a two way communication channel that connects a client with a server, each having their own end of the socket.
What memcached is doing is using the socket mechanism to transfer data between memcached and whoever it is looking for the data.
memcached is using TCP sockets and clear text messages so it is easy to work with.
What you'll have to do:
open a socket and connect it to the memcached server at 127.0.0.1 port 12345 (taken from your example)
Write 'set key 1 0 4\n' to the memcached socket
Read the string from the socket (this is memcached result)
The following read: http://www.thegeekstuff.com/2011/12/c-socket-programming/ provides code snippets and great explanation on sockets and how to use them, and the client code contains 90% of the work you need to do
Feel free to ask if you need further clarifications

Linux: stdout and stderr to socket

I want to redirect stdout and stderr to a socket that I can then use to remotely monitor the status of my application over Ethernet. Currently I accomplish this by using ssh and watching the output in the shell console. I'd prefer to remove the middle man if possible and just send the entire stdout and stderr output to a udp or tcp/ip port and have my monitoring computer connect to it.
I cannot use UART or any other wired connection. It has to be Ethernet. Also, if possible, I'd like to accomplish this via a bash script, to prevent having to rebuild my application.
Thanks for the help.
The way you describe it, it sounds like your going to need either your existing application to open a passive socket and wait for connections, or your going to have to wrap your application in something that sets up a listening socket. This post suggests that is not possible in just Bash, however it does show ways to do it from the command line with netcat or perl. For example you could do something like this with netcat: nc -l -p <port> -c "tail -F /var/log/blah"
On the monitored application side, there is a way to redirect both outputs to an outbound connection, using netcat:
$ ./application 2>&1 | nc <remote-host> <remote-port>
This way, you're redirecting stderr to stdout and then pipe it all together to netcat, which will take care of setting up the socket, establish connection with the remote host and all that stuff.
However, bear in mind that you can suffer from printf()'s buffering, if that's the function you're using to write to stdout. In my local tests, I've seen that the data sent to stderr by the application is seen immediately on the other listening end, but on the other hand the data sent to stdout is only sent when the application exits or there's enough data in the buffer to flush it all at once. So, if you care about the order and the availability of the info on the monitoring side, I'd suggest you to place calls to fflush(stdout); whenever you print something interesting to stdout, or replace the calls to printf(), fprintf() and the like to write(), which does not buffer. The downside is that you have to touch the code of the application, of course, but I don't know any way to externally force flushing of an application's output buffers (i.e. from bash).

Interprocess communication with a Daemon

I want to implement a Unix daemon (let's call it myUnixd), and want the user to be able to interact with this daemon via the command line, for example:
myUnixd --help # will display help information
myUnixd --show # will show some data (the's deamon should be doing the work)
So my question is: How can I communicate with the daemon? I was thinking about Unix domain sockets. Can someone tell me the right way to do this?
Thanks.
Use Berkeley sockets. Specifically, you can create a "UNIX domain socket" (otherwise known as a "local domain socket," which will create what looks like a text file. Write to the text file to send text to the daemon, read from it to receive text from the daemon. You can implement this with a few function calls.
If you want something more advanced, you can also use DBus, which offers a more sophisticated interface, but which is more complicated to learn.
use tcp socket if you want to use telnet to communicate with your daemon.
One could also use Remote Procedure Call (RPC) for such client-server communication. There are different types of messages (protocols) that can be used together with it, one of them is JSON.
The JSON-RPC protocol is very well accepted for such tasks. You can find different tools and libraries to embed in your software. A quick search on google gives this C library. The advantage of such libraries is that from a JSON specification file, where you define all your remote function calls, it creates client and/or server stubs that you can just use in your code out of the box.
As a listener one can use sockets, as the other responses state, or just an embedded HTTP server like microhttpd (and libcurl for the client). There are plenty of examples out there to just reuse. HTTP allows you also to run your client behind a proxy.

Resources