#include <stdio.h>
#include <stdlib.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include <netdb.h> #include <sys/types.h> #include <string.h> char *http_header = "GET / HTTP/1.1\r\n"
"Host: www.idoushuo.com\r\n"
"Connection: keep-alive\r\ni"
"Accept: */*\r\n"
"Connection: close\r\n"
"\r\n"; char *hostname = "www.idoushuo.com"; int main(){
struct sockaddr_in addr;
memset(&addr, , sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(); struct hostent *hp;
hp = gethostbyname(hostname);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); int sockfd = socket(AF_INET, SOCK_STREAM, );
int ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); //send
send(sockfd, http_header, strlen(http_header), );
//recv
ssize_t length = ;
char buf[];
memset(buf, , sizeof(buf));
char *response = calloc(, sizeof(char));
do{
length = recv(sockfd, buf, , );
if(length){
//leak mem
char *pp = realloc(response, strlen(response)+length+);
if(!pp){
break;
}
response = pp;
memcpy(response + strlen(response), buf, length);
memset(buf, 0x0, sizeof(buf));
}
}while(length != ); printf("%s\n", response); free(response);
}