Bobby
Bobby

Reputation: 31

Send/Receive data through TCP on Linux

I am new to Linux, and trying to learn so my question my be really generic. I am trying to have an app which runs on Linux, receives data from an input, maybe a USB port, then creates an string off that and send it to a web server, and webserver will process that string (I'm ok with the webserver part). The point is it has to be done continuously, with no user interaction.

Where do I start? should I go with C++? and also I am really comfortable with PHP, is it something that can be done through that? I would like to put time in it and develop something very reliable.

At the end, could you give some recommendations on where to start to become good at Linux, I have developed in Windows, but I want to move to Linux and master it.

Thanks.

Upvotes: 1

Views: 1582

Answers (5)

MikeD
MikeD

Reputation: 8941

Your question is a bit vague.

  • which device is creating the data?
  • which interface capabilities does the data generating device have (serial, USB, Ethernet. Wifi)?

The simplest solution on your Linux machine is probably a shell script polling a serial port (can even be a USB port used as serial) and writing text into a plain text file - line by line - from where PHP can read it out.

example: cat < /dev/ttyS0 >> ./input.txt

(BTW This command line can be even started from inside a PHP program)

Depending on what communication protocols are available in both your Linux machine and the data generating device, the solution architecture can become more complex and more capable.

Lots of things can be done from the shell (= command prompt) in Unix and don't necessarily call for developing C programs.

Upvotes: 0

Jestin
Jestin

Reputation: 330

If you really want to learn *nix systems, and you want to solve this problem, you should learn to use pipes from the command line.

For your USB to Web example, I would write two programs:

  • one that reads data from a USB device and prints it to standard output (the console)
  • one that reads data from standard input and posts it to the web (you could probably write this second as a bash script that uses the 'curl' utility)

From there you would just simply pipe the output of the one application into the input of the other:

$ usb_program | web_program

This is how Unix/Linux systems were first designed to be used, and your problem poses a perfect example of what piping is supposed to solve.

Upvotes: 1

Peter Wooster
Peter Wooster

Reputation: 6089

I'd start with sockets in PHP or Java. Then move on from there when it all makes sense.

Upvotes: 1

Stepo
Stepo

Reputation: 1056

If you are used to use Windows and .NET you can compile and run your program for Linux thanks Mono http://www.mono-project.com/. If you are interested in sending and recieving stuff via HTTP you can take a look at http://webserver.codeplex.com/. I'm using both.

Upvotes: 1

mohaps
mohaps

Reputation: 1020

if you're looking for a primer for network programming with sockets, there's the all-time classic Beej's Guide - http://beej.us/guide/bgnet/

very accessible for beginners and IMHO, one of the best written primers / tutorials about socket programming.

Upvotes: 1

Related Questions