Bonjour.
Mon problème est que lorsque j'envoie un fichier par sockets, la taille de celui ci est pour le client très légèrement supérieure (quelques octets), le client ne sort également pas de la boucle de réception.
J'aimerai bien connaitre la cause de tous ces désagréments et je remercie tous ce qui voudront bien m'aider.
Voici mon code:
SERVEUR
main.c
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 #include <stdlib.h> #include <stdio.h> #include <winsock2.h> #include "serveur.h" int main(void) { int err, ret; WSADATA wsa_data; err = WSAStartup(MAKEWORD(2,2), &wsa_data); if(!err) { err = serveur(); WSACleanup(); } if(err) { ret = EXIT_FAILURE; } else { ret = EXIT_SUCCESS; } return ret; }
serveur.c
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 #include <stdio.h> #include <winsock2.h> #include "serveur.h" #include "send_file.h" #define FTP 21 int serveur(void) { int err = 0; SOCKET serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(serv_sock != INVALID_SOCKET) { int sock_err; SOCKADDR_IN sin; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_family = AF_INET; sin.sin_port = htons(FTP); sock_err = bind(serv_sock, (SOCKADDR *)&sin, (int)sizeof(sin)); if(sock_err != SOCKET_ERROR) { sock_err = listen(serv_sock, 1); if(sock_err != SOCKET_ERROR) { SOCKADDR_IN csin; SOCKET client_sock; int struct_size = (int)sizeof(csin); client_sock = accept(serv_sock, (SOCKADDR *)&csin, &struct_size); if(client_sock != INVALID_SOCKET) { err = send_file(client_sock); shutdown(client_sock, SD_BOTH); closesocket(client_sock); client_sock = INVALID_SOCKET; } else { err = 1; } } else { err = 1; } } else { err = 1; } shutdown(serv_sock, SD_BOTH); closesocket(serv_sock); serv_sock = INVALID_SOCKET; } else { err = 1; } return err; }
send_file.c
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 #include <stdio.h> #include <string.h> #include <winsock2.h> #include "send_file.h" #define BUF_SIZE 256 int send_file(SOCKET client_sock) { int err = 0; FILE *pfile = NULL; pfile = fopen("C:\\tetris.jpg", "rb"); if(pfile != NULL) { int sock_err; long file_size; char s_file_size[sizeof(long)+1] = ""; fseek(pfile, 0, SEEK_END); file_size = ftell(pfile); rewind(pfile); sprintf(s_file_size, "%ld", file_size); s_file_size[sizeof(long)+1] = '\n'; sock_err = send(client_sock, s_file_size, (int)strlen(s_file_size), 0); if(sock_err != SOCKET_ERROR) { size_t data_lenght; unsigned char data[BUF_SIZE] = ""; while((data_lenght = fread(data, 1, BUF_SIZE, pfile)) != 0) { sock_err = send(client_sock, data, (int)data_lenght, 0); if(sock_err == SOCKET_ERROR) { err = 1; break; } } fclose(pfile); } else { err = 1; } } else { err = 1; } return err; }
serveur.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef SERVEUR #define SERVEUR int serveur(void); #endif
send_file.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef SEND_FILE #define SEND_FILE int send_file(SOCKET client_sock); #endif
CLIENT
main.c
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 #include <stdlib.h> #include <stdio.h> #include <winsock2.h> #include "client.h" int main(void) { int err, ret; WSADATA wsa_data; err = WSAStartup(MAKEWORD(2,2), &wsa_data); if(!err) { err = client(); WSACleanup(); } if(err) { ret = EXIT_FAILURE; } else { ret = EXIT_SUCCESS; } return ret; }
client.c
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 #include <stdio.h> #include <winsock2.h> #include "client.h" #include "clean.h" #define FTP 21 #define BUF_SIZE 256 int client(void) { int err = 0; SOCKET client_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(client_sock != INVALID_SOCKET) { int sock_err; SOCKADDR_IN csin; csin.sin_addr.s_addr = htonl(INADDR_ANY); csin.sin_family = AF_INET; csin.sin_port = htons(FTP); sock_err = bind(client_sock, (SOCKADDR *)&csin, (int)sizeof(csin)); if(sock_err != SOCKET_ERROR) { char IP_serveur[17] = ""; SOCKADDR_IN sin; fgets(IP_serveur, sizeof(IP_serveur), stdin); clean(IP_serveur); sin.sin_addr.s_addr = inet_addr(IP_serveur); sin.sin_family = AF_INET; sin.sin_port = htons(FTP); sock_err = connect(client_sock, (SOCKADDR *)&sin, (int)sizeof(sin)); if(sock_err != SOCKET_ERROR) { char s_file_size[9] = ""; sock_err = recv(client_sock, s_file_size, (int)strlen(s_file_size), 0); if(sock_err != SOCKET_ERROR) { long file_size; FILE *p_file = NULL; clean(s_file_size); sscanf(s_file_size, "%ld", &file_size); p_file = fopen("C:\\tetris_bis.jpg", "wb+"); if(p_file != NULL) { unsigned char data[BUF_SIZE] = ""; while(file_size > 0) { sock_err = recv(client_sock, data, (int)sizeof(data), 0); if(sock_err == SOCKET_ERROR) { err = 1; break; } fwrite(data, 1, (size_t)sock_err, p_file); file_size -= sock_err; } fclose(p_file); } else { err = 1; } } else { err = 1; } } else { err = 1; } } else { err = 1; } shutdown(client_sock, SD_BOTH); closesocket(client_sock); client_sock = INVALID_SOCKET; } else { err = 1; } return err; }
clean.c
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 #include <stdio.h> #include <string.h> #include "clean.h" void clean(char string[]) { char *p_string = NULL; p_string = strchr(string, '\n'); if(p_string) { *p_string = '\0'; } else { int c; while((c = getchar()) != '\n' && c != EOF); } }
client.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef CLIENT #define CLIENT int client(void); #endif
clean.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef CLEAN #define CLEAN void clean(char string[]); #endif
Partager