gpt4 book ai didi

C Socket Client 不从服务器接收任何字节

转载 作者:可可西里 更新时间:2023-11-01 16:24:47 24 4
gpt4 key购买 nike

您好,我正在用 c 语言构建一个原始浏览器,这是为了完成一项非常简单的任务。我试图让我的客户简单地从我请求的服务器中读出响应消息。我正在尝试从 www.yahoo.com 获得简单的响应。我尝试了许多不同的请求消息,它们都已成功发送。以下请求消息如下。

GET http://www.yahoo.com HTTP/1.1\r\n
Host: www.yahoo.com:80\r\n
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Language: en-us,en;q=0.5\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
Connection: keep-alive\r\n

GET http://www.yahoo.com HTTP/1.1
Host: www.yahoo.com:80

问题是我没有从服务器接收任何字节。然而,recv() 不会导致错误为 -1。相反,服务器只是不想响应。下面是我的代码。

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

#define MAXDATASIZE 500;

int main(int argc, char *argv[]){
struct addrinfo serverSide,*serverInfo;
int mySock, status;
char buf[501],ipstr[INET6_ADDRSTRLEN];

memset(&serverSide, 0, sizeof serverSide);
serverSide.ai_family = AF_UNSPEC;
serverSide.ai_socktype = SOCK_STREAM;

if(getaddrinfo("www.yahoo.com","80",&serverSide,&serverInfo)==0){
printf("get addr success\n");
}

mySock = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
connect(mySock, serverInfo->ai_addr, serverInfo->ai_addrlen);

char msg[500] = "GET / HTTP/1.1\r\n";
strcat(msg," Host: www.yahoo.com:80\r\n");

printf("%s\n",msg);
if((status = send(mySock,msg,strlen(msg),0))== -1){
printf("request not sent %d\n",status);
perror("\n");
}else{
if((status = recv(mySock, buf, 500, 0))== -1){
printf("recieved byte error");
}else{
printf("%s\n Number of bytes recieved %d\n",buf,status);
}
}
close(mySock);
freeaddrinfo(serverInfo);
return 0;
}

任何帮助都会很棒。谢谢!

最佳答案

HTTP协议(protocol)要求发送2个回车/换行结束HTTP请求

我在你的问题中没有看到它们

关于C Socket Client 不从服务器接收任何字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8921954/

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