Record some problems encountered in network programming

Article Directory

EPOLL

Today, because epollthe ET mode is lost connect事件, it took a day of brainless debugging to find out where the error is. Hereby record it.

When only using epoll's ET mode, multiple cennectmay only trigger one 事件, so it is necessary to loop acceptuntil it returns -1and erroris set to WAGAINor WEOULDBLOCKdoes not exit the loop. Otherwise it will 丢失connect事件.

The difference between epoll's LT and ET
LT: horizontal trigger, the efficiency will be lower than that of ET trigger, especially in the case of large concurrency and large flow. However, LT has relatively low requirements for code writing and is not prone to problems. The performance of the LT mode service writing is: as long as there is data that has not been obtained, the kernel will continue to notify you, so there is no need to worry about the event loss.
ET: Edge triggering is very efficient. In the case of concurrent and large traffic, there will be many fewer epoll system calls than LT, so the efficiency is high. However, the requirements for programming are high, and each request needs to be handled carefully, otherwise a loss event may easily occur.
The following is an example to illustrate the difference between LT and ET (both are non-blocking mode, blocking is not said, the efficiency is too low):
In LT mode, if the accept call returns, you can immediately establish the current connection, and then epoll_wait wait The next notice is the same as select.
But for ET, if the accpet call returns, in addition to establishing the current connection, epoll_wait can't immediately continue to loop accpet until it returns -1, and errno == EAGAIN

while(1)
{
    struct sockaddr_in client_address;
    socklen_t len = sizeof client_address;
    int connfd = accept(listenfd, (struct sockaddr*)&client_address, &len);
    if(connfd == -1)
    {
        if(errno == EAGAIN || errno == EWOULDBLOCK)
        {
            break;
        }
    }
    else
    {
        addfd(epollfd, connfd, true);
    }
} 

Two functions

https://www.quora.com/What-is-the-main-difference-between-copy_file_range-and-sendfile-system-calls-in-Linux


sendfile();
copy_file_range(); 
以上两个函数输入必须是可以映射到内存的文件,输出sendfile()必须是套接字,copy_file_range()没有限制。

GDB

// TODO
debug multi-threaded program
http://logan.tw/posts/2015/11/01/debug-multithreaded-program-with-gdb/

https://cboard.cprogramming.com/c-programming/149636-multi-thread-share-global-variable-problems.html

other

Regarding switcha small problem, you caseneed to use curly braces for each . I have rarely used this problem before. I can refer to it here: https://stackoverflow.com/questions/5685471/error-jump-to-case-label

Published 74 original articles · Like 11 · Visits 30,000+

Guess you like

Origin blog.csdn.net/yijiull/article/details/90271094