开发者

How to send HTTP reply using chunked-encoding?

开发者 https://www.devze.com 2023-02-14 02:13 出处:网络
Hi everyone I am trying to stream multimedia data using chunked encoding. So I first tried my hand at sending text data using chunked encoding.

Hi everyone I am trying to stream multimedia data using chunked encoding. So I first tried my hand at sending text data using chunked encoding.

Here is my code. I have created a server socket on which I listen to a request (port 80), reply to the request and then my program terminates. Whatever you request you will always get the same reply. On my machine I send "http://localhost/anirudh.html" as the request from the browser. The output shows that the packets are sent but on the browser nothing is shown. I am doing a blunder somewhere and since I do not know how to send reply using chunked encoding properly I am not able to figure the bug out.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<fcntl.h>

int main(){

    struct sockaddr_in sockserv,sockclient;
    int socketfd,clientfd;
    socklen_t clientsocklen;
    int filefd;
    int finalcnt = 0;

    char buff[BUFSIZ];

    socketfd = socket(AF_INET,SOCK_STREAM,0);   
    printf("Socket Creation: %s\n",strerror(errno));

    bzero(&sockserv,sizeof(sockserv));

    sockserv.sin_family = AF_INET;
    sockserv.sin_addr.s_addr = INADDR_ANY;
    sockserv.sin_port = htons(80);

    bind(socketfd,(struct sockaddr *)&sockserv,sizeof(sockserv));
    printf("Socket Bind: %s\n",strerror(errno));

    listen(socketfd,10);
    printf("Socket Listen: %s\n",strerror(errno));

    clientfd = accept(socketfd,(struct sockaddr*)&sockclient,&clientsocklen);
    printf("request accepted\n");

    buff[read(clientfd,buff,BUFSIZ)] = '\0';
    printf("Request recieved as \n%s\n",buff);  

    sprintf(buff,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nDate: Mon, 28 Feb 2011 10:38:19 GMT\r\nTransfer-Encoding: chunked\r\nServer: Myserver\r\n\r\n%x\r\n%s\r\n0\r\n",strlen("<html><body><p> HI I am anirudh tomer </p> </body></html>"),"<html><body><p> HI I am anirudh tomer </p> </body></html>");

    printf("\n\nREPLY TO THE CLIENT\n\n%s\n\n",buff);

    finalcnt = send(clientfd,buff,strlen(buff),0);  
    printf("Sent %d bytes to the client : %s\n",finalcnt,strerror(errno));

    close(clientfd);
    close(socketfd);

    return 0;
}

here is the reply:

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc test.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out
Socket Creation: Success
Socket Bind: Success
Socket Listen: Success
request accepted
Request recieved as 
GET /avashjfvjhaasadjfasafa.ogg HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3




REPLY TO THE CLIENT

HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 28 Feb 2011 10:38:19 GMT
Transfer-Encoding: chunked
Server: Myserver

39
<html><body><p> HI I am anirudh tomer </p> </body></html>
0


Sent 193 bytes to the client : Success
aniru开发者_开发问答dh@anirudh-Aspire-5920:~/Desktop/testing$

please help me out. Thanks in advance.


Your response appears to be missing the final CRLF sequence. Adding an additional \r\n to the response should correct this.

0

精彩评论

暂无评论...
验证码 换一张
取 消