Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12244

Does every Linux fd have a buffer?

$
0
0

After the client connects to the server, the input on the server terminal will be blocked until the client disconnects. Is this blocked data in the terminal's buffer?What happens if the input data is larger than the terminal's buffer?

There is a similar buffer in the tcp stack. When the application does not process the data in the buffer, tcp will adjust the window size so that the sender no longer sends data. Is there such a mechanism for the terminal?

Does every Linux fd have such a buffer? Is the size of the buffer determined by the implementation?

void echo(const clientinfo &ci){    size_t n;    char buff[MAXLEN];    rio_t rio;    Rio_readinitb(&rio,ci.fd);    while((n = Rio_readlineb(&rio,buff,MAXLEN)) != 0){        std::cout << "server received " << n  << " bytes from "<< ci.client_hostname << " port " <<ci.client_port  <<std::endl;        std::cout << buff;        Rio_writen(ci.fd,buff,n);     }}void command(){    char buf[MAXLEN];    if(!fgets(buf,MAXLEN,stdin))        return;    std::cout << buf;}int main(int argc,char **argv){    int listenfd,connfd;    socklen_t clientlen;    sockaddr_storage clientaddr;    fd_set read_set,ready_set;    clientinfo ci;    if(argc != 2){        std::cerr << "usage: " << argv[0] << " <port>" << std::endl;        exit(0);    }    listenfd = open_listenfd(argv[1]);    FD_ZERO(&read_set);    FD_SET(STDIN_FILENO,&read_set);    FD_SET(listenfd,&read_set);    while(1){        ready_set = read_set;        if(select(listenfd+1,&ready_set,NULL,NULL,NULL) < 0)            unix_error("select error");        if(FD_ISSET(STDIN_FILENO,&ready_set))            command();        if(FD_ISSET(listenfd,&ready_set)){            clientlen = sizeof(sockaddr_storage);            ci.fd = accept(listenfd,(sockaddr*)&clientaddr,&clientlen);            getnameinfo((sockaddr*)&clientaddr,clientlen,ci.client_hostname,MAXLEN,ci.client_port,MAXLEN,0);            std::cout << "connect from " << ci.client_hostname << " port " << ci.client_port <<std::endl;            echo(ci);            close(ci.fd);        }    }}

Viewing all articles
Browse latest Browse all 12244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>