C-任何人都可以建议我,如何实现暂停并恢复此数据接收代码的功能



这是来自HTTP服务器的数据接收功能代码,非常有效..但是我想添加暂停并恢复此功能。有人可以建议我如何做吗?
注意:忽略变量声明。

int rest=0,size=0,count=0;                               
memset(buffer,'',sizeof(buffer));
do {
    rc=read(sockfd,buffer,1500);                  //Reading from socket connection
    if(rest==0)
    {   /*Extracting Content length*/
        data=strstr((char*)buffer,"Content-Length"); 
        if(data!=NULL)
        {
            value=atoi((char*)data+15);data_rest=value;
            rest=1;
        }
    }
    if(count==0)
    {   /*Extracting Header*/
        content=strstr(buffer,"rnrn");
        if(content!=NULL)
        { 
            printf("FOUND Header ENDn");
            content=content+4;count=1;
            /*copying DATA to the file which has same Extension*/
            data_wrote=fwrite(content,sizeof(char),rc-(content-buffer),fp);
        }
    }
    else
    {
        content=buffer;
        /*copying DATA to the file which has same Extension*/
        data_wrote=fwrite(content,sizeof(char),rc,fp);
    }
    size=size+rc;
    total_wrote=total_wrote+data_wrote;
    printf("Total size = %dn",size);
    memset(buffer,'',sizeof(buffer));
} while(rc>0);  /*until end of file*/

这是我要使用的过程:

  • 使用Range: bytes=0- HTTP/1.1请求标头发送初始请求。如果服务器使用HTTP/1.1 206 Partial content响应,则您知道稍后可以恢复连接。保存ETag响应标头,因为除非服务器上的内容更改和/或Content-TypeLast- Modified标头,否则它应该保持不适当。请注意,Content-Range响应标头指定此响应中字节的偏移,最终数为完整内容的总长度,Content-Length仅指定当前响应的长度(不是原始内容的长度)。<<<<<<<<<<<<<<<<<<<<<<<<

  • 通过简单地关闭连接来中断传输。

  • 使用Range: bytes=N- HTTP/1.1请求标头发送连续请求,其中N是本地副本中仍然缺少第一个字节的偏移。如果服务器未使用HTTP/1.1 206 Partial content响应,则您没有追索权,但必须再次下载整个内容(使用普通的HTTP请求处理)。如果ETagContent-TypeLast-Modified响应标题不匹配,则可能在服务器上修改内容,并且您应该丢弃此连接并下载完整的请求。Content-Range响应标头指定此响应中字节的偏移量,最终数为完整内容的总长度,Content-Length仅指定当前响应的长度(不是原始内容的长度)。

在所有情况下,服务器都有可能使用chunked内容传输编码,因此您必须准备好处理它,因为请求为HTTP 1.1。有关详细信息,请参见RFC2616。

测试此问题时,我建议您使用一个简单的bash脚本使用自定义标头连接到某些URL,并将响应标头输出到标准错误和消息内容到标准输出:

#!/bin/bash
Host="localhost"   # Host name, use your own servers
Port="80"
Query="/"          # Path to the content requested
Range="0-"         # Byte range requested
# Open up a connection
exec 3<>/dev/tcp/$Host/$Port || exit $?
# Set up a reader sub-shell that will flush the headers
# to standard error, and content to standard out.
(   Headers=1
    while read Line ; do
        Line="${Line//$'r'/}"
        if [ -z "$Line" ]; then
            Headers=0
        elif [ $Headers -gt 0 ]; then
            printf '%sn' "$Line" >&2
        else
            printf '%sn' "$Line"
        fi
    done
    exit 0
) <&3 &
# Construct a byte-range request:
Request=""
Request="$Request""GET $Query HTTP/1.1"$'rn'
Request="$Request""Host: $Host"$'rn'
Request="$Request""Range: bytes=$Range"$'rn'
Request="$Request""Connection: close"$'rn'
# Do a normal request.
printf '%srn' "$Request" >&3
# Close the connection (the read end is still open).
exec 3>&-
# and wait for the request to complete.
wait

以上不是HTTP客户端,例如,不进行分块的转移;这只是一个调试工具,您可以用来准确查看从服务器流到客户端的数据。

最新更新