Bonjour,
je débute sur les sockets que j'utilise dans le cadre d'un projet étudiant.
En faisant des recherches sur internet, et en regardant de près, j'ai réussi à obtenir un code client et un code serveur, et à les manipuler de façon à envoyer un caractère par un caractère, le but étant que mon client face une gestion de clavier touche par touche, et que mon serveur récupère ces données pour pouvoir les comparer et les manipuler.
Hors, mon serveur reçoit parfaitement les données, mais les supprime à chaque fin de boucle du coup je n'ai pas le temps de les manipuler.. (dur à expliquer ^^')..
Hors ce que j'aimerais c'est que le serveur effectue une opération lorsque la touche 'm' a été pressée du coté du client. (dans le code suivant je me contente de quitter le programme une fois la touche 'm' préssée, mais cela ne marche pas..)
Je vous mets mon code, ce sera surement plus parlant.
client :
serveur :
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
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 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <termios.h> static struct termios configuration_initiale; static struct termios configuration_brute; static void initialisation_configuration_clavier(); static void initialisation_clavier(); static void restauration_clavier(); int getch() { int c; /* on bascule */ initialisation_clavier(); c = getchar(); /* retour au mode normal */ restauration_clavier(); return c; } /* Gestion du clavier en mode brut */ static void initialisation_configuration_clavier() { static char is_init=0; if(!is_init) { is_init = 1; tcgetattr(STDIN_FILENO, &configuration_initiale); configuration_brute = configuration_initiale; cfmakeraw(&configuration_brute); configuration_brute.c_cc[VMIN] = 1; configuration_brute.c_cc[VTIME] = 0; } } static void initialisation_clavier() { initialisation_configuration_clavier(); tcsetattr(STDIN_FILENO, TCSANOW, &configuration_brute); } static void restauration_clavier() { tcsetattr(STDIN_FILENO, TCSANOW, &configuration_initiale); } void error(const char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256] = {0}; while(buffer[0] != 'a'){ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname("localhost"); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(60001); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer, 256); //fgets(buffer,255,stdin); buffer[0] = getch(); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("ERROR writing to socket"); //bzero(buffer,5); n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("%s\n",buffer); } close(sockfd); return 0; }
Si quelqu'un aurait un indice pour m'aider ce serait sympa.. j'ai essayé pas mal de trucs sans succès et je suis à cours d'idées... je suppose que mon problème vient du fork(), et que je manipule mal..
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 /* A simple server in the internet domain using TCP The port number is passed as an argument This version runs forever, forking off a separate process for each connection */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> char dostuff(int); /* function prototype */ void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; char tab[5] = {0}; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(60001); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,30); clilen = sizeof(cli_addr); while (tab[0] != 'm') { // ne fonctionne pas.. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid != 0) { close(sockfd); tab[0] = dostuff(newsockfd); printf(" Voici la valeur de tab : %c \n",tab[0]); if(tab[0]== 'm'){printf("valeur = m !! \n");return 0;} //ne fonctionne pas... exit(0); // ferme le processus et rien n'est fait après. } else close(newsockfd); } /* end of while */ close(sockfd); return 0; /* we never get here */ } /******** DOSTUFF() ********************* There is a separate instance of this function for each connection. It handles all communication once a connnection has been established. *****************************************/ char dostuff (int sock) { int n; char buffer[256]; bzero(buffer,256); n = read(sock,buffer,255); if (n < 0) error("ERROR reading from socket"); n = write(sock,"I got your message",18); if (n < 0) error("ERROR writing to socket"); return buffer[0]; }
Merci à ceux qui pourront m'aider![]()
Partager