select_tut.2: tfix

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2016-02-03 14:18:21 +01:00
parent 887f19e8a1
commit f145f66700
1 changed files with 6 additions and 3 deletions

View File

@ -562,6 +562,7 @@ listen_socket(int listen_port)
perror("socket"); perror("socket");
return \-1; return \-1;
} }
yes = 1; yes = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(yes)) == \-1) { &yes, sizeof(yes)) == \-1) {
@ -569,6 +570,7 @@ listen_socket(int listen_port)
close(s); close(s);
return \-1; return \-1;
} }
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_port = htons(listen_port); addr.sin_port = htons(listen_port);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -577,6 +579,7 @@ listen_socket(int listen_port)
close(s); close(s);
return \-1; return \-1;
} }
printf("accepting connections on port %d\\n", listen_port); printf("accepting connections on port %d\\n", listen_port);
listen(s, 10); listen(s, 10);
return s; return s;
@ -723,7 +726,7 @@ main(int argc, char *argv[])
} }
} }
/* NB: read oob data before normal reads */ /* NB: read OOB data before normal reads */
if (fd1 > 0) if (fd1 > 0)
if (FD_ISSET(fd1, &er)) { if (FD_ISSET(fd1, &er)) {
@ -782,14 +785,14 @@ main(int argc, char *argv[])
buf1_written += r; buf1_written += r;
} }
/* check if write data has caught read data */ /* Check if write data has caught read data */
if (buf1_written == buf1_avail) if (buf1_written == buf1_avail)
buf1_written = buf1_avail = 0; buf1_written = buf1_avail = 0;
if (buf2_written == buf2_avail) if (buf2_written == buf2_avail)
buf2_written = buf2_avail = 0; buf2_written = buf2_avail = 0;
/* one side has closed the connection, keep /* One side has closed the connection, keep
writing to the other side until empty */ writing to the other side until empty */
if (fd1 < 0 && buf1_avail \- buf1_written == 0) if (fd1 < 0 && buf1_avail \- buf1_written == 0)