2012-10-27 69 views
7

我一直在爲一個類編寫一個簡單的web服務器(http 1.0),但每當我嘗試獲取一個文件(wget 127.0.0.1 /filename)時都只有幾個字節。令人困惑的是當我總結髮送的字節數與文件大小相匹配時,但不是wget收到的數量。簡單的Http獲取響應

爲什麼wget沒有獲取我寫入套接字的所有數據?

一些wget輸出

wget: 

    --2012-10-27 19:02:00-- (try: 4) http://127.0.0.1:5555/ 
    Connecting to 127.0.0.1:5555... connected. 
    HTTP request sent, awaiting response... 200 Document follows 
    Length: 5777 (5.6K) [text/html] 
    Saving to: `index.html.6' 

    99% [=====================================> ] 5,776  --.-K/s in 0s  

    2012-10-27 19:02:00 (322 MB/s) - Read error at byte 5776/5777 (Connection reset by peer). Retrying. 


    --2012-10-27 19:03:52-- (try: 4) http://127.0.0.1:5555/ZoEY8.jpg 
    Connecting to 127.0.0.1:5555... connected. 
    HTTP request sent, awaiting response... 200 Document follows 
    Length: 163972 (160K) [image/jpeg] 
    Saving to: `ZoEY8.jpg.4' 

    91% [==================================> ] 149,449  --.-K/s in 0.001s 

    2012-10-27 19:03:52 (98.8 MB/s) - Read error at byte 163917/163972 (Connection reset by peer). Retrying. 

Get方法:

void * 
processGetRequest(requestParser request) 
{ 

    string resp= "HTTP/1.0 200 Document follows\r\nServer: lab5 \r\nContent-Length: "; 
    string path=""; 
    path =request.path; 

    //find file 
    int page= open (path.c_str(),O_RDONLY); 
    FILE * pageF= fdopen(page,"rb"); 


    //get size 
    fseek(pageF, 0L, SEEK_END); 
    int sz = ftell(pageF); 
    fseek(pageF, 0L, SEEK_SET); 

    //form content length 
    stringstream ss; 
    ss<<resp<<sz<<"\r\n"; 
    resp=ss.str(); 

    //make response 
    if(page<0){ 
     cout<<"404 \n"; 
    resp = "HTTP/1.0 404 File Not Found\r\nServer: lab5 \r\nContent-type: text/html \r\n \r\n"; 

    write(request.fd, resp.c_str(), resp.length()); 

    return 0; 
    } 
    if(path.find(".gif")!=string::npos) 
    resp += "Content-type: image/gif\r\n \r\n"; 
    else if(path.find(".png")!=string::npos) 
    resp += "Content-type: image/png\r\n \r\n"; 
    else if(path.find(".jpg")!=string::npos) 
    resp += "Content-type: image/jpeg\r\n \r\n"; 
    else 
    resp += "Content-type: text/html \r\n \r\n"; 

    //write response 
    write(request.fd, resp.c_str(), resp.length()); 

    int total=0;  
    char buff[1024]; 
    int readBytes = 0; 
    int er; 

    //send file 
    do{ 

    readBytes= read(page, buff, 1024); 
    cout<<"read bytes "<<readBytes<<"\n"; 

    if(readBytes<0){ 
    perror("read"); 

    break; 
    } 
    total+=readBytes; 
    er= send(request.fd, buff, readBytes,0); 
    cout<<"sent bytes "<<er<<"\n"; 
    if (er==-1){ 
    perror("send"); 
    } 
    else if(er != readBytes){ 
    cout<<"Read write miss match\n"; 
    } 

}while(readBytes>0); 

close(page); 
return 0; 
} 

編輯:

我已經在這,而在努力,我想知道,如果Im做我的錯插座

// Set the IP address and port for this server 
struct sockaddr_in serverIPAddress; 
memset(&serverIPAddress, 0, sizeof(serverIPAddress)); 
serverIPAddress.sin_family = AF_INET; 
serverIPAddress.sin_addr.s_addr = INADDR_ANY; 
serverIPAddress.sin_port = htons((u_short) port); 

// Allocate a socket 
int masterSocket = socket(PF_INET, SOCK_STREAM, 0); 
if (masterSocket < 0) { 
    perror("socket"); 
    exit(-1); 
} 

while (1) { 

// Accept incoming connections 
struct sockaddr_in clientIPAddress; 
int alen = sizeof(clientIPAddress); 
int slaveSocket = accept(masterSocket, 
       (struct sockaddr *)&clientIPAddress, 
       (socklen_t*)&alen); 
// send slaveSocket to get method 
} 

回答

1

我的第一個答案是低於,但我只是發現了一些..

"Content-type: text/html \r\n \r\n"; 

標題必須從兩個CR/LF內容分開。它看起來像你宇宙中有

你可以試試這個:

"Content-type: text/html\r\n\r\n"; 

是輸出緩衝器被正確沖洗和最後一次寫入後關閉?嘗試將1024字節讀取緩衝區的大小更改爲大於gif文件的大小。這不是一個解決辦法,但可能會得到不同的結果,這可能有助於找出問題的原因。也許還可以將一些日誌記錄放入讀寫循環中。查看最後寫入緩衝區的大小是否等於響應缺失的字節數。

+0

謝謝,它實際上是標題中的空間 – JacksonReed