C http O_NONBLOCK模式问题?

时间:2011-12-11 17:16:45

标签: c http

我正在学习小型http客户端的开发,特别是使用带有http / 1.1客户端的NONBLOCKING tcp套接字的整体复杂性。

下面的小代码似乎在Linux上工作,但不是在我的mac os x 10.6.8 ..调试后它似乎循环使用send函数? linux和mac os x / freebsd之间的非阻塞模式是否有区别?

这是我收到的......

在端口80上连接到google.com(173.194.65.147)...确定 GET / HTTP / 1.1 主持人:google.com 用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_6_8)AppleWebKit / 534.52.7(KHTML,与Gecko一样)Version / 5.1.2 Safari / 534.52.7

发送失败:套接字未连接

有人可以帮我理解这段代码中出了什么问题吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

void 
setSockNonBlock(int sock)
{
    int             flags;
    flags = fcntl(sock, F_GETFL, 0);
    if (flags < 0) {
    perror("fcntl(F_GETFL) failed");
    exit(EXIT_FAILURE);
    }
    if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
    perror("fcntl(F_SETFL) failed");
    exit(EXIT_FAILURE);
    }
}

int 
getIP(const char *uri, char *hostname, struct in_addr * ip)
{
    char           *tmp;
    tmp = (char *) malloc(strlen(uri) + 1);
    strncpy(tmp, uri, strlen(uri) + 1);
    char           *p, *q;
    p = strstr(tmp, "http://");
    if (p == NULL) {
    p = tmp;
    } else {
    p = tmp + 7;
    }
    q = strstr(p, "/");
    if (q != NULL) {
    *q = '\0';
    } else {
    q = tmp + strlen(uri);
    }
    strncpy(hostname, p, q - p + 1);
    free(tmp);

    struct hostent *host;
    if ((host = gethostbyname(hostname)) == NULL) {
    fprintf(stderr, "gethostbyname failed, %s\n", hstrerror(h_errno));
    exit(EXIT_FAILURE);
    }
    int             i;
    for (i = 0; host->h_addr_list[i] != NULL; i++) {
    if (host->h_addrtype == AF_INET) {
        memcpy(ip, (struct in_addr *) (host->h_addr_list[i]), sizeof(struct in_addr));
        return 1;
    }
    }
    return 0;
}

int 
main(int argc, char *argv[])
{

    if (argc != 2) {
    fprintf(stderr, "USAGE: %s uri\n", argv[0]);
    exit(EXIT_FAILURE);
    }
    char           *uri;
    uri = argv[1];

    char           *hostname;
    hostname = (char *) malloc(strlen(uri) + 1);
    struct in_addr  ip;
    if (!getIP(uri, hostname, &ip)) {
    fprintf(stderr, "getIP for %s failed\n", uri);
    exit(EXIT_FAILURE);
    }
    char            ip_str[INET_ADDRSTRLEN];
    if (!inet_ntop(AF_INET, &ip, ip_str, INET_ADDRSTRLEN)) {
    perror("inet_ntop failed");
    exit(EXIT_FAILURE);
    }
    int             sock;
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket failed");
    exit(EXIT_FAILURE);
    }
    setSockNonBlock(sock);

    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr = ip;
    serv_addr.sin_port = htons(80);
    if (connect(sock, (struct sockaddr *) & serv_addr, sizeof(serv_addr)) == -1 && (errno != EINPROGRESS)) {
    perror("connect failed");
    exit(EXIT_FAILURE);
    }
    printf("Connect to %s(%s) on port 80 ... ok \n", hostname, ip_str);

    char            request[1024] = "";
    char           *get;
    get = strstr(uri, hostname);
    if (get == NULL) {
    get = "/";
    } else {
    get += strlen(hostname);
    if (*get == '\0') {
        get = "/";
    }
    }

    sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7\r\n\r\n", get, hostname);
    printf("%s\n", request);
    free(hostname);

    char           *p = request;
    int             remaining = strlen(request);
    ssize_t         sent_size;
    while (remaining) {
    sent_size = send(sock, p, remaining, 0);

    if (sent_size == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
        continue;
    }
    if (sent_size <= 0) {
        perror("sent failed");
        exit(EXIT_FAILURE);
    }
    remaining -= sent_size;
    p += sent_size;
    }

    char           *resp = NULL;
    int             resp_size = 0;
    char            buf[1024];
    ssize_t         recv_size;

    char           *tmp;
    while (1) {
    recv_size = recv(sock, buf, sizeof(buf), 0);
    if (recv_size == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
        continue;
    }
    if (recv_size <= 0) {
        break;
    }
    tmp = (char *) realloc(resp, resp_size + recv_size + 1);
    if (tmp == NULL) {
        fprintf(stderr, "Error: realloc failed, not enough memory to save response");
        if (resp) {
        free(resp);
        }
        exit(EXIT_FAILURE);
    }
    resp = tmp;

    tmp = resp + resp_size;
    memcpy(tmp, buf, recv_size);
    resp_size += recv_size;
    resp[resp_size] = '\0';
    if (strstr(resp, "</html>")) {
        break;
    }
    }
    if (resp) {
    printf("%s\n", resp);
    free(resp);
    }
    close(sock);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

我怀疑你的问题是你正在做一个非阻塞的连接(),但是会立即通过一些代码执行,然后尝试发送那个可能还没有连接的套接字。

你可以通过在连接后在那里停留sleep()几秒钟来做一个简单的测试,看看是否是真正的原因。但更大的问题是,至少在目前,你没有任何令人信服的理由使用非阻塞套接字,因为你不会在你无论如何被阻止的时候做任何事情。例如,您的发送循环不会执行任何操作,只需检查返回代码并尝试再次发送。你可能只是阻止。也许利用非阻塞的代码是您下一阶段的目标?

无论如何,请查看select()poll()(以及pselectppoll等朋友,...)。它将有助于多路复用。

答案 1 :(得分:0)

是否有理由避免在HTTP客户端部分使用现有的HTTP相关库(如libcurl,而在HTTP服务器端使用D.Moreno's Onionlibmicrohttpd

编写自己的HTTP协议栈非常耗时!!!