|
写了一个程序,但编译之后运行一点反应也没有。
int main(int argc,char* argv[]){
if(argc!=2){
fprintf(stderr,"lease input a hostname\n");
return -1;
}
int socket_fd=socket(PF_INET,SOCK_STREAM,0);
struct sockaddr_in address={
.sin_family=AF_INET,
.sin_port=htons(80),
};
inet_aton(argv[1],&address.sin_addr);
memset(&address,0,sizeof(address));
if(connect(socket_fd,(struct sockaddr*)&address,sizeof(struct sockaddr_in))){
fprintf(stderr,"failed to connect\n");
return -1;
}
printf("Server Conected!\n");
char* html_document=(char*)malloc(sizeof(char));;
char* head="GET /index.html HTTP/1.1\n";
if(send(socket_fd,head,strlen(head),0)==-1){
fprintf(stderr,"failed to send\n");
return -1;
}
printf("Head Sended!\n");
printf("%s",head);
if(recv(socket_fd,html_document,65535,0)==-1){
fprintf(stderr,"failed to receve\n");
return -1;
}
printf("%s\n",html_document);
return 0;
} |
|