Bonjour, depuis plusieurs jours je cherche a écrire un programme qui envoie un fichier texte depuis mon pc portable (Ubuntu 16.04 LTS) a ma tour (Windows 10). Je débute dans la programmation réseau.J'ai avant cela écris un Echo server, cad un server qui renvoie automatiquement les chaînes de caractères qu'on lui envoies, mais c'est tout.

Il me semble avoir a peut près assimilé le coté "codage du server" dans l'envoie de fichier texte (binaire). Mais là où je bloque sérieusement c'est pour le codage du client, je suis perdus.C'est pour cela que créer ce sujet afin d'obtenir des explications ainsi qu'un peu d'aide dans la réalisation de mon projet d'envois de fichier.

Voici le code :

server (sous Ubuntu) :

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
/** Sinon, si nous sommes sous Linux */
 
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
 
    /** Define, qui nous serviront par la suite **/
    #define INVALID_SOCKET -1
    #define SOCKET_ERROR -1
    #define closesocket(s) close (s)
 
    /** De même */
    typedef int SOCKET;
    typedef struct sockaddr_in SOCKADDR_IN;
    typedef struct sockaddr SOCKADDR;
 
 
/** On inclut les fichiers standards **/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
 
#define PORT    23
 
int main(int argc, char *argv[])
{
    FILE *txt1      = NULL;
    FILE *txt2      = NULL;
 
    int poidsTxt1 = 0; /** pareil **/
    int poidsTxt2 = 0; /** pareil **/
 
    int caractereLu = 0;
    int dataSend = 0;
    int dataRead = 0;
    int totalSend = 0;
    int i = 0;
 
    /** Socket et contexte d'adressage du server **/
    SOCKET sock = 0;
    SOCKADDR_IN sin;
 
    /** Socket et contexte d'adressage du client **/
    SOCKET csock = 0;
    SOCKADDR_IN csin;
    socklen_t recsize = sizeof(csin);
 
    int sock_err = 0;
    char bufferTxt1[1024] = "";
    char bufferTxt2[1024] = "";
 
    txt1 = fopen("test.txt", "r");
    txt2 = fopen("test2.txt", "r")
 
    if(txt1 == NULL || txt2 == NULL)
    {
        perror("Imposssible d'ouvrir les fichier ");
        exit(errno);
    }
 
    /** calcul du poids des fichiers txt **/
    /** test.txt **/
 
    while((caractereLu = fgetc(txt1)) != EOF)
    {
        bufferTxt1[i] = caractereLu;
        poidsTxt1++;
        //printf("buffer[%d] = %c\n", i, bufferTxt1[i]);
        i++;
    }
 
    printf("le poids du fichier txt1 est de %d octets.\n\n\n", poidsTxt1);
 
    /** test2.txt **/
     while((caractereLu = fgetc(txt2)) != EOF)
    {
        bufferTxt2[i] = caractereLu;
        poidsTxt2++;
        //printf("buffer[%d] = %c\n", i, bufferTxt2[i]);
        i++;
    }
 
    printf("le poids du fichier txt2 est de %d octets.\n", poidsTxt2);
 
    fclose(txt1);
    fclose(txt2);
 
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == INVALID_SOCKET)
    {
        perror("Can't create the socket file descriptor ");
        exit(errno);
    }
 
    printf("\nLa socket %d est maintenant ouverte en mode TCP/IP.\n", sock);
 
    /** configuration, contexte d'adressage server **/
    sin.sin_addr.s_addr     = htonl(INADDR_ANY);
    sin.sin_family          = AF_INET;
    sin.sin_port            = htons(PORT);
 
    sock_err = bind(sock, (SOCKADDR *)&sin, sizeof(sin));
 
    /** Si la socket fonctionne **/
    if(sock_err != SOCKET_ERROR)
    {
        sock_err = listen(sock, 5);
        printf("\nListage du port %d....\n", PORT);
 
        if(sock_err != SOCKET_ERROR)
        {
            printf("\nPatientez pendant q'un client se connecte sur le port %d.....\n\n", PORT);
 
            csock = accept(sock, (SOCKADDR *)&csin, &recsize);
            if(csock == -1)
            {
                perror("ERROR accept() ");
                exit(errno);
            }
            printf("Un client se connecte avec la socket %d depuis %s:%d.\n\n", csock,
                                                                            inet_ntoa(csin.sin_addr),
                                                                            htons(csin.sin_port));
            /** Envoie du poid du 1er fichier texte **/
            dataSend = send(csock, (char *)&poidsTxt1, sizeof(poidsTxt1), 0);
            if(dataSend < 0)
            {
                perror("Failed to send weight of the first texte file.\n");
                exit(errno);
            }
 
            /** printf("Envoie du poid du fichier txt1 soit %d bytes/octets.\n\n", poidsTxt1); **/
 
            /** Envoie du 1 er fichier txt **/
            txt1 = fopen("test.txt", "rb");
            if(txt1 == NULL)
            {
                perror("Imposssible d'ouvrir les fichier ");
                exit(errno);
            }
            i = 0;
            do
            {
                dataRead = fread(bufferTxt1, sizeof(bufferTxt1), 1, txt1);
                dataSend = send(csock, bufferTxt1, sizeof(caractereLu), 0);
 
                //printf("envoie de %d bytes/octets\n", dataSend);
 
                if(dataSend == SOCKET_ERROR)
                {
                    perror("send() ");
                    exit(errno);
                }
 
                if(totalSend == 28)
                    dataSend = send(csock, bufferTxt1, 1, 0);
 
                totalSend += dataSend;
 
                printf("total send = %d\n", totalSend);
 
                if(totalSend > poidsTxt1)
                    break;
 
            }while(totalSend < poidsTxt1);
 
        }
 
        shutdown(csock, 2);
        printf("Fermeture de la connection ...\n");
    }
 
    closesocket(sock);
    printf("Fermeture socket server ...\n");
 
    closesocket(csock);
    printf("Fermeture socket client ...\n");
 
 
    return EXIT_SUCCESS;
 
}

