c - 503服务暂时不可用

标签 c sockets raspberry-pi raspbian

我需要在带有 Raspbian 的 Raspberry PI 零上使用简单的 HTTP 客户端。我使用了一些示例代码,但是当我发送大约 7 个请求时,我只能下载出现此错误的页面: 503服务暂时不可用 没有可用的 fastcgi 进程来满足您的请求。

使用代码之一:

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#define SA      struct sockaddr
#define MAXLINE 4096
#define MAXSUB  200

ssize_t process_http(int sockfd, char *host, char *page)
{
    ssize_t n;
      snprintf(sendline, MAXSUB,
                 "GET %s\r\n"
                 "Host: %s\r\n"
                 "Connection: close\n"
                "\n", page, host);

        write(sockfd, sendline, strlen(sendline));

        while ((n = read(sockfd, recvline, MAXLINE)) > 0)
         {
    recvline[n] = '\0';
    }
        printf("%s", recvline);


        return n;
}

主要是这样的:

 int sockfd;
        struct sockaddr_in servaddr;

        char **pptr;

        char *hname = "plankter.cz";
        char *page = "http://plankter.cz/iot/list.json";

        char str[50];
        struct hostent *hptr;
        if ((hptr = gethostbyname(hname)) == NULL) {
                fprintf(stderr, " gethostbyname error for host: %s: %s",
                        hname, hstrerror(h_errno));
                exit(1);
        }
        printf("hostname: %s\n", hptr->h_name);
        if (hptr->h_addrtype == AF_INET
            && (pptr = hptr->h_addr_list) != NULL) {
                printf("address: %s\n",
                       inet_ntop(hptr->h_addrtype, *pptr, str,
                                 sizeof(str)));
        } else {
                fprintf(stderr, "Error call inet_ntop \n");
        }

        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(80);
        inet_pton(AF_INET, str, &servaddr.sin_addr);

        connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
        process_http(sockfd, hname, page);
        close(sockfd);

在这个例子中,我下载了 json,但是当我读取 php 或 txt 时,我遇到了同样的问题。我尝试了一些使用套接字的示例代码,例如使用 happyhttp 库的示例,并且在 7 个请求后都给我相同的结果。我认为它不会关闭连接。我需要发送 http 请求和接收数据,我需要在几分钟内完成几次。

感谢所有想法。

最佳答案

您向 GET 字段提供完整的 URI:

char *page = "http://plankter.cz/iot/list.json";
...
snprintf(sendline, MAXSUB,
            "GET %s\r\n"
            "Host: %s\r\n"
            "Connection: close\n"
            "\n", page, host);

您不应包含协议(protocol)和主机名。

试试这个:

char *page = "/iot/list.json";

关于c - 503服务暂时不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408016/

相关文章:

c - strtok 给出空字符串或我不知道的东西

c - 指针扰乱了我的价​​值观

java - 像操作系统一样运行java应用程序

python - 在 RasPi 上使用 psutil(Python 模块)时出现问题

c - 我不明白 execlp() 在 Linux 中的工作原理

c - 使用 malloc 时对字符串进行排序

c - Socket C - setsockopt超时在关闭之前做一些事情

c# - .NET 套接字缓冲区溢出没有错误

c - C 中的多线程套接字

linux - Raspberry Pi 临时网络