Bonjour,
je me suis trompé en postant sur une autre partie du forum, veillez m'excuser, je suis nouveau et le forum est tellement grand qu'on se trompe vite.Donc voila je reposte:

je suis nouveau sur ce forum et je débute dans la programmation C++, et j'ai vraiment besoin de votre aide.
je développe une application Client/Serveur, où le client telecharge un fichier du serveur en incluant son chemin.J'ai suivi les insitruction récupérés sur le site Code Soources et ça marche nikel.

seulement le problème est le suivant: je dois faire en sorte que le client au lieux de telecharger un seul fichier, qu'il télécharge tous les fichiers de la même extention présents dans le répértoire courant (par exemple en tappant *.txt pour télécharger tous les fichiers .txt), les code sont ci dessous si quelqu'un peut m'aider à modifier mon programme je vous en serrez reconnaissant

client:
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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
 
#include <shlwapi.h> // Pour utiliser la fonction PathFindFileName
#include <winsock2.h>
//#pragma comment(lib,"wsock32.lib")	// Librairie Associé au Socket
//#pragma comment(lib,"shlwapi.lib")
 
 
#define CARACTERE_OK '1'
 
__int64 __stdcall taillefichier(char *pszfl)
{
    LARGE_INTEGER r;
    WIN32_FIND_DATA wfd;
    HANDLE hfl = FindFirstFile(pszfl, &wfd);
    if(hfl == INVALID_HANDLE_VALUE) return 0;
    FindClose(hfl);
    r.HighPart = wfd.nFileSizeHigh;
    r.LowPart = wfd.nFileSizeLow;
    return r.QuadPart;
}
 
