c - The denial-of-service in unix-network-programming 3e of chapter 6.8 -
i reading unix network programming 3e, , when coming denial of service in chapter 6.8, have question why read block? codes in book using system call 'select' follow:
listenfd = socket(af_inet, sock_stream, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = af_inet; servaddr.sin_addr.s_addr = htonl(inaddr_any); servaddr.sin_port = htons(serv_port); bind(listenfd, (sa *)&servaddr, sizeof(servaddr)); signal(sigchld, sig_chld); listen(listenfd, listenq); maxfd = listenfd; maxi = -1; (i = 0; < fd_setsize; i++) { client[i] = -1; } fd_zero(&allset); fd_set(listenfd, &allset); ( ; ; ) { rset = allset; nready = select(maxfd + 1, &rset, 0, 0, 0); if (fd_isset(listenfd, &rset)) { clilen = sizeof(cliaddr); again: if ((connfd = accept(listenfd, (sa *)&cliaddr, &clilen)) < 0) { if (errno == eintr || errno == econnaborted) { goto again; } else { err_sys("accept error"); } } (i = 0; < fd_setsize; i++) { if (client[i] < 0) { client[i] = connfd; break; } } if (i == fd_setsize) { err_quit("too many clients"); } fd_set(connfd, &allset); if (connfd > maxfd) { maxfd = connfd; } if (i > maxi) { maxi = i; } if (--nready == 0) { continue; } } (i = 0; <= maxi; i++) { if ((sockfd = client[i]) < 0) { continue; } if (fd_isset(sockfd, &rset)) { if ((n = read(sockfd, buf, maxline)) == 0) { // book says if first send byte(not '\n'), read block, confusing me close(sockfd); fd_clr(sockfd, &allset); client[i] = -1; } else { writen(sockfd, buf, n); } if (--nready == 0) { break; } } } }
and use client code:
char sendline[maxline], recvline[maxline]; while (fgets(sendline, maxline, fp) != null) { writen(sockfd, sendline, 1); sleep(10); writen(sockfd, sendline + 1, strlen(sendline) - 1); if (readline(sockfd, recvline, maxline) == 0) { err_quit("strcli: server terminate prematurely"); } fputs(recvline, stdout); }
and when start 2 clients, , type in 1 line first client, while server not blocked when typing second client.
sorry, want simplify, use read
instead of read
. , i'm unclear why read
in server side block when client write
byte first(not '\n') in above example. book says waits '\n', while think read
doesn't need '\n' return.
wiki
Comments
Post a Comment