diff options
| author | Michael Kerrisk <mtk.manpages@gmail.com> | 2008-12-05 15:13:41 -0500 |
|---|---|---|
| committer | Michael Kerrisk <mtk.manpages@gmail.com> | 2008-12-05 22:47:24 -0500 |
| commit | 54781a2395c65d9718987316bfe9190f2e59c934 (patch) | |
| tree | 28885e0c4107e97fa8726f695faa83345be6be0b | |
| parent | 5af0d2f5d3f011c41de585df475103bab0383d9a (diff) | |
| download | man-pages-54781a2395c65d9718987316bfe9190f2e59c934.tar.gz | |
select_tut.2: wsfix in example program
| -rw-r--r-- | man2/select_tut.2 | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/man2/select_tut.2 b/man2/select_tut.2 index 2a97c5ea71..92e9779d50 100644 --- a/man2/select_tut.2 +++ b/man2/select_tut.2 @@ -667,8 +667,7 @@ main(int argc, char **argv) int buf2_avail, buf2_written; if (argc != 4) { - fprintf(stderr, - "Usage\\n\\tfwd <listen-port> " + fprintf(stderr, "Usage\\n\\tfwd <listen-port> " "<forward-to-port> <forward-to-ip-address>\\n"); exit(EXIT_FAILURE); } @@ -727,6 +726,7 @@ main(int argc, char **argv) if (FD_ISSET(h, &rd)) { unsigned int l; struct sockaddr_in client_address; + memset(&client_address, 0, l = sizeof(client_address)); r = accept(h, (struct sockaddr *) &client_address, &l); if (r < 0) { @@ -737,8 +737,7 @@ main(int argc, char **argv) buf1_avail = buf1_written = 0; buf2_avail = buf2_written = 0; fd1 = r; - fd2 = - connect_socket(forward_port, argv[3]); + fd2 = connect_socket(forward_port, argv[3]); if (fd2 < 0) { SHUT_FD1; } else @@ -746,7 +745,9 @@ main(int argc, char **argv) inet_ntoa(client_address.sin_addr)); } } -/* NB: read oob data before normal reads */ + + /* NB: read oob data before normal reads */ + if (fd1 > 0) if (FD_ISSET(fd1, &er)) { char c; @@ -769,8 +770,7 @@ main(int argc, char **argv) } if (fd1 > 0) if (FD_ISSET(fd1, &rd)) { - r = - read(fd1, buf1 + buf1_avail, + r = read(fd1, buf1 + buf1_avail, BUF_SIZE \- buf1_avail); if (r < 1) { SHUT_FD1; @@ -779,8 +779,7 @@ main(int argc, char **argv) } if (fd2 > 0) if (FD_ISSET(fd2, &rd)) { - r = - read(fd2, buf2 + buf2_avail, + r = read(fd2, buf2 + buf2_avail, BUF_SIZE \- buf2_avail); if (r < 1) { SHUT_FD2; @@ -789,8 +788,7 @@ main(int argc, char **argv) } if (fd1 > 0) if (FD_ISSET(fd1, &wr)) { - r = - write(fd1, buf2 + buf2_written, + r = write(fd1, buf2 + buf2_written, buf2_avail \- buf2_written); if (r < 1) { SHUT_FD1; @@ -799,21 +797,24 @@ main(int argc, char **argv) } if (fd2 > 0) if (FD_ISSET(fd2, &wr)) { - r = - write(fd2, buf1 + buf1_written, + r = write(fd2, buf1 + buf1_written, buf1_avail \- buf1_written); if (r < 1) { SHUT_FD2; } else buf1_written += r; } -/* check if write data has caught read data */ + + /* check if write data has caught read data */ + if (buf1_written == buf1_avail) buf1_written = buf1_avail = 0; if (buf2_written == buf2_avail) buf2_written = buf2_avail = 0; -/* one side has closed the connection, keep - writing to the other side until empty */ + + /* one side has closed the connection, keep + writing to the other side until empty */ + if (fd1 < 0 && buf1_avail \- buf1_written == 0) { SHUT_FD2; } |