int main(int argc, char *argv[])
{
    // Initialisation
    WSADATA initialisation_win32;
    int erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
    if (erreur!=0)
    {
        cout << "Desole, je ne peux pas initialiser Winsock du a l'erreur : ";
        cout << erreur << " " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "WSAStartup  : OK\n";
    }
 
    // Création de la socket
    SOCKET id_de_la_socket;
    id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
    if (id_de_la_socket==INVALID_SOCKET)
    {
        cout << "Desole, je ne peux pas creer la socket du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "socket  : OK\n";
    }
 
    char adr_ip[4096];adr_ip[0]=0;
    while (strlen(adr_ip)==0)
    {
        cout << "Entrez l\'adresse IP du serveur : ";
        cin.getline(adr_ip,sizeof(adr_ip));
    }
 
    // Connection au serveur
    SOCKADDR_IN information_sur_la_destination;
    information_sur_la_destination.sin_family=AF_INET;
    information_sur_la_destination.sin_addr.s_addr=inet_addr(adr_ip);
    information_sur_la_destination.sin_port=htons(47836);
    erreur=connect(id_de_la_socket,(struct sockaddr*)&information_sur_la_destination,sizeof(information_sur_la_destination));
    if (erreur!=0)
    {
        cout << "Desole, je n'ai pas pu ouvrir la session TCP : ";
        cout << erreur << " " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "setsockopt  : OK\n";
    }
 
    char adr_fic[4096];adr_fic[0]=0;
    while (strlen(adr_fic)==0)
    {
        cout << "Entrez le chemin du fichier : ";
        cin.getline(adr_fic,sizeof(adr_fic));
    }
 
    ifstream fichiero(adr_fic,ios::in|ios::binary);
    if (!fichiero)
    {
        cout << "Erreur, fichier non trouve ou non accessible !\n\n";
        shutdown(id_de_la_socket,2);
        closesocket(id_de_la_socket);
        system("PAUSE");
        return -1;
    }
 
    char adr_fic2[4096];
    char*adr_fic3 = PathFindFileName(adr_fic); // Retourne uniquement le nom du fichier
    strcpy(adr_fic2,adr_fic3); // Pour être sûr de ne pas avoir de plantage
 
    // Envoi la longueur du nom de fichier
    long longnomfich=strlen(adr_fic2);
    int nombre_de_caractere=sendto(id_de_la_socket,(char*)&longnomfich,4,0
    ,(struct sockaddr*)&information_sur_la_destination,
    sizeof(information_sur_la_destination));
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "sendto  : OK\n";
    }
 
    // -------------- Confirmation de réception --------------------------------
    cout << "Reception de confirmation (1)...";
    SOCKADDR_IN information_sur_la_source;
    information_sur_la_source.sin_family=AF_INET;
    information_sur_la_source.sin_addr.s_addr=INADDR_ANY;
    information_sur_la_source.sin_port=htons(47836); // Le porte 47836 est écouté
    int tempo=sizeof(information_sur_la_source);
    char confirm;
    nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&confirm,1,0
    ,(struct sockaddr*)&information_sur_la_source,&tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Erreur : " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else if (confirm!=CARACTERE_OK)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Explication : Mauvaise confirmation\n\n";
        system("PAUSE");
        return -1;
    }
    cout << "\tOK\n";
    // -------------- Fin de Confirmation de réception -------------------------
 
    // Envoi le nom du fichier
    nombre_de_caractere=sendto(id_de_la_socket,adr_fic2,longnomfich,0,
    (struct sockaddr*)&information_sur_la_destination,
    sizeof(information_sur_la_destination));
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "sendto2  : OK\n";
    }
 
 
    // -------------- Confirmation de réception --------------------------------
    cout << "Reception de confirmation (2)...";
    nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&confirm,1,0
    ,(struct sockaddr*)&information_sur_la_source,&tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Erreur : " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else if (confirm!=CARACTERE_OK)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Explication : Mauvaise confirmation\n\n";
        system("PAUSE");
        return -1;
    }
    cout << "\tOK\n";
    // -------------- Fin de Confirmation de réception -------------------------
 
    // Envoi la taille du fichier
    long longfich=taillefichier(adr_fic);
    if (longfich==0)
        cout << "Erreur, taille du fichier nulle !\nEnvoie non annulee.";
    nombre_de_caractere=sendto(id_de_la_socket,(char*)&longfich,4,0
    ,(struct sockaddr*)&information_sur_la_destination,
    sizeof(information_sur_la_destination));
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "sendto3  : OK\n";
    }
 
    // -------------- Confirmation de réception --------------------------------
    cout << "Reception de confirmation (3)...";
    nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&confirm,1,0
    ,(struct sockaddr*)&information_sur_la_source,&tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Erreur : " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else if (confirm!=CARACTERE_OK)
    {
        cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
        cout << "Explication : Mauvaise confirmation\n\n";
        system("PAUSE");
        return -1;
    }
    cout << "\tOK\n";
    // -------------- Fin de Confirmation de réception -------------------------
 
    // Envoi du fichier
    char buffer[4096];
    for (int i=0;i<longfich;i+=4096)
    {
        long nbdonneaenv = (longfich-i>4096)?4096:longfich-i;
        fichiero.read(buffer,nbdonneaenv);
        //cout << "Envoi : " << buffer << endl;
        nombre_de_caractere=sendto(id_de_la_socket,buffer,nbdonneaenv,0
        ,(struct sockaddr*)&information_sur_la_destination,
        sizeof(information_sur_la_destination));
        if (nombre_de_caractere==SOCKET_ERROR)
        {
            cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
            cout << WSAGetLastError() << "\n\n";
            system("PAUSE");
            return -1;
        }
        // -------------- Confirmation de réception --------------------------------
        cout << "Reception de confirmation (4-" << i << "-" << nombre_de_caractere;
        cout << ")...";
        int nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&confirm,1,0
        ,(struct sockaddr*)&information_sur_la_source,&tempo);
        if (nombre_de_caractere==SOCKET_ERROR)
        {
            cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
            cout << "Erreur : " << WSAGetLastError() << "\n\n";
            system("PAUSE");
            return -1;
        }
        else if (confirm!=CARACTERE_OK)
        {
            cout << "Erreur, je n'ai pas recu la confirmation de reception !\n";
            cout << "Explication : Mauvaise confirmation\n\n";
            system("PAUSE");
            return -1;
        }
        cout << "\tOK\n";
        // -------------- Fin de Confirmation de réception -------------------------
    }
 
    shutdown(id_de_la_socket,2);
    closesocket(id_de_la_socket);
 
    cout << "\nFichier envoye avec succes !\n\n";
    system("PAUSE");
    return 0;
}
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
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
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
#include <winsock2.h>
 
#define CARACTERE_OK '1'
 
