本文整理汇总了Java中java.nio.channels.ServerSocketChannel.accept方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocketChannel.accept方法的具体用法?Java ServerSocketChannel.accept怎么用?Java ServerSocketChannel.accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.ServerSocketChannel
的用法示例。
在下文中一共展示了ServerSocketChannel.accept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: call
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public ByteBuffer call() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
buff = ByteBuffer.allocate(bufferSize);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
while (!stop.isLocked()) {
RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.read(buff);
FileChannel channel = temp.getChannel();
channel.write(buff);
if (!pause.isLocked()) {
MappedByteBuffer b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
b.clear();
}
temp.close();
buff.clear();
}
return null;
}
示例2: call
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public ByteBuffer call() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
buff = ByteBuffer.allocate(bufferSize);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
MappedByteBuffer b;
while (!stop.isLocked()) {
sync=0;
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.read(buff);
FileChannel channel = temp.getChannel();
channel.write(buff);
if (!pause.isLocked()) {
b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
sync = 1;
if(sync==2){
b.clear();
}
}
buff.clear();
}
temp.close();
return null;
}
示例3: acceptConnection
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private final void acceptConnection(final SelectionKey key, MMOConnection<T> con)
{
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
try
{
while ((sc = ssc.accept()) != null)
{
if (_acceptFilter == null || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<T>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
sc.socket().close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例4: doAccept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void doAccept(SelectionKey sk){
ServerSocketChannel server=(ServerSocketChannel)sk.channel();
SocketChannel clientChannel;
try {
//获取客户端的channel
clientChannel = server.accept();
clientChannel.configureBlocking(false);
//register the channel for reading
SelectionKey clientKey=clientChannel.register(selector,SelectionKey.OP_READ);
//Allocate an EchoClient instance and attach it to this selection key.
EchoClient echoClient=new EchoClient();
clientKey.attach(echoClient);
InetAddress clientAddress=clientChannel.socket().getInetAddress();
System.out.println("Accepted connetion from "+clientAddress.getHostAddress()+".");
}catch (Exception e){
System.out.println("Failed to accept new client");
e.printStackTrace();
}
}
示例5: accept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
return null;
}
// accept the connection from the client
SocketChannel ch = handle.accept();
if (ch == null) {
return null;
}
return new NioSocketSession(this, processor, ch);
}
示例6: acceptClient
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void acceptClient(Selector selector, ServerSocketChannel serverSocketChannel) throws IOException {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
// will register the socket on the selector
Client client = new Client(selector, socketChannel, this::removeClient);
clients.add(client);
Log.i(TAG, "Client #" + client.getId() + " connected");
}
示例7: doAccept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* 和Socket编程很类似,当有一个新的客户端连接接入时,就会有一个新的Channel产生代表这个连接。
* 生成的clientChannel就表示和客户端通信的通道。
*/
private void doAccept(SelectionKey sk) {
ServerSocketChannel server = (ServerSocketChannel) sk.channel();
SocketChannel clientChannel;
try {
clientChannel = server.accept();
//将这个Channel配置为非阻塞模式,也就是要求系统在准备好IO后,再通知我们的线程来读取或者写入。
clientChannel.configureBlocking(false);
// Register this channel for reading.
//将新生成的Channel注册到selector选择器上,并告诉Selector,我现在对读(OP_READ)操作感兴趣。
// 这样,当Selector发现这个Channel已经准备好读时,就能给线程一个通知。
SelectionKey clientKey = clientChannel.register(selector, SelectionKey.OP_READ);
// Allocate an EchoClient instance and attach it to this selection key.
//一个EchoClient实例代表一个客户端。
// 我们将这个客户端实例作为附件,附加到表示这个连接的SelectionKey上。
// 这样在整个连接的处理过程中,我们都可以共享这个EchoClient实例。
EchoClient echoClient = new EchoClient();
clientKey.attach(echoClient);
InetAddress clientAddress = clientChannel.socket().getInetAddress();
System.out.println("Accepted connection from " + clientAddress.getHostAddress() + ".");
} catch (Exception e) {
System.out.println("Failed to accept new client.");
e.printStackTrace();
}
}
示例8: handAccept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void handAccept(SelectionKey key) throws IOException {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
System.out.println("accept client");
}
示例9: acceptConnection
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
{
final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
try
{
while ((sc = ssc.accept()) != null)
{
if ((_acceptFilter == null) || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey, TCP_NODELAY);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
{
sc.socket().close();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例10: handleAccept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public static void handleAccept(SelectionKey key) throws IOException {
ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
SocketChannel sc = ssChannel.accept();
sc.configureBlocking(false);
//
sc.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocateDirect(BUF_SIZE));
}
示例11: run
import java.nio.channels.ServerSocketChannel; //导入方法依赖的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);
}
}
示例12: launchWithSocketChannel
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
ssc.socket().getLocalPort());
SocketChannel sc1 = SocketChannel.open(isa);
SocketChannel sc2 = ssc.accept();
launch(className, options, args, Util.getFD(sc2));
sc2.close();
ssc.close();
return sc1;
}
示例13: xferTest07
import java.nio.channels.ServerSocketChannel; //导入方法依赖的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();
}
示例14: openForProject
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
* Registers the connection and initiates the listening socket.
* @param project the project which is being run / debugged
* @param debugger if true, the connection will pair with a debugger session.
*/
public ShellAgent openForProject(Project p, boolean debugger) throws IOException {
ServerSocket ss;
String encodedKey;
boolean shouldInit = false;
synchronized (this) {
shouldInit = usedKeys.isEmpty();
do {
BigInteger key = BigInteger.probablePrime(64, keyGenerator);
encodedKey = key.toString(Character.MAX_RADIX);
} while (!usedKeys.add(encodedKey));
}
if (shouldInit) {
init();
}
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
SocketAddress local = new InetSocketAddress(
// PENDING: choose something better for remote debugging!
InetAddress.getLoopbackAddress(),
0);
ssc.bind(local);
ssc.accept();
ss = ssc.socket();
LOG.log(Level.FINE, "Creating new server socket {0} for project: {1}", new Object[] {
ss, p
});
ShellAgent agent = new ShellAgent(this, p, ss, encodedKey, debugger);
synchronized (this) {
registeredAgents.put(encodedKey, agent);
}
synchronized (requests) {
servers.wakeup();
requests.add(agent);
}
return agent;
}
示例15: accept
import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void accept (ServerSocketChannel channel, SelectionKey key) throws IOException {
val client = channel.accept();
client.configureBlocking(false);
client.register(key.selector(), OP_READ, new Message());
}