我不確定這是否為問題點
我沒有編譯過,只是依照經驗判斷哪邊有可能有問題
sendfile有可能實際送出的資料少於block_size 因此img_total_size減掉block_size會有問題的
詳細請看sendfile的說明(你註解中的連結)的回傳值的部分
感謝提點,照你的方式更改好code,有看到在傳送特定幾張圖片時會出錯誤: Bad file descriptor 在其他電腦跑就可以,Raspberry pi就會錯誤,我得深入找下問題點~感謝
int send_message(int fd, char image_path[], char head[]){
/*
char imageheader[] =
"HTTP/1.1 200 Ok\r\n"
"Content-Type: image/jpeg\r\n\r\n";
*/
struct stat stat_buf; /* hold information about input file */
write(fd, head, strlen(head));
int fdimg = open(image_path, O_RDONLY);
fstat(fdimg, &stat_buf);
int img_total_size = stat_buf.st_size;
int block_size = stat_buf.st_blksize;
//printf("image block size: %d\n", stat_buf.st_blksize);
//printf("image total byte st_size: %d\n", stat_buf.st_size);
ssize_t sent_size;
while(img_total_size > 0){
if(img_total_size < block_size){
sent_size = sendfile(fd, fdimg, NULL, img_total_size);
}
else{
sent_size = sendfile(fd, fdimg, NULL, block_size);
}
img_total_size = img_total_size - sent_size;
if(sent_size < 0){
printf("send file error --> send size: %d , error: %s\n", sent_size, strerror(errno));
img_total_size = -1;
}
}
if(sent_size < 1){
}
else{
printf("send file: %s \n" , image_path);
}
close(fdimg);
}
我檢查文件的權限 ($ ls -l 指令) ----以下是以下是可以傳送的圖片的權限 -rw-r--r-- 1 pi pi 75609 Jan 1 18:10 duck-full.jpg -rw-r--r-- 1 pi pi 145423 Jan 10 09:53 fish.jpg -rw-r--r-- 1 pi pi 102816 Jan 1 17:07 five-color-full.JPG -rw-r--r-- 1 pi pi 92390 Jan 1 16:51 five-color.jpg -rw-r--r-- 1 pi pi 96327 Jan 6 14:48 kale.JPG -rw-r--r-- 1 pi pi 123816 Jan 6 15:48 lobsalad.jpg -rw-r--r-- 1 pi pi 66817 Jan 4 16:47 sea.JPG -rw-r--r-- 1 pi pi 270336 Jan 10 09:54 Thumbs.db
----以下是以下是不可以傳送的圖片的權限(會卡在4.bmp那張圖片,其餘的圖片根本沒被client的browser 請求 GET) -rw-r--r-- 1 pi pi 0 Apr 8 20:29 4.bmp -rw-r--r-- 1 pi pi 23088 Apr 8 19:23 4.jpg -rw-r--r-- 1 pi pi 67924 Apr 8 19:23 andy.jpg -rw-r--r-- 1 pi pi 67373 Apr 8 19:24 david.jpg -rw-r--r-- 1 pi pi 88352 Apr 8 19:24 frank.jpg -rw-r--r-- 1 pi pi 73190 Apr 8 19:24 liao1.jpg -rw-r--r-- 1 pi pi 75418 Apr 8 19:24 lin.jpg -rw-r--r-- 1 pi pi 66359 Apr 8 19:25 paul.jpg -rw-r--r-- 1 pi pi 61440 Jan 8 12:21 Thumbs.db
我覺得你可以先釐清是你程式問題,還是系統問題
- 你可以cat 4.bmp看會不會有東西出來
- 裝個簡易的 http server (推薦python) 看可不可以在client端把4.bmp下載或開啟 (https://dotblogs.com.tw/Funny_DotBlog/2019/05/16/python_webserver)
以上兩個都沒問題的話,那也許應該是程式問題比較有可能
另外我在PTT上要你檢查fdimg的值其實有要你處裡他的意思 因為當fdimg為-1時你後面的程式碼都是會出問題的(像是fstat和sendfile和close) 以http server來說,你應該要回傳個 404 Not Found 之類的 (https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Status) 而不是繼續跑後面的程式碼
還有就是http://man7.org/linux/man-pages/man2/open.2.html 參考 "RETURN VALUE" 的部分 "當回傳值為 -1 時,表示錯誤發生,此時,errno (error code)會被設定" 這時候你可以透過errno得到更多的錯誤資訊 https://stackoverflow.com/questions/503878/how-to-know-what-the-errno-means http://man7.org/linux/man-pages/man3/errno.3.html
我剛剛又粗略看了你的程式碼 你程式碼的header我沒看到bmp的存在 只有image/jpeg和image/vnd.microsoft.icon
讀bmp時,回傳jpeg的header,應該會有問題
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
又發現你4.bmp的檔案大小根本是 0 bytes 應該是檔案損毀吧?
我明天會根據你的指示和link來確認, 非常感謝!
我html不會呼叫4.bmp這個檔案。
東毅
Sent from Outlook Mobilehttps://aka.ms/blhgte
From: bdvstg [email protected] Sent: Monday, April 20, 2020 10:35:10 PM To: Dungyichao/http_server [email protected] Cc: Dung-Yi [email protected]; Comment [email protected] Subject: Re: [Dungyichao/http_server] 我不確定這是否為問題點 (#1)
又發現你4.bmp的檔案大小根本是 0 bytes 應該是檔案損毀吧?
— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/Dungyichao/http_server/pull/1#issuecomment-616914985, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AGAQH4UDR6UV7KRTIMXKXA3RNUA55ANCNFSM4MHWNKHQ.