Commit 351d9caaf7a717b7c774280fdd1cc7ec6f8c3229
1 parent
1f9d795a
Ajout de serveur/client TCP
Showing
6 changed files
with
183 additions
and
25 deletions
Show diff stats
Network/sender
No preview for this file type
Network/sender.c
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | #include <sys/socket.h> |
3 | 3 | #include <stdio.h> |
4 | 4 | #include <stdlib.h> |
5 | +#include <unistd.h> | |
5 | 6 | #include <string.h> |
6 | 7 | #include <netdb.h> |
7 | 8 | #include <arpa/inet.h> |
... | ... | @@ -13,12 +14,12 @@ int sock_id; |
13 | 14 | |
14 | 15 | int initializeSocketUDP(char* service) |
15 | 16 | { |
16 | - struct addrinfo precisions, *resultat=NULL, *origine; | |
17 | + 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 | |
17 | 18 | |
18 | 19 | memset(&precisions, 0, sizeof precisions); |
19 | 20 | precisions.ai_family = AF_UNSPEC; |
20 | 21 | precisions.ai_socktype = SOCK_DGRAM; |
21 | - precisions.ai_flags = AI_PASSIVE; | |
22 | + //precisions.ai_flags = AI_PASSIVE; | |
22 | 23 | |
23 | 24 | if(getaddrinfo(NULL, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } |
24 | 25 | |
... | ... | @@ -28,21 +29,26 @@ int initializeSocketUDP(char* service) |
28 | 29 | { |
29 | 30 | printf("%d - ", n); |
30 | 31 | if(i->ai_family==AF_INET) |
32 | + { | |
31 | 33 | resultat=i; |
32 | - //printf("Test : %d", origine->ai_addrlen); | |
33 | - | |
34 | + printf("Test : %d", origine->ai_addrlen); | |
35 | + } | |
36 | + | |
34 | 37 | printf("\n"); |
35 | 38 | n++; |
36 | 39 | } |
37 | - | |
40 | + | |
41 | + struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr); | |
42 | + printf("Addr : %x\n", test->sin_addr.s_addr); | |
38 | 43 | if((sock_id=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); } |
39 | 44 | |
40 | 45 | int val=1; |
41 | 46 | |
42 | - if(setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR|SO_DEBUG, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
43 | - | |
44 | - if(bind(sock_id, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); } | |
45 | - | |
47 | + if(setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR|SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
48 | + | |
49 | + if(bind(sock_id, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); } | |
50 | + | |
51 | + | |
46 | 52 | freeaddrinfo(origine); |
47 | 53 | |
48 | 54 | initialized=1; |
... | ... | @@ -52,41 +58,43 @@ int initializeSocketUDP(char* service) |
52 | 58 | |
53 | 59 | int sendUDPBroadcast(char* message, int port) |
54 | 60 | { |
55 | - static struct sockaddr_in* addrUDP; | |
56 | - addrUDP->sin_family=AF_INET; | |
57 | - addrUDP->sin_port=(uint16_t)port; | |
61 | + static struct sockaddr_in addrUDP; | |
62 | + addrUDP.sin_family=AF_INET; | |
63 | + addrUDP.sin_port=(uint16_t)port; | |
58 | 64 | |
59 | 65 | struct in_addr tmpadd={0xffffffff}; |
60 | - addrUDP->sin_addr=tmpadd; | |
61 | - | |
66 | + addrUDP.sin_addr=tmpadd; | |
67 | + | |
62 | 68 | if(initialized!=1) |
63 | 69 | { |
64 | - char* tmp_str; | |
70 | + printf("Initialize via Broadcast\n"); | |
71 | + char tmp_str[15]; | |
65 | 72 | sprintf(tmp_str, "%d", port); |
66 | 73 | initializeSocketUDP( tmp_str ); |
67 | 74 | } |
68 | 75 | |
69 | - sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) addrUDP, sizeof(addrUDP)); | |
76 | + if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);} | |
70 | 77 | } |
71 | 78 | |
72 | 79 | int sendUDPUnicast(char* address, char* message, int port) |
73 | 80 | { |
74 | - static struct sockaddr_in* addrUDP; | |
75 | - addrUDP->sin_family=AF_INET; | |
76 | - addrUDP->sin_port=(uint16_t)port; | |
81 | + static struct sockaddr_in addrUDP; | |
82 | + addrUDP.sin_family=AF_INET; | |
83 | + addrUDP.sin_port=(uint16_t)port; | |
77 | 84 | |
78 | - inet_aton(address, &(addrUDP->sin_addr)); // Converti l'écriture string de format a.b.c.d en format Internet | |
85 | + inet_aton(address, &(addrUDP.sin_addr)); // Converti l'écriture string de format a.b.c.d en format Internet | |
79 | 86 | |
80 | 87 | |
81 | 88 | |
82 | 89 | if(initialized!=1) |
83 | 90 | { |
84 | - char* tmp_str; | |
91 | + printf("Initialize via Unicast\n"); | |
92 | + char tmp_str[15]; | |
85 | 93 | sprintf(tmp_str, "%d", port); |
86 | 94 | initializeSocketUDP( tmp_str ); |
87 | 95 | } |
88 | 96 | |
89 | - sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) addrUDP, sizeof(addrUDP)); | |
97 | + if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto\n"); return(-1); } | |
90 | 98 | } |
91 | 99 | |
92 | 100 | |
... | ... | @@ -94,6 +102,8 @@ int sendUDPUnicast(char* address, char* message, int port) |
94 | 102 | |
95 | 103 | int main(void) |
96 | 104 | { |
97 | - | |
105 | + sendUDPBroadcast("Broadwesh", 2020); | |
106 | + sendUDPUnicast("255.255.255.255", "Uniwesh", 2020); | |
98 | 107 | return(1); |
108 | + close(sock_id); | |
99 | 109 | } | ... | ... |
Network/test.c deleted
... | ... | @@ -0,0 +1,65 @@ |
1 | +#include <sys/types.h> | |
2 | +#include <sys/socket.h> | |
3 | +#include <stdio.h> | |
4 | +#include <stdlib.h> | |
5 | +#include <unistd.h> | |
6 | +#include <string.h> | |
7 | +#include <netdb.h> | |
8 | +#include <arpa/inet.h> | |
9 | + | |
10 | + | |
11 | + | |
12 | +int connexionServeur(char* host, char* service) | |
13 | +{ | |
14 | + int sock_fd; | |
15 | + | |
16 | + 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 | |
17 | + | |
18 | + memset(&precisions, 0, sizeof precisions); | |
19 | + precisions.ai_family = AF_UNSPEC; | |
20 | + precisions.ai_socktype = SOCK_STREAM; | |
21 | + | |
22 | + if(getaddrinfo(host, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } | |
23 | + | |
24 | + int n=0; | |
25 | + | |
26 | + for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next) | |
27 | + { | |
28 | + printf("%d - ", n); | |
29 | + if(i->ai_family==AF_INET) | |
30 | + { | |
31 | + resultat=i; | |
32 | + printf("Test : %d", origine->ai_addrlen); | |
33 | + } | |
34 | + | |
35 | + printf("\n"); | |
36 | + n++; | |
37 | + } | |
38 | + | |
39 | + struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr); | |
40 | + printf("Addr : %x\n", test->sin_addr.s_addr); | |
41 | + if((sock_fd=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); } | |
42 | + | |
43 | + if(connect(sock_fd, resultat->ai_addr, resultat->ai_addrlen) < 0) return(-1); | |
44 | + | |
45 | + freeaddrinfo(origine); | |
46 | + | |
47 | + return sock_fd; | |
48 | +} | |
49 | + | |
50 | + | |
51 | +int main(void) | |
52 | +{ | |
53 | + int sock_fd = connexionServeur("localhost", "2020"); | |
54 | + if(sock_fd<0) { fprintf(stderr, "Erreur connexionServeur\n"); return(-1); } | |
55 | + | |
56 | + struct pollfd pipe[2] | |
57 | + pipe[0].fd = sock_fd; | |
58 | + pipe[0].events = POLLIN; | |
59 | + pipe[1].fd = 0; | |
60 | + pipe[1].events = POLLIN; | |
61 | + | |
62 | + return 0; | |
63 | +} | |
64 | + | |
65 | + | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,85 @@ |
1 | +#include <sys/types.h> | |
2 | +#include <sys/socket.h> | |
3 | +#include <stdio.h> | |
4 | +#include <stdlib.h> | |
5 | +#include <unistd.h> | |
6 | +#include <string.h> | |
7 | +#include <netdb.h> | |
8 | +#include <arpa/inet.h> | |
9 | + | |
10 | + | |
11 | + | |
12 | +int initialisationServeur(char* service) | |
13 | +{ | |
14 | + int sock_fd; | |
15 | + | |
16 | + 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 | |
17 | + | |
18 | + memset(&precisions, 0, sizeof precisions); | |
19 | + precisions.ai_family = AF_UNSPEC; | |
20 | + precisions.ai_socktype = SOCK_STREAM; | |
21 | + precisions.ai_flags = AI_PASSIVE; | |
22 | + | |
23 | + if(getaddrinfo(NULL, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } | |
24 | + | |
25 | + int n=0; | |
26 | + | |
27 | + for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next) | |
28 | + { | |
29 | + printf("%d - ", n); | |
30 | + if(i->ai_family==AF_INET) | |
31 | + { | |
32 | + resultat=i; | |
33 | + printf("Test : %d", origine->ai_addrlen); | |
34 | + } | |
35 | + | |
36 | + printf("\n"); | |
37 | + n++; | |
38 | + } | |
39 | + | |
40 | + struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr); | |
41 | + printf("Addr : %x\n", test->sin_addr.s_addr); | |
42 | + if((sock_fd=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); } | |
43 | + | |
44 | + int val=1; | |
45 | + | |
46 | + if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR|SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
47 | + | |
48 | + | |
49 | + | |
50 | + if(bind(sock_fd, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); } | |
51 | + | |
52 | + if(listen(sock_fd, 20) < 0) { fprintf(stderr, "Error listen server socket\n"); return (-1); } | |
53 | + | |
54 | + freeaddrinfo(origine); | |
55 | + | |
56 | + return sock_fd; | |
57 | +} | |
58 | + | |
59 | +void mesCouilles(int sock) | |
60 | +{ | |
61 | + printf("Connected\n"); | |
62 | +} | |
63 | + | |
64 | +int boucleServeur(int socket, void(* fctConnex)(int)) | |
65 | +{ | |
66 | + int sock_dial; | |
67 | + while(1) | |
68 | + { | |
69 | + printf("MAH !\n"); | |
70 | + if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } | |
71 | + printf("ACCEPTED\n"); | |
72 | + | |
73 | + fctConnex(sock_dial); | |
74 | + } | |
75 | +} | |
76 | + | |
77 | + | |
78 | +int main(void) | |
79 | +{ | |
80 | + int sock_fd=initialisationServeur("2020"); | |
81 | + boucleServeur(sock_fd, mesCouilles); | |
82 | + return 0; | |
83 | +} | |
84 | + | |
85 | + | ... | ... |