本文整理匯總了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());
}