sender.c
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
<<<<<<< HEAD
int initializeSocketUDP(char* service)
{
int sock_id;
struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les requêtes
=======
int initializeSocketUDP(char* service)
{
int sock_id;
struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les reqêtes
>>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
memset(&precisions, 0, sizeof precisions);
precisions.ai_family = AF_UNSPEC;
precisions.ai_socktype = SOCK_DGRAM;
//precisions.ai_flags = AI_PASSIVE;
if(getaddrinfo("192.168.0.14", 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++;
}
printf("Value : %d\n", n);
struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
printf("Addr : %x\n", test->sin_addr.s_addr);
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_BROADCAST, &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);
return sock_id;
}
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;
char tmp_str[15];
sprintf(tmp_str, "%d", port);
int sock_id=initializeSocketUDP( tmp_str );
printf("Initialize via Broadcast %d\n", sock_id);
<<<<<<< HEAD
int status;
if((status=sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP))) < 0) { fprintf(stderr, "Erreur sendto %d\n", status); return(-1);}
=======
if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}
>>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
close(sock_id);
return 0;
}
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
char tmp_str[15];
sprintf(tmp_str, "%d", port);
int sock_id=initializeSocketUDP( tmp_str );
printf("Initialize via Unicast %d\n", sock_id);
<<<<<<< HEAD
if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto %d\n", errno); return(-1); }
=======
if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto\n"); return(-1); }
close(sock_id);
return 0;
}
>>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
close(sock_id);
return 0;
}
<<<<<<< HEAD
=======
>>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
/*
int main(void)
{
sendUDPBroadcast("Broadwesh", 2020);
sendUDPUnicast("192.168.43.174", "Uniwesh", 2020);
return(1);
}
*/