Devo inviare un pacchetto UDP di 1024 byte a contenuto nullo a tutte le porte che ho in input
nella mia funzione.
In input ho: un unsignet che indica il valore di un timeout, un ip in notazione di stringa,
ed un array di puntatori a port-number espressi in codice numerico null-terminated. Un array di puntatori
a port-number come si rappresenta?
Così?
short ** portnumber;
In tal caso come devo comportarmi per iterare tutte le porte ed inviare il pacchetto
alla coppia (ip,porta) ?
Posto tutto il codice dell'esercizio va..
Graziecodice:int gestionesegnale(int segnale) { return -1; } #DEFINE BUFF 1024; char *message[BUFF]; short funzione(unsigned timeout, char *ip, array di porte ecc.. (?)) { //Creo il socket int ds_sock; ds_sock = socket(AF_INET,SOCK_DGRAM,0); if(ds_sock == -1) { printf("Errore nella creazione del socket"); exit(1); } //Faccio il bind struct sockaddr local_addr; sockaddr.sin_family = AF_INET; sockaddr.sin_port = 0; sockaddr.sin_addr = INADDR_ANY; if(bind(ds_sock,&local_addr,sizeof(local_addr))==-1) { printf("Errore nel bind"); exit(1); } //Struttura per indirizzo destinazione struct sockaddr remote_addr; sockaddr.sin_family = AF_INET; hostent *hp; hp = gethostbyname(ip); memcpy(&remote_addr.sin_addr, hp -> h_addr, sizeof(h_addr)); //Qui devo gestire l'invio while(ci sono porte) { remote_addr.sin_port = htons(portacorrente (?)); if(sendto(ds_sock,message,BUFF,0,&remote_addr,sizeof(remote_addr)) == -1) { printf("Errore nella sendto"); exit(1); } Qui dovrei incrementare un qualche contatore per iterare le varie porte } //Qui devo ricevere un datagramma int ricevuti,length_mittente_addr; struct sockaddr mittente_addr; ricevuti=0; length_mittente_addr=sizeof(mittente_addr); signal(SIGALARM,gestione_segnale); alarm(timeout); ricevuti=recvfrom(ds_sock,message,BUFF,0,&mittente_addr,&length_mittente_addr); if(ricevuti==-1 || errno==EINTR ||ricevuti==0){ return -1; } return mittente_addr.sin_port; }![]()

Rispondi quotando