/* * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 4617165 * @summary Ensure that socket hangups are handled correctly * @library .. * @build TestUtil * @run main Hangup
*/
staticvoid doSelect(Selector sel, SelectionKey sk, int count) throws IOException
{ int n = sel.select(); if (n != 1) thrownew Failure("Select returned zero");
Set sks = sel.selectedKeys(); if (sks.size() != 1) thrownew Failure("Wrong size for selected-key set: "
+ sks.size()); if (!sks.remove(sk)) thrownew Failure("Key not in selected-key set");
log.println("S: Socket selected #" + count);
}
int ns = 0; // Number of selection operations done
// Set up server socket
ssc = ServerSocketChannel.open();
SocketAddress sa = TestUtil.bindToRandomPort(ssc);
log.println("S: Listening on port "
+ ssc.socket().getLocalPort());
// Connect client
cl = SocketChannel.open(sa);
log.println("C: Connected via port "
+ cl.socket().getLocalPort());
// Create selector and register server side
sel = Selector.open();
sv.configureBlocking(false);
SelectionKey sk = sv.register(sel, SelectionKey.OP_READ);
ByteBuffer stuff = ByteBuffer.allocate(10); int n;
if (writeFromClient) {
// Write from client, read from server
stuff.clear(); if (cl.write(stuff) != stuff.capacity()) thrownew Failure("Incorrect number of bytes written");
log.println("C: Wrote stuff");
dally();
doSelect(sel, sk, ++ns);
stuff.clear(); if (sv.read(stuff) != stuff.capacity()) thrownew Failure("Wrong number of bytes read");
log.println("S: Read stuff");
}
// Close client side
cl.close();
log.println("C: Socket closed");
dally();
// Select again
doSelect(sel, sk, ++ns);
if (readAfterClose) { // Read from client after client has disconnected
stuff.clear(); if (sv.read(stuff) != -1) thrownew Failure("Wrong number of bytes read");
log.println("S: Read EOF");
}
// Select a couple more times just to make sure we're doing // the right thing
doSelect(sel, sk, ++ns);
doSelect(sel, sk, ++ns);
} finally { if (ssc != null)
ssc.close(); if (cl != null)
cl.close(); if (sv != null)
sv.close(); if (sel != null)
sel.close();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.