本文整理汇总了Java中java.nio.channels.ServerSocketChannel.close方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocketChannel.close方法的具体用法?Java ServerSocketChannel.close怎么用?Java ServerSocketChannel.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.ServerSocketChannel
的用法示例。
在下文中一共展示了ServerSocketChannel.close方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: stop
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* Stop the server.
*/
@Override
public void stop() {
try{
this_mon.enter();
for (int i=0;i<server_channels.size();i++){
try {
ServerSocketChannel server_channel = (ServerSocketChannel)server_channels.get(i);
VirtualAcceptSelector.getSingleton().cancel( server_channel );
server_channel.close();
}
catch( Throwable t ) { Debug.out( t ); }
}
server_channels.clear();
}finally{
this_mon.exit();
}
}
示例2: bindNIOServerSocket
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* Creates a NIO ServerSocketChannel, and gets the ServerSocket from
* there. Then binds the obtained socket.
* This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
* IPv6 address. Works on Oracle JDK 1.7.
*/
private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
while (true) {
int port = HBaseTestingUtility.randomFreePort();
InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
ServerSocketChannel channel = null;
ServerSocket serverSocket = null;
try {
channel = ServerSocketChannel.open();
serverSocket = channel.socket();
serverSocket.bind(addr); // This does not work
break;
} catch (BindException ex) {
//continue
} finally {
if (serverSocket != null) {
serverSocket.close();
}
if (channel != null) {
channel.close();
}
}
}
}
示例3: close
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void close(ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
handle.close();
}
示例4: tryClose
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void tryClose(ServerSocketChannel s) {
try {
s.close();
} catch (IOException sse) {
LOG.error("Error while closing server socket.", sse);
}
}
示例5: bindToPort
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@VisibleForTesting
ServerSocketChannel bindToPort() throws IOException {
// Bind to the first available port in the range
ServerSocketChannel channel = null;
for (int port = ConnectionHandler.PORT_RANGE_START; port <= ConnectionHandler.PORT_RANGE_END; port++) {
final InetSocketAddress tryAddress = new InetSocketAddress(InetAddress.getLocalHost(), port);
try {
channel = ServerSocketChannel.open();
channel.socket().bind(tryAddress);
channel.configureBlocking(false);
break;
} catch (final IOException ioe) {
// Ignore, try next port
logger.warn("Could not bind to {}, trying next port...", tryAddress);
try {
if (channel != null) channel.close();
} catch (final IOException ignored) {
}
}
}
if (channel == null || !channel.socket().isBound()) {
throw new IOException("No available port for the BitTorrent client!");
}
return channel;
}
示例6: launchWithSocketChannel
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
ssc.socket().getLocalPort());
SocketChannel sc1 = SocketChannel.open(isa);
SocketChannel sc2 = ssc.accept();
launch(className, options, args, Util.getFD(sc2));
sc2.close();
ssc.close();
return sc1;
}
示例7: launchWithServerSocketChannel
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
throws IOException
{
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
int port = ssc.socket().getLocalPort();
launch(className, options, args, Util.getFD(ssc));
ssc.close();
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
return SocketChannel.open(isa);
}
示例8: xferTest07
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@Test
public void xferTest07() throws Exception { // for bug 5103988
File source = File.createTempFile("source", null);
source.deleteOnExit();
FileChannel sourceChannel = new RandomAccessFile(source, "rw")
.getChannel();
sourceChannel.position(32000L)
.write(ByteBuffer.wrap("The End".getBytes()));
// The sink is a non-blocking socket channel
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress sa = new InetSocketAddress(
InetAddress.getLocalHost(), ssc.socket().getLocalPort());
SocketChannel sink = SocketChannel.open(sa);
sink.configureBlocking(false);
SocketChannel other = ssc.accept();
long size = sourceChannel.size();
// keep sending until congested
long n;
do {
n = sourceChannel.transferTo(0, size, sink);
} while (n > 0);
sourceChannel.close();
sink.close();
other.close();
ssc.close();
source.delete();
}
示例9: main
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetAddress lh = InetAddress.getLocalHost();
final SocketChannel sc = SocketChannel.open();
final InetSocketAddress isa =
new InetSocketAddress(lh, ssc.socket().getLocalPort());
// establish connection in another thread
Runnable connector =
new Runnable() {
public void run() {
try {
sc.connect(isa);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
Thread thr = new Thread(connector);
thr.start();
// wait for connect to be established and for thread to
// terminate
do {
try {
thr.join();
} catch (InterruptedException x) { }
} while (thr.isAlive());
// check connection is established
if (!sc.isConnected()) {
throw new RuntimeException("SocketChannel not connected");
}
// close channel - this triggered the bug as it attempted to signal
// a thread that no longer exists
sc.close();
// clean-up
ssc.accept().close();
ssc.close();
}
示例10: getBindablePort
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@Override
public int
getBindablePort(
int prefer_port )
throws IOException
{
final int tries = 1024;
Random random = new Random();
for ( int i=1;i<=tries;i++ ){
int port;
if ( i == 1 && prefer_port != 0 ){
port = prefer_port;
}else{
port = i==tries?0:random.nextInt(20000) + 40000;
}
ServerSocketChannel ssc = null;
try{
ssc = ServerSocketChannel.open();
ssc.socket().setReuseAddress( true );
bind( ssc, null, port );
port = ssc.socket().getLocalPort();
ssc.close();
return( port );
}catch( Throwable e ){
if ( ssc != null ){
try{
ssc.close();
}catch( Throwable f ){
Debug.printStackTrace(e);
}
ssc = null;
}
}
}
throw( new IOException( "No bindable ports found" ));
}