voila mes code server et client :
server.c
client.c
Code : 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 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> int main(int argc,char **argv){ int id, id_clt; char message[20] ; char *nickname ; struct sockaddr_in my_addr; struct sockaddr_in clt_addr; id = socket(AF_INET, SOCK_STREAM , 0); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(atoi(argv[1])); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); if( bind(id, (struct sockaddr *) &my_addr,sizeof(my_addr)) < 0 ){ printf("bind() ERROR\n"); exit(0); } if( listen(id, 5) < 0 ) printf("listen() ERROR\n"); int taille = sizeof(clt_addr); if( (id_clt = accept(id, (struct sockaddr*)&clt_addr, &taille)) < 0) printf ("accept() ERROR\n"); printf("Machine %s Connected !\n",(char *)inet_ntoa(clt_addr.sin_addr)); if( recv(id_clt, nickname, 100, 0) == -1){ printf("recv ERROR\n"); exit(0); } }
et voila l'execution !!!
Code : 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 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> int main(int argc,char **argv){ int id ; struct sockaddr_in my_addr; struct in_addr ip ; char message[100] ,nickname[100] ; inet_aton("127.0.0.1", &ip); id = socket(AF_INET, SOCK_STREAM , 0); int inet_aton(const char *cp, struct in_addr *inp); my_addr.sin_family = AF_INET ; my_addr.sin_port = htons(atoi(argv[1])); my_addr.sin_addr = ip ; if( connect(id, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0 ){ printf("connect() ERROR\n"); exit(0); } printf("Saisir votre nickname : "); gets(nickname); send(id, nickname, strlen(nickname), 0) ; }
un comportement bizzard !!!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Saisir votre nickname : yous
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 Machine 127.0.0.1 Connected ! recv ERROR
Partager