0

I'm totally new to programming c++ for Linux, I'd like to make the following;

Console application that would handle and echo the parameters of an incommoding HTTP GET request.

But my first step would be;

So if open a browser and do a;

http://192.168.2.10/?yadda=1

On my linux system on 192.168.2.10, I would get a echo on the screen

New incomming web request parameters: yadda=1

I've done this a few times with .NET with a http listener, but I'm totally clueless on how to do this with C++ in linux.

Thanks for your help!

(No netcat, no vmware under linux running .net with a httplistener, no echo-ing piping, script solution, emulation or whatever, I want to know how to do it in C++ under linux)

In other words;

Dim listener As New HttpListener()
listener.Prefixes.Add("http://localhost/")
Dim context As HttpListenerContext = listener.GetContext

In linux using C++ to create a binairy executable. Some actual lines of c++ code would be helpfull. Thanks

Dennis
  • 1,528
  • 2
  • 16
  • 31

1 Answers1

3

1) You can wire something up pretty quickly with netcat (nc) on Linux without writing any network code.

nc can be run in server mode, and can pipe input / output to another program, like a C++ console program.

2) You can also use inetd / xinetd to turn a console program into a network daemon. You configure your program for a specific port (in the inetd config file) and it does the work of listening for connections, and then execs your C++ program with the socket descriptor duped as the STDIN/STDOUT so you just use standard input / output calls. That, again, lets you write a network program, without knowing sockets. Here is an example: Linux: How to make a daemon/service usable with xinetd?

I would start with option 2, it works, and can get a prototype going within minutes to let you focus on your console echo functionality, then if you really need to later, you can revisit things and write a full network daemon.

3) I just remembered libcurl (http://curl.haxx.se/libcurl/c/), it works well. Thanks to SChepurin for mentioning it in the comments. I have used it on Linux. It is C, but you can wrap it in C++ easier than wrapping the Berkeley API.

Other than that, you are going to be using the Berkeley (BSD) / POSIX sockets (http://en.wikipedia.org/wiki/Berkeley_sockets) calls on Linux, or there may be a nice C++ library out there. I can send you mine offline if you like. The best book I know is the late W. Richard Stevens famous book, UNIX Network Programming, and the related series.

I just thought I'd throw the first 2 ideas out there since you said you weren't sure where to start with C++, it might get you started faster.

Community
  • 1
  • 1
codenheim
  • 20,467
  • 1
  • 59
  • 80
  • NetCat don't work, trust me, I've tried to get that working for over an year now. – Dennis May 17 '14 at 07:32
  • I've used netcat for quick and dirty servers to pipe data, but can't recall if there is any way to issue commands and respond to patterns, so that is why I recommended you try #2, or at least combine netcat with a C++ console program. #2 is well established. – codenheim May 17 '14 at 07:41