X-Received: by 2002:ad4:53c6:: with SMTP id k6mr11878817qvv.144.1559358818828; Fri, 31 May 2019 20:13:38 -0700 (PDT) X-Received: by 2002:a0d:e852:: with SMTP id r79mr7830815ywe.329.1559358818372; Fri, 31 May 2019 20:13:38 -0700 (PDT) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!c48no1297069qtc.0!news-out.google.com!i13ni440qtr.0!nntp.google.com!c48no1297067qtc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Fri, 31 May 2019 20:13:37 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.32.226.20; posting-account=juTZwwoAAAAuI5HvCVIdPIwAiaNWKLmY NNTP-Posting-Host: 24.32.226.20 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <92ecf367-9f62-4d72-84cf-c9308005ff3c@googlegroups.com> Subject: Re: server-side Socket does not recognize broken connection. From: trisha guillot Injection-Date: Sat, 01 Jun 2019 03:13:38 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 53 Xref: csiph.com comp.lang.java.programmer:38975 On Wednesday, May 29, 2019 at 4:03:29 AM UTC-7, Andreas Leitgeb wrote: > I have a ServerSocket on some port. whenever ".accept()" returns a > connection, it goes to a loop that (among doing other stuff) polls > the socket's input stream's ".available()" and if it returns > 0 > it reads and processes that data. (SSCCE at end of post) > > A client (e.g. using plain old "telnet" on unix) connects, types a > bit, then breaks the connection using Ctrl-] and "close", and is back > on the shell. (closing the terminal window that's running telnet > has same effect, ditto killing the telnet process - apparently > whatever breaks, or even properly shuts down the connection from > client side.) > > The Socket on server side, however keeps reporting 0 on .available(), > rather than throwing some exception for the broken socket. > > Single stepping the .available() method (class AbstractPlainSocketImpl) > shows that it just never receives the ConnectionResetException, and thus > won't ever get to throw new IOException("Stream closed.") itself. > Field resetState never changes away from CONNECTION_NOT_RESET. > > What am I doing wrong wrt. EOF-detection in ".available()" ? > > 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. > > PS: unlike most of my recent posts, this one is *not* Java11-related. > > > SSCCE: ServerTest.java > > import javax.net.ServerSocketFactory; > import java.net.ServerSocket; > import java.net.Socket; > import java.io.InputStream; > import java.io.IOException; > > public class ServerTest { > public static void main(String[] args) throws IOException,InterruptedException { > > ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(65432); > Socket clientSocket = serverSocket.accept(); > InputStream is = clientSocket.getInputStream(); > > while (true) { > if (is.available() > 0) { > System.out.println( is.read() ); > } > Thread.sleep(100); > } > } > }