c语言socket获得网页源代码(原)
int sockfd;
int len;
struct sockaddr_in address;
int result;
char *strings="GET /default.html HTTP/1.1 Host:23.20.236.43 Connection: Close ";
char ch;
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *pURL;
pURL = gethostbyname("http://name.com/");
memset(&address,0, sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
address.sin_port = htons(80);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1){
perror("oops: client1");
return ;
}
write(sockfd,strings,strlen(strings));
while(read(sockfd,&ch,1))
printf("%c", ch);
close(sockfd);