int main(int argc, char *argv[])
{
    // Initialisation
    WSADATA initialisation_win32;
    int erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
    if (erreur!=0)
    {
        cout << "Desole, je ne peux pas initialiser Winsock du a l'erreur : ";
        cout << erreur << " " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "WSAStartup  : OK\n";
    }
 
    // Création de la socket
    SOCKET id_de_la_socket;
    id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
    if (id_de_la_socket==INVALID_SOCKET)
    {
        cout << "Desole, je ne peux pas creer la socket du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "socket  : OK\n";
    }
 
    // Ouverture du port 47836
    SOCKADDR_IN information_sur_la_source;
    information_sur_la_source.sin_family=AF_INET;
    information_sur_la_source.sin_addr.s_addr=INADDR_ANY;
    information_sur_la_source.sin_port=htons(47836); // Le porte 47836 est écouté
    erreur=bind(id_de_la_socket,(struct sockaddr*)&information_sur_la_source,
    sizeof(information_sur_la_source));
    if (erreur!=0)
    {
        cout << "Desole, je ne peux pas ecouter ce port : ";
        cout << erreur << " " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "bind  : OK\n";
    }
 
    char buffer[4096]; long buffer2;int tempo=sizeof(information_sur_la_source);
    // Reçois la taille du nom du fichier (4 octet ou 32 bits)
    int nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&buffer2,4,0
    ,(struct sockaddr*)&information_sur_la_source,&tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Erreur, je n'ai pas recu la taille du nom de fichier !\n";
        cout << "Erreur : " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
 
    // ----------- Envoi la confirmation de réception --------------------------
    char confirm = CARACTERE_OK;
    nombre_de_caractere=sendto(id_de_la_socket,(char*)&confirm,1,0
    ,(struct sockaddr*)&information_sur_la_source,tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "sendto1  : OK\n";
    }
    // ----------- Fin d'envoi la confirmation de réception --------------------
 
    // Reçois le nom du fichier
    char*nomdefichier=new char[buffer2+1];nomdefichier[0]=0;
    for (int i=0;i<buffer2;i+=nombre_de_caractere)
    {
        nombre_de_caractere=recvfrom(id_de_la_socket,buffer,
        (buffer2-i<4096)?buffer2-i:4096// Si le nombre de donnée restant à reçevoir
        // est plus petit que le buffer on indique ce nombre sinon la taille du buffer
        ,0,(struct sockaddr*)&information_sur_la_source,&tempo);
        if (nombre_de_caractere==SOCKET_ERROR)
        {
            cout << "Erreur, je n'ai pas recu le nom du fichier !\n\n";
            system("PAUSE");
            return -1;
        }
        else
        {
            for (int y=0;y<nombre_de_caractere;y++)
            {
                nomdefichier[y+i]=buffer[y];
                nomdefichier[y+i+1]=0;
            }
        }
        // ----------- Envoi la confirmation de réception ----------------------
        int nombre_de_caractere2=sendto(id_de_la_socket,(char*)&confirm,1,0
        ,(struct sockaddr*)&information_sur_la_source,tempo);
        if (nombre_de_caractere2==SOCKET_ERROR)
        {
            cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
            cout << WSAGetLastError() << "\n\n";
            system("PAUSE");
            return -1;
        }
        else
        {
              cout << "sendto2-" << i << " (" << nombre_de_caractere << ")  : OK\n";
        }
        // ----------- Fin d'envoi la confirmation de réception ----------------
    }
    cout << "Nom du fichier a recevoir : " << nomdefichier << "\n";
 
    ofstream fichiers(nomdefichier,ios::out|ios::binary);
    if (!fichiers)
    {
        cout << "Erreur, impossible de créer le fichier !\n\n";
        shutdown(id_de_la_socket,2);
        closesocket(id_de_la_socket);
        WSACleanup();
        system("PAUSE");
        return -1;
    }
 
    // Reçois la taille du fichier (4 octet ou 32 bits)
    nombre_de_caractere=recvfrom(id_de_la_socket,(char*)&buffer2,4,0
    ,(struct sockaddr*)&information_sur_la_source,&tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Erreur, je n'ai pas recu la taille du fichier !\n";
        cout << "Erreur : " << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
 
    // ----------- Envoi la confirmation de réception --------------------------
    nombre_de_caractere=sendto(id_de_la_socket,(char*)&confirm,1,0
    ,(struct sockaddr*)&information_sur_la_source,tempo);
    if (nombre_de_caractere==SOCKET_ERROR)
    {
        cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
        cout << WSAGetLastError() << "\n\n";
        system("PAUSE");
        return -1;
    }
    else
    {
          cout << "sendto3  : OK\n";
    }
    // ----------- Fin d'envoi la confirmation de réception --------------------
 
    cout << "Taille du fichier a recevoir : " << buffer2 << "\n";
    // Reçois le fichier
    for (int i=0;i<buffer2;i+=nombre_de_caractere)
    {
        nombre_de_caractere=recvfrom(id_de_la_socket,buffer,
        (buffer2-i<4096)?buffer2-i:4096// Si le nombre de donnée restant à reçevoir
        // est plus petit que le buffer on indique ce nombre sinon la taille du buffer
        ,0,(struct sockaddr*)&information_sur_la_source,&tempo);
        if (nombre_de_caractere==SOCKET_ERROR)
        {
            cout << "Erreur, je n'ai pas recu le fichier !\n\n";
            system("PAUSE");
            return -1;
        }
        else
        {
            fichiers.write(buffer,nombre_de_caractere);
        }
        // ----------- Envoi la confirmation de réception ----------------------
        int nombre_de_caractere2=sendto(id_de_la_socket,(char*)&confirm,1,0
        ,(struct sockaddr*)&information_sur_la_source,tempo);
        if (nombre_de_caractere2==SOCKET_ERROR)
        {
            cout << "Desole, je n'ai pas envoyer les donnees du a l'erreur : ";
            cout << WSAGetLastError() << "\n\n";
            system("PAUSE");
            return -1;
        }
        else
        {
              cout << "sendto4-" << i << "-" << nombre_de_caractere << "  : OK\n";
        }
        // ----------- Fin d'envoi la confirmation de réception ----------------
    }
 
    delete[]nomdefichier;
    fichiers.close();
    shutdown(id_de_la_socket,2);
    closesocket(id_de_la_socket);
    cout << "\nLe fichier a bien ete recu !\n\n";
    system("PAUSE");
    return 0;
}
c'est vraiment urgent, merci d'avance