Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb Newsgroups: comp.lang.java.programmer Subject: Re: server-side Socket does not recognize broken connection. Date: Wed, 29 May 2019 14:50:14 -0000 (UTC) Organization: A noiseless patient Spider Lines: 38 Message-ID: References: <1v9ncmqz4ep4j$.1mdftm8twalol$.dlg@40tude.net> Reply-To: avl@logic.at Injection-Date: Wed, 29 May 2019 14:50:14 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="4026fc8d5191aa509b45c747fc04e2b7"; logging-data="10958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EMKdUHDFkAoRC8tOqrBn2" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:cZRZyj02uKrVpWbdGS9uUyhH1kM= Xref: csiph.com comp.lang.java.programmer:38972 Joerg Meier wrote: > On Wed, 29 May 2019 11:03:18 -0000 (UTC), Andreas Leitgeb wrote: >> PS: The real code does more interesting stuff while no input is available, >> so it cannot wait blockingly. The real code also has other exits from the >> loop (based on input received), but that's not relevant for this SSCCE. > In addition to Erics excellent answer, Thanks also to Eric! > I would like to add that I'm pretty > certain that you can not write code like that without resorting to > blocking. Watching the Java server with strace (on linux), I see that Java calls the system: ioctl(6, FIONREAD, [0]) = 0 Now I'm going to read up on why this syscall wouldn't report a broken socket, and what other syscall (short of actually reading and risking to block) would allow me to tell idle from broken. Calling "lsof" on the Java process' pid shows that the OS does know the socket is broken: java 29340 avl 6u IPv6 113368285 0t0 TCP localhost:65432->localhost:58500 (CLOSE_WAIT) > You may want to look at NIO or higher level frameworks if you can > not astract the blocking aspect away on your own for some reason. A separate thread to read blockingly and shovel the results into a Queue (as Eric suggested) is plausible, assuming that it works. Trying to replace inputStreams by nio is also on my "TO CHECK" list. Thanks anyway for pointing me in the direction that available() alone just isn't designed to recognize broken sockets. I consider my question answered. Thanks!