#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char **argv)
{
int sockfd;
int result;
char httpstring[100];
struct sockaddr_in servaddr;
char ch;
struct hostent *hptr;
char hostip[16];
if(argc != 2){
printf("error usage: %s ip\n", argv[0]);
return 1;
}
if((hptr = gethostbyname(argv[1])) == NULL){
herror("gethostbyname error");
return 1;
}
inet_ntop(hptr->h_addrtype, *hptr->h_addr_list, hostip, sizeof(hostip));
sprintf(httpstring, "GET / HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: close\r\n\r\n", argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(hostip);
servaddr.sin_port = htons(80);
result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if(result == -1){
perror("connect error");
return 1;
}
write(sockfd, httpstring, strlen(httpstring));
while(read(sockfd, &ch, 1)){
printf("%c", ch);
}
close(sockfd);
return 0;
}