本文整理汇总了Java中java.nio.channels.SocketChannel.configureBlocking方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.configureBlocking方法的具体用法?Java SocketChannel.configureBlocking怎么用?Java SocketChannel.configureBlocking使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.configureBlocking方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SocketConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public SocketConnection(SocketChannel socket, MessageSerializer streamSerializer, StatefulSerializer<T> messageSerializer) {
this.socket = socket;
try {
// NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while
// keeping writes active. For example, Socket.shutdownInput() does not work on Windows.
socket.configureBlocking(false);
outstr = new SocketOutputStream(socket);
instr = new SocketInputStream(socket);
} catch (IOException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
InetSocketAddress localSocketAddress = (InetSocketAddress) socket.socket().getLocalSocketAddress();
localAddress = new SocketInetAddress(localSocketAddress.getAddress(), localSocketAddress.getPort());
InetSocketAddress remoteSocketAddress = (InetSocketAddress) socket.socket().getRemoteSocketAddress();
remoteAddress = new SocketInetAddress(remoteSocketAddress.getAddress(), remoteSocketAddress.getPort());
objectReader = messageSerializer.newReader(streamSerializer.newDecoder(instr));
encoder = streamSerializer.newEncoder(outstr);
objectWriter = messageSerializer.newWriter(encoder);
}
示例2: openSocketChannel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public synchronized SocketChannel openSocketChannel(Address address) throws IOException {
if (this.clientSocketChannel != null && this.clientSocketChannel.isOpen()) {
return this.clientSocketChannel;
}
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
this.registerPendingMap.put(socketChannel, OP_CONNECT | OP_READ);
this.selector.wakeup();
LOG.info("Connecting to " + address + "...");
socketChannel.connect(new InetSocketAddress(address.getHost(), address.getPort()));
while (!socketChannel.finishConnect()) {
}
LOG.info("Connected.");
this.clientSocketChannel = socketChannel;
return this.clientSocketChannel;
}
示例3: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static SocketChannel connect(SocketAddress remote, final int timeoutMillis) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
sc.configureBlocking(true);
sc.socket().setSoLinger(false, -1);
sc.socket().setTcpNoDelay(true);
sc.socket().setReceiveBufferSize(1024 * 64);
sc.socket().setSendBufferSize(1024 * 64);
sc.socket().connect(remote, timeoutMillis);
sc.configureBlocking(false);
return sc;
} catch (Exception e) {
if (sc != null) {
try {
sc.close();
} catch (IOException e1) {
logger.error(e1.getMessage(), e1);
}
}
}
return null;
}
示例4: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
NioThread nt=new NioThread();
nt.start();
Thread.sleep(1000);
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress("localhost", 9999));
ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
DuplexNioConnection conn=new DuplexNioConnection(m);
m.start();
CoolRMIClient client=new CoolRMIClient(getClass().getClassLoader(), new RMINioClientConnectionFactory(conn), false);
Iremote r= (Iremote)client.getService(Iremote.class, Iremote.class.getName());
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
}
示例5: newHandle
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected SocketChannel newHandle(SocketAddress localAddress) throws Exception {
SocketChannel ch = SocketChannel.open();
int receiveBufferSize = (getSessionConfig()).getReceiveBufferSize();
if (receiveBufferSize > 65535) {
ch.socket().setReceiveBufferSize(receiveBufferSize);
}
if (localAddress != null) {
ch.socket().bind(localAddress);
}
ch.configureBlocking(false);
return ch;
}
示例6: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void run() {
try {
SelectionKey key;
log.debug("运行连接请求处理与服务请求接收线程,线程ID:" + Thread.currentThread().getId());
while (true) {
selector.select();
// 首先处理连接请求,并注册连接后的Channel注册到选择器
// 这种处理连接的方式,可能造成某一个选择器注册的Channel或选择键比其他线程多,导致线程内的繁忙程度不一致
while ((key = connQueue.poll()) != null) {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
// 接受一个连接
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
if (log.isDebugEnabled()) {
log.debug("接受一个客户端连接,处理连接请求的线程ID:" + Thread.currentThread().getId());
}
}
// 再处理服务请求的选择键,并将选择键放入待处理服务队列中
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> it = keys.iterator();
while (it.hasNext()) {
SelectionKey keytmp = it.next();
it.remove();
if (keytmp.isReadable()) {
reqQueue.add(keytmp);
}
}
}
} catch (IOException e) {
log.error("处理连接请求,并接收服务请求处理异常", e);
}
}
示例7: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
NioThread nt=new NioThread();
nt.start();
Thread.sleep(1000);
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress("localhost", 9999));
ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
m.start();
MainChannel mc=new MainChannel();
mc.register(m);
}
示例8: initClient
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void initClient(String ip, int port) throws IOException { // 获得一个Socket通道
SocketChannel channel = SocketChannel.open(); // 设置通道为非阻塞
channel.configureBlocking(false); // 获得一个通道管理器
this.selector = Selector.open(); // 客户端连接服务器,其实方法执行并没有实现连接,需要在listen()方法中调
channel.connect(new InetSocketAddress(ip, port));
channel.register(selector, SelectionKey.OP_CONNECT);
}
示例9: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void run() {
try {
SocketChannel sc = ssc.accept();
sc.configureBlocking(true);
execThPool.execute(new FDTNetPerfClient(sc));
} catch (Throwable t) {
t.printStackTrace();
}
}
示例10: xferTest07
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void xferTest07() throws Exception { // for bug 5103988
File source = File.createTempFile("source", null);
source.deleteOnExit();
FileChannel sourceChannel = new RandomAccessFile(source, "rw")
.getChannel();
sourceChannel.position(32000L)
.write(ByteBuffer.wrap("The End".getBytes()));
// The sink is a non-blocking socket channel
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress sa = new InetSocketAddress(
InetAddress.getLocalHost(), ssc.socket().getLocalPort());
SocketChannel sink = SocketChannel.open(sa);
sink.configureBlocking(false);
SocketChannel other = ssc.accept();
long size = sourceChannel.size();
// keep sending until congested
long n;
do {
n = sourceChannel.transferTo(0, size, sink);
} while (n > 0);
sourceChannel.close();
sink.close();
other.close();
ssc.close();
source.delete();
}
示例11: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void connect(NioThread nt, SocketAddress address, byte[] thisId, byte[] otherId) throws Exception
{
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(address);
connect(nt, sc, true, thisId, otherId);
}
示例12: createSock
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* create a socket channel.
* @return the created socket channel
* @throws IOException
*/
SocketChannel createSock() throws IOException {
SocketChannel sock;
sock = SocketChannel.open();
sock.configureBlocking(false);
sock.socket().setSoLinger(false, -1);
sock.socket().setTcpNoDelay(true);
return sock;
}
示例13: openSocketChannelNonBlocking
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* open non-blocking socket channel
* @param msg
* message
* @return
* SocketChannel
*/
public SocketChannel openSocketChannelNonBlocking(String msg) {
SocketChannel socketChannel;
do {
try {
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
return socketChannel;
} catch (IOException e) {
this.logger.warning(msg + e.toString());
}
} while (true);
}
示例14: doWork
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void doWork() throws Exception {
for (; ; ) {
//TODO, stop the server (this loop and the executor) if there are no more connected sockets
while (sel.select() > 0)
;
Iterator it = sel.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = (SelectionKey) it.next();
if (sk.isAcceptable()) {
ServerSocketChannel ssc = (ServerSocketChannel) sk.channel();
SocketChannel sc = ssc.accept();
if (!sshMode) {// standalone mode
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_READ);
} else {// SSH mode
if (allowedIP != null && !allowedIP.equals(sc.socket().getInetAddress().getHostAddress())) {
// just the IP passed on secured SSH control connection is allowed to connect
System.err.println(" [" + allowedIP + "] does not match " + sc.socket().getInetAddress().getHostAddress());
sc.close();
} else {// allowed connection
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_READ);
if (--connectionNo == 0) {
// stop listening for other connection
this.ssc.keyFor(sel).cancel();
this.ssc.close();
}
}
}
} else if (sk.isReadable()) {
sk.interestOps(sk.interestOps() & ~SelectionKey.OP_READ);
executor.execute(new ReaderTask(sk));
}
it.remove();
}
}
}
示例15: handleClients
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void handleClients() throws Exception {
int selectCount = 0;
while (true) {
int createdCount = 0;
synchronized (this) {
if (connectionsNeeded > 0) {
while (connectionsNeeded > 0 && createdCount < 20) {
connectionsNeeded--;
createdCount++;
totalCreated++;
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(address);
if (!channel.finishConnect()) {
channel.register(selector,
SelectionKey.OP_CONNECT);
}
}
log("Started total of " +
totalCreated + " client connections");
Thread.sleep(200);
}
}
if (createdCount > 0) {
selector.selectNow();
} else {
selectCount++;
long startTime = System.nanoTime();
selector.select();
long duration = durationMillis(startTime);
log("Exited clientSelector.select(), loop #"
+ selectCount + ", duration = " + duration + "ms");
}
int keyCount = -1;
Iterator<SelectionKey> keys =
selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
synchronized (key) {
keyCount++;
keys.remove();
if (!key.isValid()) {
log("Ignoring client key #" + keyCount);
continue;
}
int readyOps = key.readyOps();
if (readyOps == SelectionKey.OP_CONNECT) {
key.interestOps(0);
((SocketChannel) key.channel()).finishConnect();
} else {
log("readyOps() on client key #" + keyCount +
" returned " + readyOps);
}
}
}
}
}