开发者

CGI/FastCGI app in C/C++ (socket programming question)

I am developing my first C/C++ fastCGI app, using the FastCGI Developers Kit.

I have successfully built one of the sample programs (snippet shown below):

int main (void)
{
    int count = 0;
    long pid = getpid();

    streambuf * cin_streambuf  = cin.rdbuf();
    streambuf * cout_streambuf = cout.rdbuf();
    streambuf * cerr_streambuf = cerr.rdbuf();

    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);

    while (FCGX_Accept_r(&request) == 0)
    {
       /* Handle request */
    }

    return 0;
}

However, when I run the example above, it simply terminates. Upon debugging, I realized that FCGX_Accept_r was returning an error code. Upon further investigation, I realized that the accept() method call was failing.

The actual line that fails is:

 socket = accept(listen_sock, (struct sockaddr *)&sa, &len);

The arguments passed to the function are:

listen_sock: 0
len: 112
sa:

{un = {sun_family = 0, sun_path = "\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\020\\"'\\000\\000\\032\\356m\\000\\000\\000\\0开发者_运维知识库00\\377\\377\\377\\377\\364\\277\\022\\000\\310\\366\\377\\277\\001\\000\\000\\000\\000\\000\\000\\000X\\366\\377\\277\\343\\276\\004\\b\\377\\377\\377\\377\\001\\000\\000\\000\\001\\000\\000\\000\\000\\000\\000\\000\\001\\000\\000\\000\\310\\366\\377\\277\\210\\366\\377\\277F\\277\\004\\b\\310\\366\\377\\277\\001\\000\\000\\000\\000"}, in = {sin_family = 0, sin_port = 0, sin_addr = {s_addr = 0}, sin_zero = "\\000\\000\\000\\000\\000\\000\\000"}}

I am new to socket programming, so I am need some assistance in being able to diagnose what is causing accept() to fail.

Given the information above, can anyone see why the call to accept() would fail?

I am developing on Ubuntu 10.x


Don't know much about this SDK, however a cursory glance through their headers reveals that you need to create a socket first to accept an incoming request, and pass this into the FCGX_InitRequest method.

http://www.fastcgi.com/devkit/include/fcgiapp.h

reveals a method: int FCGX_OpenSocket(const char *path, int backlog); which I would assume you call to create the socket, and then call FCGX_InitRequest with this socket as the second parameter...

Hence why your code fails, you pass in a 0 for socket, which is invalid!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