简体   繁体   English

服务器的非阻塞TCP套接字花费时间流式传输内容

[英]Server's NonBlocking TCP socket taking time to stream content

Problem - I am working on a Streaming server & created a nonblocking socket using: 问题 -我正在使用流服务器,并使用以下命令创建了一个非阻塞套接字:

flag=fcntl(m_fd,F_GETFL);
flag|=O_NONBLOCK;
fcntl(m_fd,F_SETFL,flag);

Server then sends the Media file contents using code: 然后,服务器使用以下代码发送媒体文件内容:

bool SendData(const char *pData,long nSize)
{
    int fd=m_pSock->get_fd();
    fd_set write_flag;
    while(1)
    {   
        FD_ZERO(&write_flag);
        FD_SET(fd,&write_flag);
        struct timeval tout;
        tout.tv_sec=0;
        tout.tv_usec=500000;

        int res=select(fd+1,0,&write_flag,0,&tout);
        if(-1==res)
        {
            print("select() failure\n");
            return false;
        }
        if(1==res)
        {
            unsigned long sndLen=0;
            if(!m_pSock->send(pData,nSize,&sndLen))
            {
                print(socket send() failure\n");
                return false;
            }
            nSize-=sndLen;
            if(!nSize)
            return true;    //everything is sent
        }
    }
}

Using above code, I am streaming a say 200sec audio file, which I expect that Server should stream it in 2-3secs using full n/w available bandwidth(Throttle off), but the problem is that Server is taking 199~200secs to stream full contents. 使用上面的代码,我流的发言权200sec音频文件,这是我想到的是服务器应该在2-3secs流它采用全N / W可用带宽(节气门关闭),但问题是,服务器正在199〜200secs到流全部内容。 While debugging, I commented the 在调试时,我评论了

m_pSock->send() m_pSock-> send()

section & tried to dump the file locally. 部分并尝试在本地转储文件。 It takes 1~2secs to dump the file. 转储文件需要12秒

Questions - If I am using a NonBlocking TCP socket, why does send() taking so much time? 问题 -如果我使用的是NonBlocking TCP套接字,为什么send()需要这么多时间?

  • Since the data is always available, select() will return immediately (as we have seen while dumping the file ). 由于数据始终可用,因此select()将立即返回(如我们在转储文件时所看到的 )。 Does that mean send() is affected by the recv() on the client side? 这是否意味着send()受客户端的recv()影响?

Any inputs on this would be helpul. 任何对此的投入将是有帮助的。 Client behavior is not in our scope. 客户行为不在我们的范围之内。

Your client is probably doing some buffering to avoid network jitter, but it is likely still playing the audio file in real time. 您的客户端可能正在做一些缓冲以避免网络抖动,但是它可能仍会实时播放音频文件。 So, the file transfer rate is matched to the rate that the client is consuming the data. 因此,文件传输速率与客户端使用数据的速率匹配。 Since it is a 200 second audio file, it will take about 200 seconds to complete the transfer. 由于它是200秒的音频文件,因此大约需要200秒才能完成传输。

Because TCP output and input buffers are propably much smaller than the audio file, reading speed of the receiving application can slow down the sending speed. 因为TCP输出和输入缓冲区的大小可能比音频文件小得多,所以接收应用程序的读取速度可能会降低发送速度。

When both the TCP output buffer of sender and the input buffer of receiver are both full, TCP stack of the sender is not able to receive any data from the sender. 当发送方的TCP输出缓冲区和接收方的输入缓冲区都已满时,发送方的TCP堆栈将无法从发送方接收任何数据。 So sending will be blocked, until there is space. 因此,发送将被阻止,直到有空间为止。

If the receiver reads the TCP stream same speed as data is needed for playing. 如果接收方读取TCP流,则与播放数据所需的速度相同。 Then the transfer takes about 200 seconds. 然后,转移大约需要200秒。 Or little bit less. 或少一点。

This can be avoided by using application layer buffering in the receiving end. 可以通过在接收端使用应用程序层缓冲来避免这种情况。

The problem could be that if the client side is using blocking TCP, plus is processing all the data on a single thread with no no buffer/queue etc right through to the "player" of the file, then your side being non-blocking will only speed things until you reach the point where the TCP/IP protocol stack buffers, NIC buffers etc are full. 问题可能是,如果客户端使用阻塞式TCP,加上正在处理单个线程上的所有数据而没有缓冲区/队列等,一直到文件的“播放器”,则您的非阻塞端将只能加快速度,直到达到TCP / IP协议堆栈缓冲区,NIC缓冲区等已满的程度。 Then you will ultimately still only be able to send data as fast as the client side is consuming it. 这样,您最终仍然只能以客户端使用数据时的速度发送数据。 Remember TCP is a reliable, point-to-point protocol. 请记住,TCP是可靠的点对点协议。

Where does your client code come from in your testing? 客户代码来自哪里? Is it some sort of simple test client someone has written? 是某人编写的某种简单的测试客户端吗?

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

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