- Client (Sous Windows 10) :

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
#include <winsock2.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
 
#define PORT        23
#define TAILLE_MAX  10000
 
int main(int argc, char *argv[])
{
    int erreur = 0;
 
    SOCKET sock;
    SOCKADDR_IN sin;
    int poidTxt1 = 0;
    int poidTxt2 = 0;
    int totalRcv = 0;
    int tailleBlockRecut = 0;
 
    int bufferPoidFileTxt1[512];
    int bufferPoidFileTxt2[512];
 
    char bufferReceptionTxt1[1024];
    char bufferReceptionTxt2[1024];
 
    FILE *txt1 = NULL;
    FILE *txt2 = NULL;
 
    WSADATA wsa;
    erreur = WSAStartup(MAKEWORD(2, 2), &wsa);
    if(erreur < 0)
    {
        puts("WSAStartup failed !");
        exit(EXIT_FAILURE);
    }
    else
        printf("Initialisation socket windows success !\n");
 
    if(!erreur)
    {
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock == INVALID_SOCKET)
        {
            perror("can't create the socket's file descriptor ");
            exit(errno);
        }
 
        sin.sin_addr.s_addr = inet_addr("x.x.x.x");
        sin.sin_family = AF_INET;
        sin.sin_port = htons(PORT);
 
        if(connect(sock, (SOCKADDR *)&sin, sizeof(sin)) != SOCKET_ERROR)
            printf("Connection a %s sur le port %d\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
        else
        {
            printf("Impossible de se connecter ... \n");
            perror("connect() ");
        }
 
        txt1 = fopen("test.txt", "w+");
        if(txt1 == NULL)
        {
            perror("Impossible d'ouvrir le fichier txt1 ");
            exit(errno);
        }
 
        /** Reception du poid du 1er fichier texte **/
        tailleBlockRecut = recv(sock, (char *)&bufferPoidFileTxt1, sizeof(poidTxt1), 0);
        if(tailleBlockRecut < 0)
        {
            perror("error de transmission du poid du fichier txt 1 ");
            exit(errno);
        }
 
        printf("\nReception de la taille du fichier txt 1 soit %d bytes/octets\n", *bufferPoidFileTxt1);
 
        do
        {
            tailleBlockRecut = recv(sock, bufferReceptionTxt1, sizeof(*bufferPoidFileTxt1), 0);
 
            printf("reception d'un bout de fichier %d bytes/octets \n", tailleBlockRecut);
 
            if(tailleBlockRecut < 0)
            {
                perror("problème de reception ");
                exit(errno);
            }
 
            totalRcv += tailleBlockRecut;
            printf("totalRcv = %d\n", totalRcv);
 
            if(totalRcv == poidTxt1)
                break;
 
            fputs(bufferReceptionTxt1, txt1);
 
        }while(totalRcv < poidTxt1);
 
        fclose(txt1);
 
        closesocket(sock);
        printf("\nFermeture de la socket client ...\n\n");
    }
 
    return EXIT_SUCCESS;
}
Merci d'avance, a+++.