-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
224 lines (190 loc) · 6.16 KB
/
client.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string.h>
#define DEBUG 0
int main(int argc, char *argv[]) {
char buffer[1024],texte[200],res_http[1024],requete_http[200],token[200],recep_msg[100];
char add_serveur[200], chooseAddr[200];
int port=-1, choosePort = -1, chooseHttp=0;
//Caractere choisissant le traitement
char char_consonant='+';
char char_vowel='-';
char char_number='?';
char char_value='=';
int sock,i,c, menu = 0, mode_http=0, resultat_traitement=-1;
struct sockaddr_in addr;
struct hostent *entree;
if(argc == 2 && !strcmp(argv[1], "-v")) {
menu = 1;
} else if(argc > 2 && !strcmp(argv[1], "-o")) {
chooseHttp=1;
if(DEBUG) printf("ONLY HTTP\n");
} else if(argc < 3) {
printf("\nUsage:\n client [option] <server address> <port> [<command><sentence>...]\n\n");
printf("Commands:\n %c\t\tcount number of consonant\n"
" %c\t\tcount number of vowel\n"
" %c\t\tcount number of letter (address and port doesn\'t matter)\n"
" %c\t\tcount value of the sentence (address and port doesn\'t matter)\n"
"\nOptions:\n -v\t\tuse verbose mode with menu\n -o\t\tuse only http requests\n\n",
char_consonant,char_vowel,char_number,char_value);
exit(1);
}
int nbCom = (argc==3 || menu) ? (chooseHttp?argc-2:1) : (chooseHttp?argc-2:argc - 3),
iteArg = (chooseHttp?2:3);
if(DEBUG) printf("Number of commands: %d %d\n", nbCom, iteArg);
while(nbCom) {
mode_http=0; // Pour eviter tout traitement non prevu
bzero(texte,sizeof(texte));
bzero(buffer,sizeof(buffer));
bzero(res_http,sizeof(res_http));
if(menu){ // cas du menu
int choice = 0;
while(!choice) {
printf("Possible operations:\n\t1- Consonant count\n\t2- Vowel count\n\t3- Characters count\n\t4- Value count\n\nYour choice?\n>>> ");
bzero(texte,sizeof(texte));
i = 0;
while((c=getchar()) != '\n')
texte[i++]=c;
choice = texte[0] - '0';
choice = (choice > 0 && choice < 5) ? choice : 0;
}
bzero(texte,sizeof(texte));
// ajout du caractere de la commande
switch(choice) {
//Commandes ssh
case 1:
texte[0] = char_consonant;i = 1;
break;
case 2:
texte[0] = char_vowel;
break;
//Commandes http
case 3:
texte[0] = char_number;
break;
case 4:
texte[0] = char_value;
break;
default:
printf("Bad entry.\n");
break;
}
printf("\nEnter your sentence:\n>>> ");
i = 1;
while((c=getchar()) != '\n'){
texte[i++]=c;
}
} else if(argc==3 && !chooseHttp) { // cas sans le menu
i = 0;
printf("Enter your command:\n>>> ");
while((c=getchar()) != '\n'){
texte[i++]=c;
}
} else { // cas des commandes en ligne de commande
strcpy(texte, argv[iteArg++]);
}
//Test mode HTTP
mode_http = texte[0]==char_number || texte[0]==char_value;
if(!mode_http && !chooseHttp){ //Connection ssh
texte[strlen(texte)]='\n';
texte[strlen(texte)+1]='\0';
if(menu) {
printf("\nEnter server's address:\n>>> ");
i = 0;
while((c=getchar()) != '\n'){
chooseAddr[i++]=c;
}
chooseAddr[i]='\0';
printf("\nEnter server's port:\n>>> ");
if(!scanf("%d", &choosePort)){
printf("Scanf error");
exit(1);
}
}
port=(menu?choosePort:atoi(argv[2]));
strcpy(add_serveur,(menu?chooseAddr:argv[1]));
}else{ //Connection http
port=80;
strcpy(add_serveur,"www.isima.fr");
if(texte[0]==char_number){
// Requete GET => compteur de lettre
sprintf(requete_http,"GET /~laurenco/ZZ2/nb_lettre_get.php?phrase=%s HTTP/1.1\r\n"
"Host: www.isima.fr\r\n"
"\r\n\r\n",texte+1);
if(DEBUG) printf("Http request:\n%s",requete_http);
strcpy(token,"La phrase comporte : ");
strcpy(recep_msg,"Number of letters: ");
}else if(texte[0]==char_value){
// Requete POST => valeur phrase
sprintf(requete_http,"POST /~laurenco/ZZ2/val_phrase_post.php HTTP/1.1\r\n"
"Host: www.isima.fr\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: %d\r\n"
"\r\n"
"phrase=%s\r\n",(int)(7+strlen(texte))-1,texte+1);
if(DEBUG) printf("Requete http :\n%s",requete_http);
strcpy(token,"La phrase a une valeur de : ");
strcpy(recep_msg,"Value of sentence: ");
}else{
printf("Warning, Error in HTTP mode\nreception of character %c\n",texte[0]);
}
// On donne a la variable d'envoie la requete http
strcpy(texte,requete_http);
}
// Creation de la socket
addr.sin_port=htons(port);
addr.sin_family=AF_INET;
entree=(struct hostent *)gethostbyname(add_serveur);
if (entree==NULL){
printf("Connexion host problem! %s unreachable\n",add_serveur);
exit(1);
}
bcopy((char *)entree->h_addr,(char *)&addr.sin_addr,entree->h_length);
sock= socket(AF_INET,SOCK_STREAM,0);
if(connect(sock, (struct sockaddr *)&addr,sizeof(struct sockaddr_in)) < 0) {
printf("Connexion problem!\n");
exit(1);
}
printf("Connexion established.\n");
// Envoie
if(send(sock,texte,strlen(texte),0)){
// Reception
if(!mode_http){
recv(sock,buffer,sizeof(buffer),0);
printf("Server's message:\n>>> %s\n",buffer);
bzero(texte,sizeof(texte));
strcpy(texte,"/\0");
send(sock,texte,strlen(texte)+1,0);
}else{
/*while(recv(sock,buffer,sizeof(buffer),0)){
strcat(res_http,buffer);
}*/
recv(sock,buffer,sizeof(buffer),0);
strcpy(res_http,buffer);
if(res_http==NULL){
printf("No HTTP server response\n");
}
// Traitement de la reponse du serveur HTTP
if(DEBUG) printf("\n%s\n",res_http);
resultat_traitement = -1;
if(res_http!=NULL){
// On parse le resultat renvoye par le serveur distant
char* temp =strstr(res_http,token);
strcat(token,"%d");
sscanf(temp,token,&resultat_traitement);
// On l'affiche
printf("Server's message:\n>>>%s%d\n",recep_msg,resultat_traitement);
}
}
}else{
printf("Send error");
exit(1);
}
nbCom--;
}
//close(sock);
return 0;
}