Commit b159f2fa4748ff0e05a8e3d1c4c95be6dd681487
1 parent
3b480342
Amélioration serveur
Showing
2 changed files
with
47 additions
and
12 deletions
Show diff stats
Sioux/sioux
No preview for this file type
Sioux/sioux.c
@@ -3,11 +3,12 @@ | @@ -3,11 +3,12 @@ | ||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | +#include <getopt.h> | ||
6 | #include <string.h> | 7 | #include <string.h> |
7 | #include <netdb.h> | 8 | #include <netdb.h> |
8 | #include <arpa/inet.h> | 9 | #include <arpa/inet.h> |
9 | 10 | ||
10 | - | 11 | +#define BUFF_SIZE 100 |
11 | 12 | ||
12 | int initialisationServeur(char* service) | 13 | int initialisationServeur(char* service) |
13 | { | 14 | { |
@@ -56,41 +57,75 @@ int initialisationServeur(char* service) | @@ -56,41 +57,75 @@ int initialisationServeur(char* service) | ||
56 | return sock_fd; | 57 | return sock_fd; |
57 | } | 58 | } |
58 | 59 | ||
59 | -void maFonction(int sock) | 60 | +void reponseConnexion(int sock) |
60 | { | 61 | { |
61 | printf("Connected\n"); | 62 | printf("Connected\n"); |
62 | - char buffer[100]; | 63 | + char buffer[BUFF_SIZE]; |
63 | 64 | ||
64 | FILE* sockdial_stream = fdopen(sock, "a+"); | 65 | FILE* sockdial_stream = fdopen(sock, "a+"); |
65 | 66 | ||
66 | - fgets(buffer, 100, sockdial_stream); | 67 | + while(strcmp(buffer, "EXIT\n")!=0) |
68 | + { | ||
69 | + fgets(buffer, BUFF_SIZE, sockdial_stream); | ||
67 | 70 | ||
68 | - printf("%s\n", buffer); | 71 | + printf("Le message reçu : %s", buffer); |
72 | + | ||
73 | + fprintf(sockdial_stream, "Ceci est une réponse du serveur TCP.\n"); | ||
74 | + | ||
75 | + } | ||
69 | 76 | ||
77 | + printf("Fin de la connexion\n"); | ||
70 | fclose(sockdial_stream); | 78 | fclose(sockdial_stream); |
79 | + | ||
71 | } | 80 | } |
72 | 81 | ||
73 | int boucleServeur(int socket, void(* fctConnex)(int)) | 82 | int boucleServeur(int socket, void(* fctConnex)(int)) |
74 | { | 83 | { |
75 | int sock_dial; | 84 | int sock_dial; |
76 | - | ||
77 | - | ||
78 | 85 | ||
86 | + printf("------- Début boucle serveur TCP -------\n\n"); | ||
87 | + | ||
79 | while(1) | 88 | while(1) |
80 | { | 89 | { |
81 | - printf("MAH !\n"); | 90 | + printf("-- boucle --\n"); |
82 | if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } | 91 | if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } |
83 | - printf("ACCEPTED\n"); | 92 | + printf("Dialogue ACCEPTED\n"); |
84 | 93 | ||
85 | fctConnex(sock_dial); | 94 | fctConnex(sock_dial); |
86 | } | 95 | } |
87 | } | 96 | } |
88 | 97 | ||
98 | +void argPortParsing(int argc, char* argv[], char* port) | ||
99 | +{ | ||
100 | + struct option port_arg={"port", 1, NULL, 'p'}; | ||
101 | + char opt; | ||
102 | + int longindex; | ||
103 | + | ||
104 | + while( (opt=getopt_long(argc, argv, "p:", &port_arg, &longindex)) !='p' && opt!=-1) {} | ||
89 | 105 | ||
90 | -int main(void) | 106 | + if(opt=='p') |
107 | + { | ||
108 | + strcpy(port, optarg); | ||
109 | + printf("%s\n", optarg); | ||
110 | + } | ||
111 | + else | ||
112 | + { | ||
113 | + printf("La syntaxe doit être ./sioux -p <port> ou ./sioux --port <port>"); | ||
114 | + } | ||
115 | +} | ||
116 | + | ||
117 | +int main(int argc, char* argv[]) | ||
91 | { | 118 | { |
92 | - int sock_fd=initialisationServeur("2020"); | ||
93 | - boucleServeur(sock_fd, maFonction); | 119 | + char port[10]="80"; |
120 | + argPortParsing(argc, argv, port); | ||
121 | + | ||
122 | + | ||
123 | + int sock_fd=initialisationServeur(port); | ||
124 | + | ||
125 | + if( sock_fd==-1 ) { fprintf(stderr, "Initialisaiton du serveur impossible\n"); return -1; } | ||
126 | + | ||
127 | + | ||
128 | + boucleServeur(sock_fd, reponseConnexion); | ||
94 | return 0; | 129 | return 0; |
95 | } | 130 | } |
96 | 131 |