#include #include #include #include #include #include #include int initialized=0; int sock_id; int initializeSocketUDP(char* service) { struct addrinfo precisions, *resultat=NULL, *origine; memset(&precisions, 0, sizeof precisions); precisions.ai_family = AF_UNSPEC; precisions.ai_socktype = SOCK_DGRAM; precisions.ai_flags = AI_PASSIVE; if(getaddrinfo(NULL, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } int n=0; for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next) { printf("%d - ", n); if(i->ai_family==AF_INET) resultat=i; //printf("Test : %d", origine->ai_addrlen); printf("\n"); n++; } if((sock_id=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); } int val=1; if(setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR|SO_DEBUG, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } if(bind(sock_id, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); } freeaddrinfo(origine); initialized=1; } int sendUDPBroadcast(char* message, int port) { static struct sockaddr_in* addrUDP; addrUDP->sin_family=AF_INET; addrUDP->sin_port=(uint16_t)port; struct in_addr tmpadd={0xffffffff}; addrUDP->sin_addr=tmpadd; if(initialized!=1) { char* tmp_str; sprintf(tmp_str, "%d", port); initializeSocketUDP( tmp_str ); } sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) addrUDP, sizeof(addrUDP)); } int sendUDPUnicast(char* address, char* message, int port) { static struct sockaddr_in* addrUDP; addrUDP->sin_family=AF_INET; addrUDP->sin_port=(uint16_t)port; inet_aton(address, &(addrUDP->sin_addr)); // Converti l'écriture string de format a.b.c.d en format Internet if(initialized!=1) { char* tmp_str; sprintf(tmp_str, "%d", port); initializeSocketUDP( tmp_str ); } sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) addrUDP, sizeof(addrUDP)); } int main(void) { return(1); }