- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: java NIO select() call does not block
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2010 12:27 AM
09-23-2010 12:27 AM
java NIO select() call does not block
I created a non-blocking SocketChannel and registered it with OP_CONNECT and OP_READ.
The following select() returns 0 without ever blocking:
java.nio.channels.Selector mxChannelsSelector;
int selectReturn = mxChannelsSelector.select(lTimeout);
The same code works with Java 1.4.1 on HP-UX 11.11. It works on Linux RedHat 5 + Java 1.6.
How to make it work on HP-UX with the latest Java JVM (1.6)?
Chris
- Tags:
- Java
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2010 03:35 AM
09-29-2010 03:35 AM
Re: java NIO select() call does not block
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Set;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
SocketChannel mxSocketChannel = null;
Selector mxChannelsSelector = null;
try
{
mxSocketChannel = SocketChannel.open();
mxSocketChannel.configureBlocking(false);
InetSocketAddress mxRemoteAddress = new InetSocketAddress("localhost", 51421);
boolean t_connect = mxSocketChannel.connect(mxRemoteAddress);
int mnInterestOps = SelectionKey.OP_CONNECT;
//int mnInterestOps = SelectionKey.OP_CONNECT | SelectionKey.OP_READ;
//create the selector
mxChannelsSelector= SelectorProvider.provider().openSelector();
//register the channel with the selector indicating an interest in
// accepting new connections
SelectionKey x_key = mxSocketChannel.register(mxChannelsSelector, mnInterestOps);
//select loop
while(true)
{
//block until connect response is received
int selectReturn = mxChannelsSelector.select(0);
if (selectReturn == 0)
{
System.out.println("select returned 0");
}
} //end while(true)
} //end try block
catch (Exception ex)
{
System.out.println("exception " + ex);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2010 10:16 PM
09-29-2010 10:16 PM
Re: java NIO select() call does not block
You could also use tusc to see what system calls are being made.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2010 03:46 AM
10-04-2010 03:46 AM
Re: java NIO select() call does not block
Here is the woking code sample:
public static void main(String[] args) {
SocketChannel mxSocketChannel = null;
Selector mxChannelsSelector = null;
int mnInterestOps = 0;
try
{
mxSocketChannel = SocketChannel.open();
mxSocketChannel.configureBlocking(false);
InetSocketAddress mxRemoteAddress = new InetSocketAddress("localhost", 51421);
boolean t_connect = mxSocketChannel.connect(mxRemoteAddress);
if (t_connect)
{
System.out.println("connected");
mnInterestOps = SelectionKey.OP_WRITE | SelectionKey.OP_READ ;
}
else
{
mnInterestOps = SelectionKey.OP_CONNECT;
}
//create the selector
mxChannelsSelector= SelectorProvider.provider().openSelector();
//register the channel with the selector indicating an interest
SelectionKey x_key = mxSocketChannel.register(mxChannelsSelector, mnInterestOps);
//select loop
while(true)
{
//block until connect response is received
int selectReturn = mxChannelsSelector.select(0);
if (selectReturn == 0)
{
System.out.println("select returned 0");
}
Set myKeys = mxChannelsSelector.selectedKeys();
if (!myKeys.isEmpty())
{
Iterator x_readyKeysIterator = myKeys.iterator();
// Iterate over the set of keys for which events are available
while (x_readyKeysIterator.hasNext())
{
SelectionKey x_selectionKey = (SelectionKey) x_readyKeysIterator.next();
// Remove selected key
x_readyKeysIterator.remove();
if (x_selectionKey.isValid())
{
if (x_selectionKey.isConnectable()) {
mxSocketChannel.finishConnect();
System.out.println("connection 2 accepted");
}
else if (x_selectionKey.isWritable()) {
System.out.println("writable channel, select return =" + selectReturn);
x_selectionKey.cancel();
}
else if (x_selectionKey.isReadable()) {
System.out.println("readable channel" + selectReturn);
}
}
else
{
//cancel the channel registration with this selector
x_selectionKey.cancel();
System.out.println("key not valid");
}
}
}
} //end while(true)
} //end try block
catch (Exception ex)
{
System.out.println("exception " + ex);
}
}
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2010 03:48 AM
10-04-2010 03:48 AM
Re: java NIO select() call does not block
Chris