日期:2014-05-20  浏览次数:20778 次

java NIO里面的东西,帮忙解释一下
if(client.isConnectionPending()){
client.finishConnect();
System.out.println("client finish conection");
sendbuffer.clear();
sendbuffer.put("hello server".getBytes());
sendbuffer.flip();
client.write(sendbuffer);
}

------解决方案--------------------
判断是否连接到服务器,连接成功就向服务器发送字符串
------解决方案--------------------
引用:
client.finishConnect();不是结束连接了吗?怎么后面还发送字符串呢


是连接啊!

public abstract boolean finishConnect()
                               throws IOException

Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket channel in non-blocking mode and then invoking its connect method. Once the connection is established, or the attempt has failed, the socket channel will become connectable and this method may be invoked to complete the connection sequence. If the connection operation failed then invoking this method will cause an appropriate IOException to be thrown.

If this channel is already connected then this method will not block and will immediately return true. If this channel is in non-blocking mode then this method will return false if the connection process is not yet complete. If this channel is in blocking mode then this method will block until the connection either completes or fails, and will always either return true or throw a checked exception describing the failure.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

Returns:
    true if, and only if, this channel's socket is now connected
Throws:
    NoConnectionPendingException - If this channel is not connected and a connection operation has not been initiated
    ClosedChannelException - If this channel is closed
    AsynchronousCloseException - If another thread closes this channel while the connect operation is in progress
    ClosedByInterruptException - If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupt status
    IOException - If some other I/O error occurs