gpt4 book ai didi

C http O_NONBLOCK 模式问题?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:13:24 26 4
gpt4 key购买 nike

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

下面的小代码似乎可以在 linux 上运行,但不能在我的 mac os x 10.6.8 上运行。调试后它似乎在使用发送函数循环? linux 和 mac os x/freebsd 之间的非阻塞模式有区别吗?

这是我收到的...

在端口 80 上连接到 google.com(173.194.65.147) ... 好获取/HTTP/1.1主机:google.com用户代理: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

发送失败:套接字未连接

谁能帮我理解这段代码出了什么问题?

#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;
}

最佳答案

我怀疑你的问题是你正在做一个非阻塞的 connect() 但立即通过一些代码,这将需要几毫秒来执行,然后尝试在可能尚未连接的套接字上发送。

您可以通过在连接后将 sleep() 插入几秒钟来进行简单测试,看看这是否是真正的原因。但更大的问题是,至少在目前,您没有任何令人信服的理由使用非阻塞套接字,因为您没有使用任何东西,否则无论如何都会被阻塞。例如,您的发送循环除了检查返回码并尝试再次发送外什么都不做。你也可以阻止。也许利用非阻塞的代码是您下一阶段的目标?

无论如何,请查看select()poll() (还有像 pselectppoll 等 friend )。这将有助于多路复用。

关于C http O_NONBLOCK 模式问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8465882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com