Introduction
HTTP client that does a head request to the server indicated as first argument.
Note: 209.85.146.99 == 0xd1559263 == 3512046179
Code and example
#cat httpclient.c
#include <netdb.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> int main(int argc,char *argv[]){ struct addrinfo server_hints; struct addrinfo *server_result; memset(&server_hints,0,sizeof(struct addrinfo)); server_hints.ai_family=AF_UNSPEC; server_hints.ai_socktype=SOCK_STREAM; server_hints.ai_protocol=0; int sfd,s; printf("%s\n",argv[1]); s=getaddrinfo(argv[1],"80",&server_hints,&server_result); if(s==0){ sfd=socket(server_result->ai_family,server_result->ai_socktype,server_result->ai_protocol); if(sfd>0){ struct timeval timeout; timeout.tv_sec=0; timeout.tv_usec=500000; setsockopt(sfd,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,sizeof(timeout)); setsockopt(sfd,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout)); if(connect(sfd,server_result->ai_addr,server_result->ai_addrlen)!=-1){ freeaddrinfo(server_result); char msg[20]="HEAD / HTTP/1.0\r\n\r\n\0"; send(sfd,msg,strlen(msg),0); char buffer[4096]; bzero(buffer,sizeof(buffer)); recv(sfd,buffer,sizeof(buffer),0); close(sfd); printf("%s\n",buffer); } } } return 0; } #gcc -o httpclient httpclient.c
#./httpclient 3512046179
No comments:
Post a Comment