Bien le bonjour,
J'ai trouvé un code permettant d'envoyer/recevoir une image spécifique.
Mais je ne sais pas quelle condition dois-je ajouter pour que le serveur soit en attente de manière infini ?
Mon but est d'envoyer un flux d'image et de le retrouver en sortie Serveur --> Client.
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
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 #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h> #include<sys/ioctl.h> #include<unistd.h> #include<iostream> #include<fstream> #include<errno.h> using namespace std; //This function is to be used once we have confirmed that an image is to be sent //It should read and output an image file int receive_image(int socket) { // Start function int buffersize = 0, recv_size = 0,size = 0, read_size, write_size, packet_index =1,stat; char imagearray[10241],verify = '1'; FILE *image; //Find the size of the image do{ stat = read(socket, &size, sizeof(int)); }while(stat<0); printf("Packet received.\n"); printf("Packet size: %i\n",stat); printf("Image size: %i\n",size); printf(" \n"); char buffer[] = "Got it"; //Send our verification signal do{ stat = write(socket, &buffer, sizeof(int)); }while(stat<0); printf("Reply sent\n"); printf(" \n"); image = fopen("capture2.jpeg", "w"); if( image == NULL) { printf("Error has occurred. Image file could not be opened\n"); return -1; } //Loop while we have not received the entire file yet int need_exit = 0; struct timeval timeout = {10,0}; fd_set fds; int buffer_fd, buffer_out; while(recv_size < size) { //while(packet_index < 2){ FD_ZERO(&fds); FD_SET(socket,&fds); buffer_fd = select(FD_SETSIZE,&fds,NULL,NULL,&timeout); if (buffer_fd < 0) printf("error: bad file descriptor set.\n"); if (buffer_fd == 0) printf("error: buffer read timeout expired.\n"); if (buffer_fd > 0) { do{ read_size = read(socket,imagearray, 10241); }while(read_size <0); printf("Packet number received: %i\n",packet_index); printf("Packet size: %i\n",read_size); //Write the currently read data into our image file write_size = fwrite(imagearray,1,read_size, image); printf("Written image size: %i\n",write_size); if(read_size !=write_size) { printf("error in read write\n"); } //Increment the total number of bytes read recv_size += read_size; packet_index++; printf("Total received image size: %i\n",recv_size); printf(" \n"); printf(" \n"); } } fclose(image); printf("Image successfully Received!\n"); return 1; } int main(int argc , char *argv[]) { int socket_desc; struct sockaddr_in server; char *parray; //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); if (socket_desc == -1) { printf("Could not create socket"); } memset(&server,0,sizeof(server)); server.sin_addr.s_addr = inet_addr("10.0.0.30"); server.sin_family = AF_INET; server.sin_port = htons( 8889 ); //Connect to remote server if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0) { cout<<strerror(errno); close(socket_desc); puts("Connect Error"); return 1; } puts("Connected\n"); receive_image(socket_desc); close(socket_desc); return 0; }
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
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 #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h> #include<unistd.h> #include<iostream> #include<fstream> #include<errno.h> using namespace std; int send_image(int socket){ FILE *picture; int size, read_size, stat, packet_index; char send_buffer[10240], read_buffer[256]; packet_index = 1; picture = fopen("capture.jpeg", "r"); printf("Getting Picture Size\n"); if(picture == NULL) { printf("Error Opening Image File"); } fseek(picture, 0, SEEK_END); size = ftell(picture); fseek(picture, 0, SEEK_SET); printf("Total Picture size: %i\n",size); //Send Picture Size printf("Sending Picture Size\n"); write(socket, (void *)&size, sizeof(int)); //Send Picture as Byte Array printf("Sending Picture as Byte Array\n"); do { //Read while we get errors that are due to signals. stat=read(socket, &read_buffer , 255); printf("Bytes read: %i\n",stat); } while (stat < 0); printf("Received data in socket\n"); printf("Socket data: %c\n", read_buffer); while(!feof(picture)) { //while(packet_index = 1){ //Read from the file into our send buffer read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture); //Send data through our socket do{ stat = write(socket, send_buffer, read_size); }while (stat < 0); printf("Packet Number: %i\n",packet_index); printf("Packet Size Sent: %i\n",read_size); printf(" \n"); printf(" \n"); packet_index++; //Zero out our send buffer bzero(send_buffer, sizeof(send_buffer)); } } int main(int argc , char *argv[]) { int socket_desc , new_socket , c, read_size,buffer = 0; struct sockaddr_in server , client; char *readin; //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); if (socket_desc == -1) { printf("Could not create socket"); } //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons( 8889 ); //Bind if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) { puts("bind failed"); return 1; } puts("bind done"); //Listen listen(socket_desc , 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); if((new_socket = accept(socket_desc, (struct sockaddr *)&client,(socklen_t*)&c))){ puts("Connection accepted"); } fflush(stdout); if (new_socket<0) { perror("Accept Failed"); return 1; } send_image(new_socket); close(socket_desc); fflush(stdout); return 0; }
Merci d'avance !
Partager