-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsock_v2.c
256 lines (220 loc) · 6.23 KB
/
tsock_v2.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* librairie standard ... */
#include <stdlib.h>
/* pour getopt */
#include <unistd.h>
/* déclaration des types de base */
#include <sys/types.h>
/* constantes relatives aux domaines, types et protocoles */
#include <sys/socket.h>
/* constantes et structures propres au domaine UNIX */
#include <sys/un.h>
/* constantes et structures propres au domaine INTERNET */
#include <netinet/in.h>
/* structures retournées par les fonctions de gestion de la base de
données du réseau */
#include <netdb.h>
/* pour les entrées/sorties */
#include <stdio.h>
/* pour la gestion des erreurs */
#include <errno.h>
void construire_message(char *message, char motif, int lg) {
int i;
for (i=0;i<lg;i++){
message[i] = motif;
}
}
void afficher_message(char *message, int lg) {
int i;
printf("message construit : ");
for (i=0;i<lg;i++) {
printf("%c", message[i]);
printf("\n");
}
}
void main (int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind;
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
int source = -1 ; /* 0=puits, 1=source */
int mode = -1 ; /* 0=UDP, 1=TCP */
int sock ;
int port ;
struct hostent * hp ;
struct sockaddr_in adr_local ;
struct sockaddr_in adr_distant ;
struct sockaddr_in adr_em ;
int lg_mesg = 40 ;
int lg_max = 100 ;
char * pmesg = malloc(lg_mesg) ;
int lg_adr_em=sizeof(adr_em) ;
int lg_adr_local=sizeof(adr_local) ;
int lg_adr_dist=sizeof(adr_distant) ;
int nb_max_connexion = 5 ;
while ((c = getopt(argc, argv, "pn:su")) != -1) {
switch (c) {
case 'p':
if (source == 1) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1);
}
source = 0;
break;
case 's':
if (source == 0) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1) ;
}
source = 1;
break;
case 'n':
nb_message = atoi(optarg);
break;
case 'u':
mode = 0;
break;
default:
printf("usage: cmd [-p|-s][-n ##]\n");
break;
}
}
if (source == -1) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1) ;
}
port = atoi(argv[argc-1]);
port = htons(port);
if (mode == 0){
if (source == 1){
printf("On est dans la source\n");
if ((sock = socket(AF_INET, SOCK_DGRAM, 0))==-1){
printf("échec de création du socket\n");
exit(1);
}
memset((char*)&adr_distant, 0, lg_adr_dist); /*reset socket distant*/
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
if ((hp = gethostbyname(argv[argc-2]))==NULL){
printf("erreur gethostbyname\n");
exit(1);
}
memcpy((char*)&(adr_distant.sin_addr.s_addr),
hp->h_addr,
hp->h_length);
nb_message=10;
printf("Le nombre de tampon à envoyer est %d.\n",nb_message);
for (int i=0;i<nb_message;i++){
construire_message(pmesg, (char)((i%26)+97), lg_mesg);
afficher_message(pmesg,lg_mesg);
sendto(sock,pmesg,lg_mesg,0,(struct sockaddr *)&adr_distant,lg_adr_dist);
}
if(close(sock)==-1){
printf("échec de destruction du socket\n");
exit(1);
}
}
if (source == 0){
printf("On est dans le puits\n");
if ((sock = socket(AF_INET, SOCK_DGRAM, 0))==-1){
printf("échec de création du socket\n");
exit(1);
}
memset((char*)&adr_local, 0, lg_adr_local); /*reset socket local*/
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
adr_local.sin_addr.s_addr = INADDR_ANY ;
if(bind(sock, (struct sockaddr *)&adr_local, lg_adr_local)==-1){
printf("échec du bind\n");
exit(1);
}
while ((lg_mesg=recvfrom(sock, pmesg, lg_max,0,(struct sockaddr *)&adr_em, &lg_adr_em)) != -1){
afficher_message(pmesg,lg_mesg);
}
if(close(sock)==-1){
printf("échec de destruction du socket\n");
exit(1);
}
}
}
if (mode == 1){
if (source == 1){
printf("On est dans la source\n");
if ((sock = socket(AF_INET, SOCK_STREAM, 0))==-1){
printf("échec de création du socket\n");
exit(1);
}
memset((char*)&adr_distant, 0, lg_adr_dist); /*reset socket distant*/
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
if ((hp = gethostbyname(argv[argc-2]))==NULL){
printf("erreur gethostbyname\n");
exit(1);
}
memcpy((char*)&(adr_distant.sin_addr.s_addr),
hp->h_addr,
hp->h_length);
if (connect(sock,(struct sockaddr*)&adr_distant, lg_adr_dist) == -1){
printf("échec de la connexion\n");
exit(1);
}
nb_message=10;
printf("Le nombre de tampon à envoyer est %d.\n",nb_message);
for (int i=0;i<nb_message;i++){
construire_message(pmesg, (char)((i%26)+97), lg_mesg);
afficher_message(pmesg,lg_mesg);
sendto(sock,pmesg,lg_mesg,0,(struct sockaddr *)&adr_distant,lg_adr_dist);
}
if(close(sock)==-1){
printf("échec de destruction du socket\n");
exit(1);
}
}
else{
printf("On est dans le puits\n");
if ((sock = socket(AF_INET, SOCK_STREAM, 0))==-1){
printf("échec de création du socket\n");
exit(1);
}
memset((char*)&adr_local, 0, lg_adr_local); /*reset socket local*/
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
adr_local.sin_addr.s_addr = INADDR_ANY ;
if(bind(sock, (struct sockaddr *)&adr_local, lg_adr_local)==-1){
printf("échec du bind\n");
exit(1);
}
if (listen(sock, nb_max_connexion) == -1) {
printf("échec du listen\n");
exit(1);
}
if(accept(sock,(struct sockaddr *)&adr_em, &lg_adr_em) == -1) {
printf("Échec du accept\n");
exit(1);
}
while ((lg_mesg=recvfrom(sock, pmesg, lg_max,0,(struct sockaddr *)&adr_em, &lg_adr_em)) != -1){
afficher_message(pmesg,lg_mesg);
}
if(close(sock)==-1){
printf("échec de destruction du socket\n");
exit(1);
}
}
}
if (source == 1)
printf("on est dans le source\n");
else
printf("on est dans le puits\n");
if (nb_message != -1) {
if (source == 1)
printf("nb de tampons à envoyer : %d\n", nb_message);
else
printf("nb de tampons à recevoir : %d\n", nb_message);
} else {
if (source == 1) {
nb_message = 10 ;
printf("nb de tampons à envoyer = 10 par défaut\n");
} else
printf("nb de tampons à envoyer = infini\n");
}
}