प्राप्त C:भेजने के लिए और इस सर्वर (Sendfile) हिस्सा है फ़ाइल
offset = 0;
for (size_to_send = fsize; size_to_send > 0;){
rc = sendfile(newsockd, fd, &offset, size_to_send);
if (rc <= 0){
perror("sendfile");
onexit(newsockd, sockd, fd, 3);
}
offset += rc;
size_to_send -= rc;
}
close(fd); /* la chiusura del file va qui altrimenti rischio loop infinito e scrittura all'interno del file */
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "226 File Successfully transfered\n");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){
perror("Errore durante l'invio 226");
onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
और इस ग्राहक (recv फ़ाइल) का हिस्सा हिस्सा है:
fsize_tmp = fsize;
sInfo.filebuffer = malloc(fsize);
if(sInfo.filebuffer == NULL){
perror("malloc");
onexit(sockd, 0, fd, 4);
}
while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, sInfo.filebuffer, fsize_tmp)) > 0)){
if(write(fd, sInfo.filebuffer, nread) != nread){
perror("write RETR");
onexit(sockd, 0, 0, 1);
}
total_bytes_read += nread;
fsize_tmp -= nread;
}
close(fd); /* la chiusura del file va qui altrimenti client entra in loop infinito e si scrive all'interno del file */
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, 34, 0) < 0){
perror("Errore ricezione 226");
onexit(sockd, 0, 0, 1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
memset(dirpath, 0, sizeof(dirpath));
free(sInfo.filebuffer);
समस्या
कि है स्ट्रिंग "226 फ़ाइल इत्यादि" के अंदर भेजी गई फ़ाइल के अंदर लिखा गया है।
मैंने एक छोटा डीबग करने की कोशिश की है और इसलिए मैंने लूप (सर्वर sendfile) और printf
के बाद लूप (क्लाइंट) के बाद printf
जोड़ा है और मैंने देखा है कि फ़ाइल भेजी गई है लेकिन क्लाइंट यह समय से बाहर नहीं निकलता है क्योंकि printf
मुद्रित नहीं है ...
मुझे यह अजीब व्यवहार क्यों मिला ??
br>
संपादित करें:
fd = open(filename, O_RDONLY);
if(fd < 0){
error!!
}
if(fstat(fd, &fileStat) < 0){
perror("Errore fstat");
onexit(newsockd, sockd, fd, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
perror("Errore durante l'invio della grandezza del file\n");
onexit(newsockd, sockd, fd, 3);
}
ग्राहक इस कोड के साथ सर्वर से fsize प्राप्त करता है:
सर्वर इस कोड को ग्राहक तिनका करने के लिए फ़ाइल आकार भेज
if(read(sockd, &fsize, sizeof(fsize)) < 0){
perror("Errore durante ricezione grandezza file\n");
onexit(sockd, 0 ,0 ,1);
}
fd = open(sInfo.filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("open");
onexit(sockd, 0 ,0 ,1);
}
fsize_tmp = fsize;
fsize
दोनों uint32_t
के रूप में घोषित किए गए हैं ...
संभावित डुप्लिकेट [सी/सी ++ (जीसीसी/जी ++) के साथ लिनक्स में सॉकेट प्रोग्रामिंग में फ़ाइल भेजें और प्राप्त करें] (https://stackoverflow.com/questions/2014033/send-and-receive-a- फ़ाइल-इन-सॉकेट-प्रोग्रामिंग-इन-लिनक्स-साथ-सीसी-जीसीसी-जी) –