C Socket 客户端未从服务器接收任何字节
你好,我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
标头后必须有两个换行符:
strcat(msg,"Host: www.yahoo.com:80\r\n\r\n");
recv()
当远程关闭套接字时返回 0。AF_INET
而不是AF_UNSPEC
。还要检查
socket()
和connect()
返回值,例如(包括 errno.h、stdlib.h):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.AF_INET
instead ofAF_UNSPEC
.Also check
socket()
andconnect()
return values, e.g. (include errno.h, stdlib.h):