Вопрос

I need to create a C++ FastCGI server and connect to it in the simplest way possible in order to test/develop incrementally.

I have both an issue conceptually and with the lack of API documentation for FastCGI. My understanding is that when I do

FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
while (FCGX_Accept_r(&request) == 0) {
}

This opens a socket and listens on it for requests. Then, using boost::asio, I want to do something like

boost::asio::io_service io_service;
boost::system::error_code error;

using boost::asio::ip::tcp;
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", "" /* WTF are the params here? */);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);

while (true)
{
    boost::array<char, 128> buf;

    size_t len = socket.read_some(boost::asio::buffer(buf), error);
    if (error)
      break;
}

CPPUNIT_ASSERT_EQUAL(error, (boost::system::error_code)boost::asio::error::eof);

I.e. to programmatically send a request to the FastCGI process to test it's working.

Anyway, I believe that tcp::resolver::query is the key to setting up communication from my simple unit test.

However,

  • I don't fully understand what a 'socket' is in this case. I've read that it's just a file (on linux)?
  • Given this, how do I know what socket FastCGI is listening on?
  • How do I specify the socket to boost::asio?
  • How do I stop the FastCGI process from listening once the test is done?

I'm possibly going about this all wrong, so any hints on the proper way to approach this simple test case would be gratefully accepted. Also, if anyone knows where the hell the API documentation for fcgi/fcgi++/fastcgi++ is, that would be awesome.

Это было полезно?

Решение

Your question is really only about elementary networking.

Edit Check out this most valued internet resource on network programming (basics to advanced):

A socket is a connection. A server is listening on a /port/ (TCP or UDP. Assuming TCP for the rest, since you're doing CGI).

Clients connect to the port and the server accepts the connection, resulting in a socket (bidirectional stream, so to speak). The client now (usually) sends and the server receives the request. Then the server sends the response. (The connection may be closed, or kept alive depending on the protocol/preferences).

Specific answers:

query("localhost", "http");

would end up connecting to port 80 on most systems.

Of course you can also just specify the port number there:

query("localhost", "8080");

Choose the port number that the server bound it's listener to.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top