Código
#include <unistd.h> #include <stdio.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdlib.h> #include <string.h> #include <iostream> int main(int argc, char *argv[]) { char request[1024]; char web[20]; int puertods; printf("escribe la direccion web\n"); std::cin>>web; printf("escribe el puerto (90 & 80) por defecto\n"); std::cin>>puertods; struct hostent *host = gethostbyname(web); if(!host) { printf("No se ha podido resolver la direccion del servidor"); exit(1); } struct sockaddr_in sock; sock.sin_family = AF_INET; sock.sin_port = htons(puertods); sock.sin_addr.s_addr = inet_addr(host->h_addr); int sockfd = socket(AF_INET, SOCK_STREAM, 0); int aux = connect(sockfd, (struct sockaddr*) &sock, sizeof(sock)); if(aux==-1) { printf("No se pudo conectar al servidor"); exit(2); } sprintf(request, "GET / HTTP/1.1\nHost: %s\nUser-Agent: Mozilla/4.0\n\n ", host->h_name); for(aux=0; aux<2000; aux++) { write(sockfd, request, strlen(request)); } }