本文整理汇总了Java中java.nio.channels.SocketChannel.isConnected方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.isConnected方法的具体用法?Java SocketChannel.isConnected怎么用?Java SocketChannel.isConnected使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.isConnected方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createOrReuseSocket
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public SocketChannel createOrReuseSocket(InetAddress inetAddress, int port) throws IOException {
String key = NIOHandler.makeKey(inetAddress, port);
SocketChannel channel = null;
keyedSemaphore.enterIOCriticalSection(key);
try {
channel = getSocket(key);
if(channel != null && (!channel.isConnected() || !channel.isOpen())) {
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("Channel disconnected " + channel);
channel = null;
}
if(channel == null) { // this is where the threads will race
SocketAddress sockAddr = new InetSocketAddress(inetAddress, port);
channel = messageProcessor.blockingConnect((InetSocketAddress) sockAddr, 10000);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("create channel = " + channel + " " + inetAddress + " " + port);
if(channel != null && channel.isConnected()) {
putSocket(NIOHandler.makeKey(inetAddress, port), channel);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("channel cached channel = " + channel);
}
}
return channel;
} finally {
keyedSemaphore.leaveIOCriticalSection(key);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("Returning socket " + key + " channel = " + channel);
}
}
示例2: main
import java.nio.channels.SocketChannel; //导入方法依赖的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();
}
示例3: moveDataIntoIndividualChannelBuffers
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private int moveDataIntoIndividualChannelBuffers()
{
byte[] dst = {};
synchronized( buffer )
{
if (buffer.position() == 0)
return 0;
buffer.flip();
dst = new byte[buffer.remaining()];
buffer.get(dst);
buffer.compact();
}
for( SocketChannel chan : connectionBuffers.keySet() )
{
if(!chan.isConnected())
{
connectionBuffers.remove(chan);
continue;
}
ByteBuffer connectionBuffer = connectionBuffers.get(chan);
try
{
if( connectionBuffer.remaining() > dst.length )
{
connectionBuffer.put(dst);
haveDataInIndividualBuffers = true;
}
else
{
String remoteClientAddress = chan.toString();
log.error( "Overflow while trying to write to the output buffer associated with address "+remoteClientAddress+".");
}
}catch(BufferOverflowException ex)
{
log.error( "Overflow while trying to write to an output buffer.");
}
}
return dst.length;
}