Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Nonblocking socket question of client'sprogram ,help!!!!!!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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???
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
duplicate in the IO forum
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic