posted 21 years ago
Nonblocking socket client'sprogram like this:
SocketChannel m_oCltChannel = null;
Selector m_oReadSelector = null;
Selector m_oWriteSelector = null;
m_oReadSelector = Selector.open();
m_oWriteSelector = Selector.open();
m_oCltChannel = SocketChannel.open();
m_oCltChannel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(m_sServerHost, m_iPort);
m_oCltChannel.connect(isa);
m_oCltChannel.register(m_oReadSelector,SelectionKey.OP_CONNECT | SelectionKey.OP_READ );
m_oClt.m_oCltChannel.register(m_oWriteSelector,SelectionKey.OP_WRITE );
while(true){
if(m_oReadSelector.select()>0){
Set oReadKeys = m_oReadSelector.selectedKeys();
Iterator i = oReadKeys.iterator();
while(i.hasNext()){
SelectionKey tmp = (SelectionKey)i.next();
i.remove();
if (tmp.isConnectable()){
/*why come here always ???*/
if (((SocketChannel)tmp.channel()).isConnectionPending()){
while(!((SocketChannel)tmp.channel()).finishConnecta(){}
}
if (tmp.isReadable()){
/*why dont come here always ???*/
}
}
}
if(m_oWriteSelector.select()>0){
Set oWriteKeys = m_oWriteSelector.selectedKeys();
Iterator i = oWriteKeys.iterator();
while(i.hasNext()){
SelectionKey tmp = (SelectionKey)i.next();
i.remove();
if (tmp.isWriteable()){
/*why dont come here always ???*/
}
}
}
}
if i use blocking socket, this while do well work .what happend???