public bool isConnected() { bool conn = false; if (activeConnection && sockChan != null && sockChan.isConnected()) { // try to read 1 byte, if this fails then the socket was reset int nread = -1; try { ByteBuffer tmp = ByteBuffer.allocate(1); sockChan.socket().Client.Blocking = false; nread = sockChan.read(tmp); // fast non-blocking read sockChan.socket().Client.Blocking = true; } catch (IOException e) { } //System.Console.WriteLine("read " + nread + "bytes"); System.out.flush(); if (nread < 0) { activeConnection = false; } else { conn = true; } } return(conn); }
//********************************************************************* // protected methods and variables from here on //********************************************************************* protected ByteBuffer readAll(ByteBuffer dst) { int cap = dst.capacity(); while (cap > 0) { int now = sockChan.read(dst); cap -= now; } return(dst); }
//********************************************************************* // protected methods and variables from here on //********************************************************************* protected ByteBuffer readAll(ByteBuffer dst) { lock (this) { // Implement our own read-time-out for when the connection goes down.... int cap = dst.capacity(); while (cap > 0 && sockChan.isConnected()) { int now = sockChan.read(dst); if (now == 0) { return(dst); // read failed = abort } cap -= now; } return(dst); } }