Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a c++ application in web server?

I am using a C++ console application in windows. I want to use this application in my website so that input is taken from client side which then invokes this application to process it and output will be forward to the web server. I have heard about COM DLL but don't know how to create it for my application.

like image 380
ANUJ SINGH Avatar asked Jun 01 '16 07:06

ANUJ SINGH


People also ask

Can C# be used for web application?

Among other languages, C# is gaining huge popularity for developing web-based applications. Its core concepts help build an interactive environment and provide functionalities that the dynamic web platform requires. Most aspiring full-stack developers choose this versatile language.

What is webserver C#?

A web server is a software program that serves web pages to web users (browsers). A web server delivers requested web pages to users who enter the URL in a web browser. Every computer on the internet that contains a web site must have a web server program.

Do you need .NET framework to execute AC application?

C# does not compile to native code: it compiles to something called Intermediate Language which is executed by the Common Language Runtime (CLR). Without the . NET framework, the program cannot, and will not run.


2 Answers

You should try creating a cgi script, depending on the needs of your application you should use FastCGI (which does not create an entire process context each time you call it).

  • CGI

You should install an Apache Server and activate the cgi module(its activated by default commonly). Then you can develop a c++ program, put the executable inside the configured CGI folder, give the correct permissions. This CGI script should make some kind of inter-process communication (it could be through socket or shared memory, the first one is easier). I hope you know how a CGI script works in C/C++ + Apache, but its pretty straight forward, in summary you receive the environment inside stdin and put your answer to stdout.

  • FastCGI

You can use apache, install the fastcgi module and create a thread (it could be inside your main loop too, but i dont recommend) inside your program and attach the apache server FastCGI module to your daemon.

Last but not least, you should run your daemon as a service.

PS : There are some framework options(like cppcms and wt), but since you already have the daemon written i thought it would be a pain in the ass to change everything (of course, it depends on a lot of things, like the complexity and size of your application).

like image 130
Paulo André Haacke Avatar answered Sep 30 '22 16:09

Paulo André Haacke


Use CGI: http://cgi.sourceforge.net/

How to take input from the client side?

getenv("QUERY_STRING")  

How to forward output to the web server?

cout << "<html>\n";

Hello world CGI:

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{

   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Hello World - First CGI Program</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<h2>Hello World! This is my first CGI program</h2>\n";
   cout << "<p>REQUEST_METHOD = " << getenv("REQUEST_METHOD") << "</p>\n";
   cout << "<p>QUERY_STRING = " << getenv("QUERY_STRING") << "</p>\n";
   cout << "</body>\n";
   cout << "</html>\n";

   return 0;
}
like image 34
Marian Munteanu Avatar answered Sep 30 '22 16:09

Marian Munteanu