Salut à tous,
J'ai un projet à faire pour mes études, et je bloque sur une erreur. Les profs ne sont d'aucun secours et google non plus.
Voici mon code pour le serveur :
Code c : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <errno.h> int main() { //initialisation des variables errno=0; //déclaration de la socket int SocketConnexion, SocketCommunication; struct sockaddr_in SocketConnexionParam, SocketCommunicationParam; //déclaration des chaines reception et émission char *Reception, Charactere; //création de la socket SocketConnexion = socket(PF_INET, SOCK_STREAM, 0); if(SocketConnexion == -1) { printf("erreur sur socket : %s\n", strerror(errno)); return(1); } //initialisation de l'adresse bzero(&SocketConnexionParam, sizeof(SocketConnexionParam)); SocketConnexionParam.sin_family = PF_INET; SocketConnexionParam.sin_port = htons(9100); SocketConnexionParam.sin_addr.s_addr = inet_addr("127.0.0.1"); //liaison entre le nom et la socket if(bind(SocketConnexion, (struct sockaddr *) &SocketConnexionParam, sizeof(SocketConnexionParam)) != 0) { printf("erreur sur bind : %s\n", strerror(errno)); close(SocketConnexion); return(1); } //mise en écoute if(listen(SocketConnexion, 10) != 0) { printf("erreur sur listen : %s\n", strerror(errno)); close(SocketConnexion); return(1); } while(1) { //accepte les connexions entrantes SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, (socklen_t *) sizeof(SocketCommunicationParam)); if(SocketCommunication != -1) { while(1) { while(1) { if (read(SocketCommunication, &Charactere, 1) == 1) {printf("TEST\n"); *Reception++ = Charactere; if (Charactere == '\0') break; } } if(strcmp(Reception, "PING") == 0) { write(SocketCommunication, "PONG\0", 5); } if(strcmp(Reception, "DING") == 0) { write(SocketCommunication, "DONG\0", 5); } if(strcmp(Reception, "FERMETURE") == 0) { close(SocketCommunication); break; } if(strcmp(Reception, "ARRET") == 0) { close(SocketCommunication); close(SocketConnexion); return(0); } }//end while(1) } else { printf("erreur sur accept : %s\n", strerror(errno)); close(SocketConnexion); return(1); } }//end while(1) }
et le code d'un des clients :
Code c : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <errno.h> int main() { //initialisation des variables errno=0; //déclaration de la socket int SocketCommunication; struct sockaddr_in SocketParam; //déclaration des chaines reception et émission char *Reception, Charactere; //création de la socket SocketCommunication = socket(PF_INET, SOCK_STREAM, 0); if(SocketCommunication == -1) { printf("erreur sur socket : %s\n", strerror(errno)); return(1); } //initialisation de l'adresse bzero(&SocketParam, sizeof(SocketParam)); SocketParam.sin_family = PF_INET; SocketParam.sin_port = htons(9100); SocketParam.sin_addr.s_addr = inet_addr("127.0.0.1"); //Connexion if(connect(SocketCommunication, (struct sockaddr *) &SocketParam, sizeof(SocketParam)) == 0) { write(SocketCommunication, "PING\0", 5); printf("PING\n"); while(1) { if (read(SocketCommunication, &Charactere, 1) == 1) {printf("TEST\n"); *Reception++ = Charactere; if (Charactere == '\0') break; } } printf("%s\n", Reception); if(strcmp(Reception, "PONG") == 0) {printf("fermeture"); write(SocketCommunication, "FERMETURE\0", 10); } close(SocketCommunication); } else { printf("erreur sur connect : %s\n", strerror(errno)); return(1); } return(0); }
Lors du lancement du serveur, il n'y a aucune erreur. Mais lorsque le client est lancé et tente de se connecter au serveur, celui ci renvois une erreur lors de l'accept() :
Pourtant le client ne renvois aucune erreur sur le connect().[toto@tata]$ ./Serveur
erreur sur accept : Bad address
[toto@tata]$
Est-ce que quelqu'un aurait une idée du pourquoi du comment que ca marche pas ?
Partager