C Socket 客户端未从服务器接收任何字节

发布于 2024-12-27 21:55:06 字数 1877 浏览 1 评论 0原文

你好,我正在用 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;
}

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

Hi I'm building a primitive browser in c which is to do a very simple task. I'm trying to get my client to simply readout the response message from a server that I request from. I'm trying to get a simple response from www.yahoo.com. I have experimented with many different request messages which have all sent successfully. The following request messages are below.

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

and

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

The problem is that I am not recv()-ing any bytes from the server. recv() however does not result in error being -1. Rather the server just doesn't want to respond. Below is my code.

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

Any help would be great. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2025-01-03 21:55:06

HTTP 协议要求发送 2 个回车/换行符来结束 HTTP 请求,

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

HTTP protocol requires that 2 carriage returns/newlines are sent to end the HTTP request

I dont see them in your question

不回头走下去 2025-01-03 21:55:06

标头后必须有两个换行符: strcat(msg,"Host: www.yahoo.com:80\r\n\r\n");

  • recv() 当远程关闭套接字时返回 0。
  • 使用正确的地址系列 AF_INET 而不是 AF_UNSPEC

还要检查 socket()connect() 返回值,例如(包括 errno.h、stdlib.h):

if (mySocket == -1) {
  perror("Creating socket failed");
  exit(EXIT_FAILURE);
}

There must be two newlines after the header: strcat(msg,"Host: www.yahoo.com:80\r\n\r\n");

  • recv() returns 0 when remote has closed the socket.
  • Use proper address family AF_INET instead of AF_UNSPEC.

Also check socket() and connect() return values, e.g. (include errno.h, stdlib.h):

if (mySocket == -1) {
  perror("Creating socket failed");
  exit(EXIT_FAILURE);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文