简体   繁体   中英

Preventing timeout while transferring files between 2 peers

I am transferring files from server to clients using nonblocking sockets. Some files may be quite big (hundreds of megabytes). A separate thread handles each client.

while (more_to_read)
{
    written = write(fd, ...);

    if (written == 0)
    {
        struct pollfd wait = {
            .fd = fd,
            .events = POLLOUT,
            .revents = 0
        };
        int status = poll(&wait, 1, TIMEOUT);
        // ...
    }

    // ...
}

Sometimes poll() timeouts and the server closes the connection (to prevent malicious clients from using up server resources). I discovered this can happen even when the client is still trying to read the data. In such case, the client gets ECONNRESET when calling read() .

I figured out that the server repeatedly sends some chunks of the file and at some point the socket may remain not ready for writing for some seconds. I assume this is because the data hasn't been sent yet and the corresponding kernel buffer is full. However this causes the timeout which leads the server to close the connection.

When I increase the value of TIMEOUT , the error happens less often. What is the best way to make sure unexpected timeouts don't happen?

The server is supposed to run on multiple platforms so I cannot really use an OS-specific solution.

You can never protect yourself fully against both DOS attacks and unexpected timeouts.

I suggest that you scale the allowed timeout with the amount of data already sent. Start with a shart timeout and increase it as you send more and more data.

DOS attackers and defunct clients will likely open many connections but won't bother reading from them. You want to close this kind of connection fast.

Clients who have received tens or hundreds of MB of data should be allowed much greater timeouts.

Example:

  • Initial: 500 ms
  • After 1 MB: 2 s
  • After 100 MB: 10 s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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