简体   繁体   English

如何使用C语言通过TCP通过HTTP协议发送html文件?

[英]How to send html files with http protocol over tcp in c language?

I 'm writing a HTTP web server that can respond to PUT , GET , ... requests when GET request is sent from a browser , the browser should receives the related HTML file I have analyzed the GET request in the Wireshark it sends the whole HTML file of the website and then the browser requests the rest of the files that are used in the HTML file 我正在编写一个HTTP Web服务器,当从浏览器发送GET请求时,它可以响应PUTGET ...的请求,浏览器应在Wireshark中接收我已经分析过GET请求的相关HTML文件,它将发送整个请求网站的HTML文件,然后浏览器请求HTML文件中使用的其余文件

My problem is that when I use the send() function and send and HTTP respond to the GET requests the body part of the HTTP message that contains the HTML file is not displayed correctly in the browser when it is read from a HTML file but when I type the HTML file into a string variable and give it to the send() function it works. 我的问题是,当我使用send()函数并发送和HTTP响应GET请求时,当从HTML文件中读取包含HTML文件的HTTP消息的正文部分时,该HTML消息的正文部分无法在浏览器中正确显示,但是当我将HTML文件键入一个字符串变量,并将其提供send()它起作用的send()函数。

here is my code: 这是我的代码:

char send_buffer[1000];
FILE *sendFile = fopen("foo.txt", "r");
while( !feof(sendFile) )
{
int numread = fread(send_buffer, sizeof(unsigned char), 1000, sendFile);
if( numread < 1 ) break; // EOF or error

char *send_buffer_ptr = send_buffer;
do
{
    int numsent = send(connected, send_buffer_ptr, numread, 0);
    if( numsent < 1 ) // 0 if disconnected, otherwise error
    {
        if( numsent < 0 )
        {
            if( WSAGetLastError() == WSAEWOULDBLOCK )
            {
                fd_set wfd;
                FD_ZERO(&wfd);
                FD_SET(connected, &wfd);

                timeval tm;
                tm.tv_sec = 10;
                tm.tv_usec = 0;

                if( select(0, NULL, &wfd, NULL, &tm) > 0 )
                    continue;
            }
        }

        break; // timeout or error
    }

    send_buffer_ptr += numsent;
    numread -= numsent;
}
while( numread > 0 );
}

I have tested the code with the foo file containing the HTTP header and some HTML code as below: 我已经使用包含HTTP标头和以下HTML代码的foo文件对代码进行了测试:

HTTP/1.1 200 OK
Content-length: 60
Content-Type: text/html


<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

I have also tries sending the header with a separate send function and the HTML file with another send function right after it. 我也尝试过使用单独的发送函数发送标头,并在紧随其后使用另一个发送函数发送HTML文件。

The Content-Length header in your response is incorrect. 您的响应中的Content-Length标头不正确。 The body of your response is over 100 bytes long, not 60 bytes like the header says. 您的响应正文超过100个字节长,而不是标头所说的60个字节。 (If your file uses CR+LF line endings, it's actually even longer.) (如果您的文件使用CR + LF行尾,则实际上更长。)

In general, the Content-Length header should be generated by the web server, as it must exactly match the size of the response body. 通常, Content-Length标头应由Web服务器生成,因为它必须与响应主体的大小完全匹配。 Storing it in the response is far too prone to errors. 将其存储在响应中非常容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM